Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • September 04, 2015, 04:06:35 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Tutorial: Visual C# For Beginners - Part 3 (With Pretty Pictures!!!)  (Read 2947 times)

Stephen66515

  • Animated Giffer in Chief
  • Honorary Member
  • Joined in 2010
  • **
  • Posts: 3,004
    • View Profile
    • Donate to Member
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
1.png
2.png

To add these to your project, click and drag them onto the bottom menu bar

3.png

Now you have added them all you will have something that looks like this:

5.png

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#
  1. string OFile = "";
  2.  
  3.             openFileDialog1.InitialDirectory = "C:";
  4.             openFileDialog1.Title = "Open a Text File";
  5.             openFileDialog1.FileName = "";
  6.             openFileDialog1.Filter = "Text Files|*.txt|Word Documents|*.doc";
  7.             if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
  8.             {
  9.                 OFile = openFileDialog1.FileName;
  10.                 richTextBox1.LoadFile(OFile, RichTextBoxStreamType.PlainText);
  11.             }
  12.  
  13.  
Which should give you:

 
Code: C#
  1. private void openToolStripMenuItem_Click(object sender, EventArgs e)
  2.         {
  3.             string OFile = "";
  4.  
  5.             openFileDialog1.InitialDirectory = "C:";
  6.             openFileDialog1.Title = "Open a Text File";
  7.             openFileDialog1.FileName = "";
  8.             openFileDialog1.Filter = "Text Files|*.txt|Word Documents|*.doc";
  9.             if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
  10.             {
  11.                 OFile = openFileDialog1.FileName;
  12.                 richTextBox1.LoadFile(OFile, RichTextBoxStreamType.PlainText);
  13.             }
  14.         }
  15.  

Now you want to go back and double click "Save"

Add:

Code: C#
  1.  string SFile = "";
  2.  
  3.             openFileDialog1.InitialDirectory = "C:";
  4.             openFileDialog1.Title = "Save Your Notes!";
  5.             openFileDialog1.FileName = "";
  6.             openFileDialog1.Filter = "Text Files|*.txt|Word Documents|*.doc";
  7.             if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
  8.             {
  9.                 SFile = saveFileDialog1.FileName;
  10.                 richTextBox1.SaveFile(SFile, RichTextBoxStreamType.PlainText);
  11.             }
  12.  

Which gives you:

Code: C#
  1. private void saveToolStripMenuItem_Click(object sender, EventArgs e)
  2.         {
  3.             string SFile = "";
  4.  
  5.             openFileDialog1.InitialDirectory = "C:";
  6.             openFileDialog1.Title = "Save Your Notes!";
  7.             openFileDialog1.FileName = "";
  8.             openFileDialog1.Filter = "Text Files|*.txt|Word Documents|*.doc";
  9.             if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
  10.             {
  11.                 SFile = saveFileDialog1.FileName;
  12.                 richTextBox1.SaveFile(SFile, RichTextBoxStreamType.PlainText);
  13.             }
  14.         }
  15.  

Now for the "Print" option:

Add:

Code: C#
  1.  printDialog1.Document = printDocument1;
  2.             if (printDialog1.ShowDialog() == DialogResult.OK)
  3.             {
  4.                 printDocument1.Print();
  5.             }
  6.  

Which gives you:

Code: C#
  1.         private void printToolStripMenuItem_Click(object sender, EventArgs e)
  2.         {
  3.             printDialog1.Document = printDocument1;
  4.             if (printDialog1.ShowDialog() == DialogResult.OK)
  5.             {
  6.                 printDocument1.Print();
  7.             }
  8.         }
  9.  


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#
  1.             if (MessageBox.Show("Are you sure you want to quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
  2.             {
  3.                 Application.Exit();
  4.             }
  5.  

Which gives you the following:

Code: C#
  1.         private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  2.         {
  3.             if (MessageBox.Show("Are you sure you want to quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
  4.             {
  5.                 Application.Exit();
  6.             }
  7.         }
  8.  

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!!!
« Last Edit: May 29, 2011, 10:48:01 AM by Stephen66515 »

Fuppiz

  • Participant
  • Joined in 2012
  • *
  • default avatar
  • Posts: 1
    • View Profile
    • Donate to Member
Re: Tutorial: Visual C# For Beginners - Part 3 (With Pretty Pictures!!!)
« Reply #1 on: June 05, 2012, 01:43:02 PM »
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 :)

Stephen66515

  • Animated Giffer in Chief
  • Honorary Member
  • Joined in 2010
  • **
  • Posts: 3,004
    • View Profile
    • Donate to Member
Re: Tutorial: Visual C# For Beginners - Part 3 (With Pretty Pictures!!!)
« Reply #2 on: June 05, 2012, 01:53:55 PM »
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 :)

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