1- //<Snippet1>
1+ //<Snippet1>
22// The following example displays an application that provides the ability to
33// open rich text files (rtf) into the RichTextBox. The example demonstrates
44// using the FolderBrowserDialog to set the default directory for opening files.
@@ -12,59 +12,64 @@ public class FolderBrowserDialogExampleForm : System.Windows.Forms.Form
1212{
1313 private FolderBrowserDialog folderBrowserDialog1 ;
1414 private OpenFileDialog openFileDialog1 ;
15-
15+
1616 private RichTextBox richTextBox1 ;
1717
18- private MainMenu mainMenu1 ;
19- private MenuItem fileMenuItem , openMenuItem ;
20- private MenuItem folderMenuItem , closeMenuItem ;
21-
18+ private MenuStrip mainMenu1 ;
19+ private ToolStripMenuItem fileMenuItem , openMenuItem ;
20+ private ToolStripMenuItem folderMenuItem , closeMenuItem ;
21+
2222 private string openFileName , folderName ;
2323
2424 private bool fileOpened = false ;
2525
2626 // The main entry point for the application.
2727 [ STAThreadAttribute ]
28- static void Main ( )
28+ static void Main ( )
2929 {
3030 Application . Run ( new FolderBrowserDialogExampleForm ( ) ) ;
3131 }
3232
3333 // Constructor.
3434 public FolderBrowserDialogExampleForm ( )
3535 {
36- this . mainMenu1 = new System . Windows . Forms . MainMenu ( ) ;
37- this . fileMenuItem = new System . Windows . Forms . MenuItem ( ) ;
38- this . openMenuItem = new System . Windows . Forms . MenuItem ( ) ;
39- this . folderMenuItem = new System . Windows . Forms . MenuItem ( ) ;
40- this . closeMenuItem = new System . Windows . Forms . MenuItem ( ) ;
41-
42- this . openFileDialog1 = new System . Windows . Forms . OpenFileDialog ( ) ;
43- this . folderBrowserDialog1 = new System . Windows . Forms . FolderBrowserDialog ( ) ;
44- this . richTextBox1 = new System . Windows . Forms . RichTextBox ( ) ;
45-
46- this . mainMenu1 . MenuItems . Add ( this . fileMenuItem ) ;
47- this . fileMenuItem . MenuItems . AddRange (
48- new System . Windows . Forms . MenuItem [ ] { this . openMenuItem ,
49- this . closeMenuItem ,
50- this . folderMenuItem } ) ;
36+ this . mainMenu1 = new MenuStrip ( ) ;
37+
38+ this . fileMenuItem = new ToolStripMenuItem ( ) ;
39+ this . openMenuItem = new ToolStripMenuItem ( ) ;
40+ this . folderMenuItem = new ToolStripMenuItem ( ) ;
41+ this . closeMenuItem = new ToolStripMenuItem ( ) ;
42+
43+ this . openFileDialog1 = new OpenFileDialog ( ) ;
44+ this . folderBrowserDialog1 = new FolderBrowserDialog ( ) ;
45+ this . richTextBox1 = new RichTextBox ( ) ;
46+
47+ this . mainMenu1 . Items . Add ( this . fileMenuItem ) ;
48+
5149 this . fileMenuItem . Text = "File" ;
50+ this . fileMenuItem . DropDownItems . AddRange (
51+ new ToolStripItem [ ] {
52+ this . openMenuItem ,
53+ this . closeMenuItem ,
54+ this . folderMenuItem
55+ }
56+ ) ;
5257
5358 this . openMenuItem . Text = "Open..." ;
54- this . openMenuItem . Click += new System . EventHandler ( this . openMenuItem_Click ) ;
59+ this . openMenuItem . Click += new EventHandler ( this . openMenuItem_Click ) ;
5560
5661 this . folderMenuItem . Text = "Select Directory..." ;
57- this . folderMenuItem . Click += new System . EventHandler ( this . folderMenuItem_Click ) ;
62+ this . folderMenuItem . Click += new EventHandler ( this . folderMenuItem_Click ) ;
5863
5964 this . closeMenuItem . Text = "Close" ;
60- this . closeMenuItem . Click += new System . EventHandler ( this . closeMenuItem_Click ) ;
65+ this . closeMenuItem . Click += new EventHandler ( this . closeMenuItem_Click ) ;
6166 this . closeMenuItem . Enabled = false ;
6267
6368 this . openFileDialog1 . DefaultExt = "rtf" ;
6469 this . openFileDialog1 . Filter = "rtf files (*.rtf)|*.rtf" ;
6570
6671 // Set the help text description for the FolderBrowserDialog.
67- this . folderBrowserDialog1 . Description =
72+ this . folderBrowserDialog1 . Description =
6873 "Select the directory that you want to use as the default." ;
6974
7075 // Do not allow the user to create new files via the FolderBrowserDialog.
@@ -74,19 +79,24 @@ public FolderBrowserDialogExampleForm()
7479 this . folderBrowserDialog1 . RootFolder = Environment . SpecialFolder . Personal ;
7580
7681 this . richTextBox1 . AcceptsTab = true ;
77- this . richTextBox1 . Location = new System . Drawing . Point ( 8 , 8 ) ;
78- this . richTextBox1 . Size = new System . Drawing . Size ( 280 , 344 ) ;
79- this . richTextBox1 . Anchor = AnchorStyles . Top | AnchorStyles . Left |
80- AnchorStyles . Bottom | AnchorStyles . Right ;
82+ this . richTextBox1 . Location = new System . Drawing . Point ( 8 , 40 ) ;
83+ this . richTextBox1 . Size = new System . Drawing . Size ( 280 , 312 ) ;
84+ this . richTextBox1 . Anchor = (
85+ AnchorStyles . Top |
86+ AnchorStyles . Left |
87+ AnchorStyles . Bottom |
88+ AnchorStyles . Right
89+ ) ;
8190
8291 this . ClientSize = new System . Drawing . Size ( 296 , 360 ) ;
92+ this . Controls . Add ( this . mainMenu1 ) ;
8393 this . Controls . Add ( this . richTextBox1 ) ;
84- this . Menu = this . mainMenu1 ;
94+ this . MainMenuStrip = this . mainMenu1 ;
8595 this . Text = "RTF Document Browser" ;
8696 }
8797
8898 // Bring up a dialog to open a file.
89- private void openMenuItem_Click ( object sender , System . EventArgs e )
99+ private void openMenuItem_Click ( object sender , EventArgs e )
90100 {
91101 // If a file is not opened, then set the initial directory to the
92102 // FolderBrowserDialog.SelectedPath value.
@@ -99,21 +109,21 @@ private void openMenuItem_Click(object sender, System.EventArgs e)
99109 DialogResult result = openFileDialog1 . ShowDialog ( ) ;
100110
101111 // OK button was pressed.
102- if ( result == DialogResult . OK )
112+ if ( result == DialogResult . OK )
103113 {
104114 openFileName = openFileDialog1 . FileName ;
105115 try
106116 {
107117 // Output the requested file in richTextBox1.
108118 Stream s = openFileDialog1 . OpenFile ( ) ;
109119 richTextBox1 . LoadFile ( s , RichTextBoxStreamType . RichText ) ;
110- s . Close ( ) ;
111-
120+ s . Close ( ) ;
121+
112122 fileOpened = true ;
113- }
114- catch ( Exception exp )
123+ }
124+ catch ( Exception exp )
115125 {
116- MessageBox . Show ( "An error occurred while attempting to load the file. The error is:"
126+ MessageBox . Show ( "An error occurred while attempting to load the file. The error is:"
117127 + System . Environment . NewLine + exp . ToString ( ) + System . Environment . NewLine ) ;
118128 fileOpened = false ;
119129 }
@@ -123,37 +133,37 @@ private void openMenuItem_Click(object sender, System.EventArgs e)
123133 }
124134
125135 // Cancel button was pressed.
126- else if ( result == DialogResult . Cancel )
136+ else if ( result == DialogResult . Cancel )
127137 {
128138 return ;
129139 }
130140 }
131141
132142 // Close the current file.
133- private void closeMenuItem_Click ( object sender , System . EventArgs e )
143+ private void closeMenuItem_Click ( object sender , EventArgs e )
134144 {
135145 richTextBox1 . Text = "" ;
136146 fileOpened = false ;
137147
138148 closeMenuItem . Enabled = false ;
139149 }
140150
141- // Bring up a dialog to chose a folder path in which to open or save a file.
142- private void folderMenuItem_Click ( object sender , System . EventArgs e )
151+ // Bring up a dialog to choose a folder path in which to open or save a file.
152+ private void folderMenuItem_Click ( object sender , EventArgs e )
143153 {
144154 // Show the FolderBrowserDialog.
145155 DialogResult result = folderBrowserDialog1 . ShowDialog ( ) ;
146- if ( result == DialogResult . OK )
156+ if ( result == DialogResult . OK )
147157 {
148158 folderName = folderBrowserDialog1 . SelectedPath ;
149- if ( ! fileOpened )
159+ if ( ! fileOpened )
150160 {
151161 // No file is opened, bring up openFileDialog in selected path.
152162 openFileDialog1 . InitialDirectory = folderName ;
153163 openFileDialog1 . FileName = null ;
154164 openMenuItem . PerformClick ( ) ;
155- }
165+ }
156166 }
157167 }
158168}
159- //</Snippet1>
169+ //</Snippet1>
0 commit comments