Skip to content

Commit ea00fea

Browse files
authored
Modernize C# FolderBrowserDialog example
Update the C# example for the FolderBrowserDialog class to use MenuStrip, MenuStripItem, etc. classes instead of the deprecated MainMenu and MenuItem classes.
1 parent cbea657 commit ea00fea

File tree

1 file changed

+55
-46
lines changed
  • snippets/csharp/System.Windows.Forms/FolderBrowserDialog/Overview

1 file changed

+55
-46
lines changed
Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//<Snippet1>
21
// The following example displays an application that provides the ability to
32
// open rich text files (rtf) into the RichTextBox. The example demonstrates
43
// using the FolderBrowserDialog to set the default directory for opening files.
@@ -12,59 +11,64 @@ public class FolderBrowserDialogExampleForm : System.Windows.Forms.Form
1211
{
1312
private FolderBrowserDialog folderBrowserDialog1;
1413
private OpenFileDialog openFileDialog1;
15-
14+
1615
private RichTextBox richTextBox1;
1716

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+
2221
private string openFileName, folderName;
2322

2423
private bool fileOpened = false;
2524

2625
// The main entry point for the application.
2726
[STAThreadAttribute]
28-
static void Main()
27+
static void Main()
2928
{
3029
Application.Run(new FolderBrowserDialogExampleForm());
3130
}
3231

3332
// Constructor.
3433
public FolderBrowserDialogExampleForm()
3534
{
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+
5148
this.fileMenuItem.Text = "File";
49+
this.fileMenuItem.DropDownItems.AddRange(
50+
new ToolStripItem[] {
51+
this.openMenuItem,
52+
this.closeMenuItem,
53+
this.folderMenuItem
54+
}
55+
);
5256

5357
this.openMenuItem.Text = "Open...";
54-
this.openMenuItem.Click += new System.EventHandler(this.openMenuItem_Click);
58+
this.openMenuItem.Click += new EventHandler(this.openMenuItem_Click);
5559

5660
this.folderMenuItem.Text = "Select Directory...";
57-
this.folderMenuItem.Click += new System.EventHandler(this.folderMenuItem_Click);
61+
this.folderMenuItem.Click += new EventHandler(this.folderMenuItem_Click);
5862

5963
this.closeMenuItem.Text = "Close";
60-
this.closeMenuItem.Click += new System.EventHandler(this.closeMenuItem_Click);
64+
this.closeMenuItem.Click += new EventHandler(this.closeMenuItem_Click);
6165
this.closeMenuItem.Enabled = false;
6266

6367
this.openFileDialog1.DefaultExt = "rtf";
6468
this.openFileDialog1.Filter = "rtf files (*.rtf)|*.rtf";
6569

6670
// Set the help text description for the FolderBrowserDialog.
67-
this.folderBrowserDialog1.Description =
71+
this.folderBrowserDialog1.Description =
6872
"Select the directory that you want to use as the default.";
6973

7074
// Do not allow the user to create new files via the FolderBrowserDialog.
@@ -74,19 +78,24 @@ public FolderBrowserDialogExampleForm()
7478
this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Personal;
7579

7680
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+
);
8189

8290
this.ClientSize = new System.Drawing.Size(296, 360);
91+
this.Controls.Add(this.mainMenu1);
8392
this.Controls.Add(this.richTextBox1);
84-
this.Menu = this.mainMenu1;
93+
this.MainMenuStrip = this.mainMenu1;
8594
this.Text = "RTF Document Browser";
8695
}
8796

8897
// 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)
9099
{
91100
// If a file is not opened, then set the initial directory to the
92101
// FolderBrowserDialog.SelectedPath value.
@@ -99,21 +108,21 @@ private void openMenuItem_Click(object sender, System.EventArgs e)
99108
DialogResult result = openFileDialog1.ShowDialog();
100109

101110
// OK button was pressed.
102-
if(result == DialogResult.OK)
111+
if (result == DialogResult.OK)
103112
{
104113
openFileName = openFileDialog1.FileName;
105114
try
106115
{
107116
// Output the requested file in richTextBox1.
108117
Stream s = openFileDialog1.OpenFile();
109118
richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText);
110-
s.Close();
111-
119+
s.Close();
120+
112121
fileOpened = true;
113-
}
114-
catch(Exception exp)
122+
}
123+
catch (Exception exp)
115124
{
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:"
117126
+ System.Environment.NewLine + exp.ToString() + System.Environment.NewLine);
118127
fileOpened = false;
119128
}
@@ -123,37 +132,37 @@ private void openMenuItem_Click(object sender, System.EventArgs e)
123132
}
124133

125134
// Cancel button was pressed.
126-
else if(result == DialogResult.Cancel)
135+
else if (result == DialogResult.Cancel)
127136
{
128137
return;
129138
}
130139
}
131140

132141
// Close the current file.
133-
private void closeMenuItem_Click(object sender, System.EventArgs e)
142+
private void closeMenuItem_Click(object sender, EventArgs e)
134143
{
135144
richTextBox1.Text = "";
136145
fileOpened = false;
137146

138147
closeMenuItem.Enabled = false;
139148
}
140149

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)
143152
{
144153
// Show the FolderBrowserDialog.
145154
DialogResult result = folderBrowserDialog1.ShowDialog();
146-
if( result == DialogResult.OK )
155+
if (result == DialogResult.OK)
147156
{
148157
folderName = folderBrowserDialog1.SelectedPath;
149-
if(!fileOpened)
158+
if (!fileOpened)
150159
{
151160
// No file is opened, bring up openFileDialog in selected path.
152161
openFileDialog1.InitialDirectory = folderName;
153162
openFileDialog1.FileName = null;
154163
openMenuItem.PerformClick();
155-
}
164+
}
156165
}
157166
}
158167
}
159-
//</Snippet1>
168+
//</Snippet1>

0 commit comments

Comments
 (0)