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
  • December 04, 2016, 12:16:36 AM
  • 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 2 (With Pretty Pictures!!!)  (Read 3467 times)

Stephen66515

  • Animated Giffer in Chief
  • Honorary Member
  • Joined in 2010
  • **
  • Posts: 3,129
    • View Profile
    • Donate to Member
Welcome Back!

Can't be bothered with long winded introductions so lets get cracking:

Recap:

In the last lesson we learned the following:

  • Where to Download Visual C# 2010 Express
  • How to setup your workspace
  • How to add MenuStrip with all the standard controls
  • How to add a RichTextBox and dock it to fit the entire screen
  • How to test your application

In this lesson we will learn the following:


  • Coding some of the File Menu Functions
1.png
  • Coding some of the Edit Menu Functions
2.png
  • Change the name of your Main Form
3.png


Firstly, lets deal with the easy part - Which is changing the name of your form.

To get the required property list up, you will need to click at the top of your form:

4.png

Now you need to look at the bottom right of your workspace and find the following screen.  Change the text where I have typed "Change Me"

5.png

OK, now thats done, lets get some coding done!

What you now need to do is click "File" (On your form - not on Visual C#) and double click "New"

1.png

Wow, the screen changed and now theres confusing text everywhere!

6.pngTutorial: Visual C# For Beginners - Part 2 (With Pretty Pictures!!!)

Don't Panic and Take a Deep Breath!

Your cursor should have been automatically placed where it needs to be:

7.pngTutorial: Visual C# For Beginners - Part 2 (With Pretty Pictures!!!)

Now you need to type the following code into the area your cursor is:

richTextBox1.Clear();

Which should now look like this:

8.png

Now you have done that you can click the play button (which we learned in the previous tutorial).  Type some stuff into the notepad window when it opens, then try your brand spanking new "New" button.

Awesome! you can now type stuff, and delete it all...not much use but we are getting somewhere!

Right, now back to coding! - Close the application you have made and go back to the Visual C# Window.

We are still on the coding window and need to go back to the visual designer, to do this you simply click on the Form1.cs [Design] button at the top:

9.png

Right, now we need the application to quit when you tell it to, without having to click the X in the top right.

Click "File" and double click "Exit"

Same as before, your cursor will have been placed in the correct position:

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            | <--- There
        }


Simply add:

Application.Exit();

So we have:

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


Brilliant, now we have working "New" and "Exit" buttons

In the next tutorial we will learn how to code the rest of the file menu.  I am not covering those in this one because they require a little more adanced coding and a few extra things adding...so lets not get into that now :)

Lets get onto the "Edit" Menu now!

Same as before, go back to the visual designer, but this time you need to click "Edit"  then double click "Undo"

       private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            |
        }


Add:

richTextBox1.Undo();

You should now have:

       private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
           richTextBox1.Undo();
        }


Back to the designer, click "Edit" then "Redo":

       private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
             |
        }


Add This Again (Ill explain why later):

richTextBox1.Undo();

And now we have:

       private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Undo();
        }


Go back to the designer click "Edit" and double click "Cut"

Add:

richTextBox1.Cut();

Go back to the designer click "Edit" and double click "Copy"

Add:

richTextBox1.Copy();

Go back to the designer click "Edit" and double click "Paste"

Add:

richTextBox1.Paste();

You should now have:

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
           richTextBox1.Copy();
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }


Now go and click "Edit" then double click "Select All"

Add:

richTextBox1.SelectAll();

Which should now be:

       private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
        }


OK, now in your coding window you should have, in total, the following code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Notepad_Application
{
   public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
           richTextBox1.Undo();
        }

        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
           richTextBox1.Undo();
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
           richTextBox1.Copy();
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
        }
        
    }
}


Awesome! - Now you want to save your project and go and play with it!



No homework this time except to try do all this without looking at the tutorial!

Have fun and Part 3 will be done within the next 24 hours!

« Last Edit: May 28, 2011, 08:15:49 PM by Stephen66515 »