Other Software > Developer's Corner
Tutorial: Visual C# For Beginners - Part 3 (With Pretty Pictures!!!)
(1/1)
KynloStephen66515:
Welcome To Part 3:
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:
--- Code: C# ---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); }Which should give you:
--- Code: C# ---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:
--- Code: C# ---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:
--- Code: C# ---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:
--- Code: C# ---printDialog1.Document = printDocument1; if (printDialog1.ShowDialog() == DialogResult.OK) { printDocument1.Print(); }
Which gives you:
--- Code: C# ---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:
--- Code: C# ---if (MessageBox.Show("Are you sure you want to quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK) { Application.Exit(); }
Which gives you the following:
--- Code: C# ---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!!!
Fuppiz:
Hello,
i was looking around to find some tutorials about c# and i saw your post. i read some of your tutorials. i understood mainly what what some stuff but i think you sould make explication. like what eatch part of the code mean and what is mainly hapening in the program. well its a good job you are doing here you gave me some ideas so tnx :)
KynloStephen66515:
Hello,
i was looking around to find some tutorials about c# and i saw your post. i read some of your tutorials. i understood mainly what what some stuff but i think you sould make explication. like what eatch part of the code mean and what is mainly hapening in the program. well its a good job you are doing here you gave me some ideas so tnx :)
-Fuppiz (June 05, 2012, 01:43 PM)
--- End quote ---
If you need any specific peices of code explaining, feel free to ask...
To be perfectly honest...I completely forgot about these lol, I have moved on to Delphi programming lately but am more than willing to continue helping people with this :)
Thanks for the good feedback :)
-Stephen
Navigation
[0] Message Index
Go to full version