// The following example displays an application that provides the ability to // open rich text files (rtf) into the RichTextBox. The example demonstrates // using the FolderBrowserDialog to set the default directory for opening files.// The OpenFileDialog class is used To open the file.using system; using system.drawing; using system.windows.form;
public class FolderBrowserDialogExampleForm: System.Windows.Forms.Form {private FolderBrowserDialog folderBrowserDialog1; private OpenFileDialog openFileDialog1; private RichTextBox richTextBox1;
Private Mainmenu MainMenu1; Private Menuitem; Private Menuitem; Private String OpenFileName, FoldernAMe;
PRIVATE BOOL FileOpened = False;
// The main entry point for the application. Static void main () {Application.run (New FolderBrowserDialoGexampleform ());}
// Constructor public FolderBrowserDialogExampleForm () {this.mainMenu1 = new System.Windows.Forms.MainMenu ();. This.fileMenuItem = new System.Windows.Forms.MenuItem (); this.openMenuItem = new System.Windows.Forms. Menuitem (); this.folderMenuItem = new system.windows.forms.MenuItem (); this.closeMenuItem = new system.windows.forms.MenuItem ();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog (); this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog (); this.richTextBox1 = new System.Windows.Forms.RichTextBox ();
this.mainMenu1.MenuItems.Add (this.fileMenuItem); this.fileMenuItem.MenuItems.AddRange (new System.Windows.Forms.MenuItem [] {this.openMenuItem, this.closeMenuItem, this.folderMenuItem}); this.fileMenuItem. Text = "file";
this.openMenuItem.Text = "Open ..."; this.openMenuItem.Click = new System.EventHandler (this.openMenuItem_Click); this.folderMenuItem.Text = "Select Directory ..."; this.folderMenuItem.Click = New System.EventHandler (this.folderMenuItem_click);
This.closeMenuItem.text = "close"; this.closeMenuItem.Click = new system.eventhandler (this.closeMenuItem_Click); this.closemenuitem.enabled = false;
This.openfiledialog1.defaultext = "RTF"; this.openfiledialog1.filter = "RTF files (* .RTF) | * .RTF";
// set the help text description for the folderbrowserdialog. This.folderbrowserdialog1.description = "Select the Directory That You Want to Use as the default."
// do not allow the user to create new files via the folderbrowserdialog. This.folderbrowserdialog1.shownewfolderbutton = false;
// default to the my documents folder. This.folderbrowserdialog1.rootfolder = Environment.SpecialFolder.Personal;
this.richTextBox1.AcceptsTab = true; this.richTextBox1.Location = new System.Drawing.Point (8, 8); this.richTextBox1.Size = new System.Drawing.Size (280, 344); this.richTextBox1.Anchor = Anchorstyles.top | Anchorstyles.Left | Anchorstyles.Right;
This.autoscalebasesize = new system.drawing.size (5, 13); this.clientsize = new system.drawing.size (296, 360); this.Controls.add (this.richtextbox1); this.Menu = this.mainum1 "This.Text =" RTF Document Browser ";}
// Bring up a dialog to open a file. Private void openMenuItem_Click (object sender, System.EventArgs e) {// If a file is not opened, then set the initial directory to the // FolderBrowserDialog.SelectedPath value. If (! fileOpened) {openFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedPath; openFileDialog1.FileName = null;}. // Display the openFile dialog DialogResult result = openFileDialog1.ShowDialog ();
// OK button was pressed if (result == DialogResult.OK) {openFileName = openFileDialog1.FileName;. Try {// Output the requested file in richTextBox1 Stream s = openFileDialog1.OpenFile ();. RichTextBox1.LoadFile (s, RichTextBoxStreamType .Richtext); s.close (); fileopened = true;
} Catch (Exception exp) {MessageBox.Show ( "An error occurred while attempting to load the file The error is:." System.Environment.NewLine exp.ToString () System.Environment.NewLine); fileOpened = false } Invalidate ();
CloseMenuItem.enabled = fileopes
// Cancel Button Was Pressed. Else IF (Result == DialogResult.cancel) {return;}}
// Close The Current File. Private Void CloseMenuItem_Click (Object Sender, System.EventArgs E) {RichtextBox1.text = ""; FileOpened = False;
CloseMenuitem.enabled = false;