Simple Notepad
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| Exam 70-526 Preparation Guide: Create and configure menus. |
To best see the ToolStrip in action, let us create a simple notepad application.
- Create a new project
- Add a
ToolStripto a Form

- Insert Standard Items by clicking on the Smart Tag icon


- Change the
RenderModeto System

- Create event handlers for all of the buttons on the
ToolStripby double-clicking on them.
private void newToolStripButton_Click(object sender, EventArgs e) { } private void openToolStripButton_Click(object sender, EventArgs e) { } private void saveToolStripButton_Click(object sender, EventArgs e) { } private void printToolStripButton_Click(object sender, EventArgs e) { } private void cutToolStripButton_Click(object sender, EventArgs e) { } private void copyToolStripButton_Click(object sender, EventArgs e) { } private void pasteToolStripButton_Click(object sender, EventArgs e) { }
- Change the Form to an MDI Form

- Modify the code of the form to the following
int count; Form mdiChild; TextBox editTextBox; public Form1() { InitializeComponent(); count = 1; }
- Edit the
newToolStripButton_Clickmethod with the following code:
private void newToolStripButton_Click(object sender, EventArgs e) { mdiChild = new Form(); mdiChild.Text = "Document" + count.ToString(); mdiChild.MdiParent = this; editTextBox = new TextBox(); editTextBox.Multiline = true; editTextBox.Dock = DockStyle.Fill; mdiChild.Controls.Add(editTextBox); mdiChild.Show(); count++; }
- Test the new method

- Edit the
cutToolStripButton_Clickmethod with the following code:
private void cutToolStripButton_Click(object sender, EventArgs e) { Form activeChildForm = this.ActiveMdiChild; if (activeChildForm != null) { TextBox activeTextBox = (TextBox)activeChildForm.ActiveControl; if (activeTextBox != null) activeTextBox.Cut(); } }
- Edit the
copyToolStripButton_Clickmethod with the following code:
private void copyToolStripButton_Click(object sender, EventArgs e) { Form activeChildForm = this.ActiveMdiChild; if (activeChildForm != null) { TextBox activeTextBox = (TextBox)activeChildForm.ActiveControl; if (activeTextBox != null) activeTextBox.Copy(); } }
- Edit the
pasteToolStripButton_Clickmethod with the following code:
private void pasteToolStripButton_Click(object sender, EventArgs e) { Form activeChildForm = this.ActiveMdiChild; if (activeChildForm != null) { TextBox activeTextBox = (TextBox)activeChildForm.ActiveControl; if (activeTextBox != null) activeTextBox.Paste(); } }
- Test the Cut, Copy and Paste methods

- Edit the
saveToolStripButton_Clickmethod with the following code:
private void saveToolStripButton_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Title = "Save a Text File"; sfd.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*"; DialogResult dr = sfd.ShowDialog(); if (dr == DialogResult.OK) { System.IO.StreamWriter sw = new System.IO.StreamWriter(sfd.FileName); Form activeChildForm = this.ActiveMdiChild; if (activeChildForm != null) { TextBox activeTextBox = (TextBox)activeChildForm.ActiveControl; if (activeTextBox != null) sw.Write(activeTextBox.Text); sw.Close(); } } }
- Edit the
openToolStripButton_Clickmethod with the following code:
private void openToolStripButton_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "Open a Text File"; ofd.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*"; DialogResult dr = ofd.ShowDialog(); if (dr == DialogResult.OK) { System.IO.StreamReader sr = new System.IO.StreamReader(ofd.FileName); Form activeChildForm = this.ActiveMdiChild; if (activeChildForm != null) { TextBox activeTextBox = (TextBox)activeChildForm.ActiveControl; if (activeTextBox != null) activeTextBox.Text = sr.ReadToEnd(); sr.Close(); } } }
- Edit the
printToolStripButton_Clickmethod with the following code:
/// <summary> /// This displays the Print dialog only, it does not print the document /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void printToolStripButton_Click(object sender, EventArgs e) { PrintDialog pd = new PrintDialog(); pd.ShowDialog(); }
- We can add more controls to the ToolStrip such as:
- Button
- Label
- SplitButton
- DropDownButton
- Separator
- ComboBox
- TextBox
- ProgressBar

Download the complete sample application (written in C# Express).
In Simple Notepad - Part 2 we will add the printing capabilites.