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