1
- //<Snippet1>
1
+ //<Snippet1>
2
2
// The following example displays an application that provides the ability to
3
3
// open rich text files (rtf) into the RichTextBox. The example demonstrates
4
4
// using the FolderBrowserDialog to set the default directory for opening files.
@@ -12,59 +12,64 @@ public class FolderBrowserDialogExampleForm : System.Windows.Forms.Form
12
12
{
13
13
private FolderBrowserDialog folderBrowserDialog1 ;
14
14
private OpenFileDialog openFileDialog1 ;
15
-
15
+
16
16
private RichTextBox richTextBox1 ;
17
17
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
+
22
22
private string openFileName , folderName ;
23
23
24
24
private bool fileOpened = false ;
25
25
26
26
// The main entry point for the application.
27
27
[ STAThreadAttribute ]
28
- static void Main ( )
28
+ static void Main ( )
29
29
{
30
30
Application . Run ( new FolderBrowserDialogExampleForm ( ) ) ;
31
31
}
32
32
33
33
// Constructor.
34
34
public FolderBrowserDialogExampleForm ( )
35
35
{
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
+
51
49
this . fileMenuItem . Text = "File" ;
50
+ this . fileMenuItem . DropDownItems . AddRange (
51
+ new ToolStripItem [ ] {
52
+ this . openMenuItem ,
53
+ this . closeMenuItem ,
54
+ this . folderMenuItem
55
+ }
56
+ ) ;
52
57
53
58
this . openMenuItem . Text = "Open..." ;
54
- this . openMenuItem . Click += new System . EventHandler ( this . openMenuItem_Click ) ;
59
+ this . openMenuItem . Click += new EventHandler ( this . openMenuItem_Click ) ;
55
60
56
61
this . folderMenuItem . Text = "Select Directory..." ;
57
- this . folderMenuItem . Click += new System . EventHandler ( this . folderMenuItem_Click ) ;
62
+ this . folderMenuItem . Click += new EventHandler ( this . folderMenuItem_Click ) ;
58
63
59
64
this . closeMenuItem . Text = "Close" ;
60
- this . closeMenuItem . Click += new System . EventHandler ( this . closeMenuItem_Click ) ;
65
+ this . closeMenuItem . Click += new EventHandler ( this . closeMenuItem_Click ) ;
61
66
this . closeMenuItem . Enabled = false ;
62
67
63
68
this . openFileDialog1 . DefaultExt = "rtf" ;
64
69
this . openFileDialog1 . Filter = "rtf files (*.rtf)|*.rtf" ;
65
70
66
71
// Set the help text description for the FolderBrowserDialog.
67
- this . folderBrowserDialog1 . Description =
72
+ this . folderBrowserDialog1 . Description =
68
73
"Select the directory that you want to use as the default." ;
69
74
70
75
// Do not allow the user to create new files via the FolderBrowserDialog.
@@ -74,19 +79,24 @@ public FolderBrowserDialogExampleForm()
74
79
this . folderBrowserDialog1 . RootFolder = Environment . SpecialFolder . Personal ;
75
80
76
81
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
+ ) ;
81
90
82
91
this . ClientSize = new System . Drawing . Size ( 296 , 360 ) ;
92
+ this . Controls . Add ( this . mainMenu1 ) ;
83
93
this . Controls . Add ( this . richTextBox1 ) ;
84
- this . Menu = this . mainMenu1 ;
94
+ this . MainMenuStrip = this . mainMenu1 ;
85
95
this . Text = "RTF Document Browser" ;
86
96
}
87
97
88
98
// 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 )
90
100
{
91
101
// If a file is not opened, then set the initial directory to the
92
102
// FolderBrowserDialog.SelectedPath value.
@@ -99,21 +109,21 @@ private void openMenuItem_Click(object sender, System.EventArgs e)
99
109
DialogResult result = openFileDialog1 . ShowDialog ( ) ;
100
110
101
111
// OK button was pressed.
102
- if ( result == DialogResult . OK )
112
+ if ( result == DialogResult . OK )
103
113
{
104
114
openFileName = openFileDialog1 . FileName ;
105
115
try
106
116
{
107
117
// Output the requested file in richTextBox1.
108
118
Stream s = openFileDialog1 . OpenFile ( ) ;
109
119
richTextBox1 . LoadFile ( s , RichTextBoxStreamType . RichText ) ;
110
- s . Close ( ) ;
111
-
120
+ s . Close ( ) ;
121
+
112
122
fileOpened = true ;
113
- }
114
- catch ( Exception exp )
123
+ }
124
+ catch ( Exception exp )
115
125
{
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:"
117
127
+ System . Environment . NewLine + exp . ToString ( ) + System . Environment . NewLine ) ;
118
128
fileOpened = false ;
119
129
}
@@ -123,37 +133,37 @@ private void openMenuItem_Click(object sender, System.EventArgs e)
123
133
}
124
134
125
135
// Cancel button was pressed.
126
- else if ( result == DialogResult . Cancel )
136
+ else if ( result == DialogResult . Cancel )
127
137
{
128
138
return ;
129
139
}
130
140
}
131
141
132
142
// Close the current file.
133
- private void closeMenuItem_Click ( object sender , System . EventArgs e )
143
+ private void closeMenuItem_Click ( object sender , EventArgs e )
134
144
{
135
145
richTextBox1 . Text = "" ;
136
146
fileOpened = false ;
137
147
138
148
closeMenuItem . Enabled = false ;
139
149
}
140
150
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 )
143
153
{
144
154
// Show the FolderBrowserDialog.
145
155
DialogResult result = folderBrowserDialog1 . ShowDialog ( ) ;
146
- if ( result == DialogResult . OK )
156
+ if ( result == DialogResult . OK )
147
157
{
148
158
folderName = folderBrowserDialog1 . SelectedPath ;
149
- if ( ! fileOpened )
159
+ if ( ! fileOpened )
150
160
{
151
161
// No file is opened, bring up openFileDialog in selected path.
152
162
openFileDialog1 . InitialDirectory = folderName ;
153
163
openFileDialog1 . FileName = null ;
154
164
openMenuItem . PerformClick ( ) ;
155
- }
165
+ }
156
166
}
157
167
}
158
168
}
159
- //</Snippet1>
169
+ //</Snippet1>
0 commit comments