I'm sure that if you have completed Parts 1 & 2 then you are keen to continue with your project, so lets get cracking with that File Menu!
For this, you are going to need to add the following items:
- openFileDialog
- saveFileDialog
- printDialog
- printDocument
To add these to your project, click and drag them onto the bottom menu bar
Now you have added them all you will have something that looks like this:
Brilliant!
Now...Remembering what you learned in Part 2 you need to go ahead and click the "File" button and then double click the "Open" button
Add:
Which should give you:Formatted for C# with the GeSHI Syntax Highlighter [copy or print]
string OFile = ""; openFileDialog1.InitialDirectory = "C:"; openFileDialog1.Title = "Open a Text File"; openFileDialog1.FileName = ""; openFileDialog1.Filter = "Text Files|*.txt|Word Documents|*.doc"; if (openFileDialog1.ShowDialog() != DialogResult.Cancel) { OFile = openFileDialog1.FileName; richTextBox1.LoadFile(OFile, RichTextBoxStreamType.PlainText); }
Formatted for C# with the GeSHI Syntax Highlighter [copy or print]
private void openToolStripMenuItem_Click(object sender, EventArgs e) { string OFile = ""; openFileDialog1.InitialDirectory = "C:"; openFileDialog1.Title = "Open a Text File"; openFileDialog1.FileName = ""; openFileDialog1.Filter = "Text Files|*.txt|Word Documents|*.doc"; if (openFileDialog1.ShowDialog() != DialogResult.Cancel) { OFile = openFileDialog1.FileName; richTextBox1.LoadFile(OFile, RichTextBoxStreamType.PlainText); } }
Now you want to go back and double click "Save"
Add:
Formatted for C# with the GeSHI Syntax Highlighter [copy or print]
string SFile = ""; openFileDialog1.InitialDirectory = "C:"; openFileDialog1.Title = "Save Your Notes!"; openFileDialog1.FileName = ""; openFileDialog1.Filter = "Text Files|*.txt|Word Documents|*.doc"; if (openFileDialog1.ShowDialog() != DialogResult.Cancel) { SFile = saveFileDialog1.FileName; richTextBox1.SaveFile(SFile, RichTextBoxStreamType.PlainText); }
Which gives you:
Formatted for C# with the GeSHI Syntax Highlighter [copy or print]
private void saveToolStripMenuItem_Click(object sender, EventArgs e) { string SFile = ""; openFileDialog1.InitialDirectory = "C:"; openFileDialog1.Title = "Save Your Notes!"; openFileDialog1.FileName = ""; openFileDialog1.Filter = "Text Files|*.txt|Word Documents|*.doc"; if (openFileDialog1.ShowDialog() != DialogResult.Cancel) { SFile = saveFileDialog1.FileName; richTextBox1.SaveFile(SFile, RichTextBoxStreamType.PlainText); } }
Now for the "Print" option:
Add:
Formatted for C# with the GeSHI Syntax Highlighter [copy or print]
printDialog1.Document = printDocument1; if (printDialog1.ShowDialog() == DialogResult.OK) { printDocument1.Print(); }
Which gives you:
Formatted for C# with the GeSHI Syntax Highlighter [copy or print]
private void printToolStripMenuItem_Click(object sender, EventArgs e) { printDialog1.Document = printDocument1; if (printDialog1.ShowDialog() == DialogResult.OK) { printDocument1.Print(); } }
Now we are going to extend our "Exit" button. Go to the designer, click "File", double click "Exit" and delete 'Application.Exit();'
Add the following code in its place:
Formatted for C# with the GeSHI Syntax Highlighter [copy or print]
if (MessageBox.Show("Are you sure you want to quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK) { Application.Exit(); }
Which gives you the following:
Formatted for C# with the GeSHI Syntax Highlighter [copy or print]
private void exitToolStripMenuItem_Click(object sender, EventArgs e) { if (MessageBox.Show("Are you sure you want to quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK) { Application.Exit(); } }
Awesome!!!!!
Save your project and go play with it! You can even run a print from it if you own a printer!!!
Thats all for this tutorial.
In the next we will add code for "Save As" and "Print Preview".
We will also make a start with our Tools Menu! Even adding things like Fonts and Colours!!!













Logged

