Skip to content

Commit

Permalink
Modernize C# FolderBrowserDialog example
Browse files Browse the repository at this point in the history
Update the C# example for the FolderBrowserDialog class to use MenuStrip, MenuStripItem, etc. classes instead of the deprecated MainMenu and MenuItem classes.
  • Loading branch information
Varriount authored Feb 27, 2024
1 parent cbea657 commit ea00fea
Showing 1 changed file with 55 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//<Snippet1>
// The following example displays an application that provides the ability to

Check failure on line 1 in snippets/csharp/System.Windows.Forms/FolderBrowserDialog/Overview/form1.cs

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
// open rich text files (rtf) into the RichTextBox. The example demonstrates
// using the FolderBrowserDialog to set the default directory for opening files.
Expand All @@ -12,59 +11,64 @@ public class FolderBrowserDialogExampleForm : System.Windows.Forms.Form
{
private FolderBrowserDialog folderBrowserDialog1;
private OpenFileDialog openFileDialog1;

private RichTextBox richTextBox1;

private MainMenu mainMenu1;
private MenuItem fileMenuItem, openMenuItem;
private MenuItem folderMenuItem, closeMenuItem;
private MenuStrip mainMenu1;
private ToolStripMenuItem fileMenuItem, openMenuItem;
private ToolStripMenuItem folderMenuItem, closeMenuItem;

private string openFileName, folderName;

private bool fileOpened = false;

// The main entry point for the application.
[STAThreadAttribute]
static void Main()
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.mainMenu1 = new MenuStrip();

this.fileMenuItem = new ToolStripMenuItem();
this.openMenuItem = new ToolStripMenuItem();
this.folderMenuItem = new ToolStripMenuItem();
this.closeMenuItem = new ToolStripMenuItem();

this.openFileDialog1 = new OpenFileDialog();
this.folderBrowserDialog1 = new FolderBrowserDialog();
this.richTextBox1 = new RichTextBox();

this.mainMenu1.Items.Add(this.fileMenuItem);

this.fileMenuItem.Text = "File";
this.fileMenuItem.DropDownItems.AddRange(
new ToolStripItem[] {
this.openMenuItem,
this.closeMenuItem,
this.folderMenuItem
}
);

this.openMenuItem.Text = "Open...";
this.openMenuItem.Click += new System.EventHandler(this.openMenuItem_Click);
this.openMenuItem.Click += new EventHandler(this.openMenuItem_Click);

this.folderMenuItem.Text = "Select Directory...";
this.folderMenuItem.Click += new System.EventHandler(this.folderMenuItem_Click);
this.folderMenuItem.Click += new EventHandler(this.folderMenuItem_Click);

this.closeMenuItem.Text = "Close";
this.closeMenuItem.Click += new System.EventHandler(this.closeMenuItem_Click);
this.closeMenuItem.Click += new 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 =
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.
Expand All @@ -74,19 +78,24 @@ public FolderBrowserDialogExampleForm()
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.Bottom | AnchorStyles.Right;
this.richTextBox1.Location = new System.Drawing.Point(8, 40);
this.richTextBox1.Size = new System.Drawing.Size(280, 312);
this.richTextBox1.Anchor = (
AnchorStyles.Top |
AnchorStyles.Left |
AnchorStyles.Bottom |
AnchorStyles.Right
);

this.ClientSize = new System.Drawing.Size(296, 360);
this.Controls.Add(this.mainMenu1);
this.Controls.Add(this.richTextBox1);
this.Menu = this.mainMenu1;
this.MainMenuStrip = this.mainMenu1;
this.Text = "RTF Document Browser";
}

// Bring up a dialog to open a file.
private void openMenuItem_Click(object sender, System.EventArgs e)
private void openMenuItem_Click(object sender, EventArgs e)
{
// If a file is not opened, then set the initial directory to the
// FolderBrowserDialog.SelectedPath value.
Expand All @@ -99,21 +108,21 @@ private void openMenuItem_Click(object sender, System.EventArgs e)
DialogResult result = openFileDialog1.ShowDialog();

// OK button was pressed.
if(result == DialogResult.OK)
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();
s.Close();

fileOpened = true;
}
catch(Exception exp)
}
catch (Exception exp)
{
MessageBox.Show("An error occurred while attempting to load the file. The error is:"
MessageBox.Show("An error occurred while attempting to load the file. The error is:"
+ System.Environment.NewLine + exp.ToString() + System.Environment.NewLine);
fileOpened = false;
}
Expand All @@ -123,37 +132,37 @@ private void openMenuItem_Click(object sender, System.EventArgs e)
}

// Cancel button was pressed.
else if(result == DialogResult.Cancel)
else if (result == DialogResult.Cancel)
{
return;
}
}

// Close the current file.
private void closeMenuItem_Click(object sender, System.EventArgs e)
private void closeMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
fileOpened = false;

closeMenuItem.Enabled = false;
}

// Bring up a dialog to chose a folder path in which to open or save a file.
private void folderMenuItem_Click(object sender, System.EventArgs e)
// Bring up a dialog to choose a folder path in which to open or save a file.
private void folderMenuItem_Click(object sender, EventArgs e)
{
// Show the FolderBrowserDialog.
DialogResult result = folderBrowserDialog1.ShowDialog();
if( result == DialogResult.OK )
if (result == DialogResult.OK)
{
folderName = folderBrowserDialog1.SelectedPath;
if(!fileOpened)
if (!fileOpened)
{
// No file is opened, bring up openFileDialog in selected path.
openFileDialog1.InitialDirectory = folderName;
openFileDialog1.FileName = null;
openMenuItem.PerformClick();
}
}
}
}
}
//</Snippet1>
//</Snippet1>

0 comments on commit ea00fea

Please sign in to comment.