Very Easy Program for Beginners
Displays your first, middle, and last name in a message box and allows you to change the color and font of all the labels and buttons.
Introduction
This program has a few features to help you create a simple program.
Using the Code
The features included are: changing the font and color of all the labels and buttons, adding an About Box, using the MenuStrip control, linking click events, and entering text into a text box and having it display in a message box.
- Open Visual Studio
- Click New Project:
- Select Windows Form Application from the list
- In the Name text box, change the name to
EasyProject
- Next to the Location text box, click browse and save your work in whatever folder you want, for this one I am just working off the Visual Studio project folder in My Documents.
- Click OK
- Right click the form and select Properties from the list:
- In the Properties Pane, scroll down to Text property
- Replace the words Form1 with Easy Project and hit enter
- If your Toolbox is not on the left side of the screen, click View in the menu bar and then select Toolbox:
- In the Toolbox pane, scroll down to Menus & Toolbars and drag a MenuStrip control to your form and let go:
- Click in the white text box that says Type Here
- When you see an insertion point, type File
- Click the white text box to the right of file and type Edit
- Click in the white text box to the right of edit and type Help
- Click File again and a white text box that says Type Here appears under File
- Click where it says Type Here and type Exit
- Under Edit type in Font and under Font type in Color
- Under Help type in About:
- Click the Toolbox again and find the Button control and drag one over to your form
- View the
button1
properties and go to the (Name) property - Replace
button1
withexitButton
- Still in the properties for the button find the Text property
- Replace
button1
with Exit - Now to insert the first line of code into your program:
- Double click on the new Exit button you just made
- Now this is where you will see the code for your project:
- Under
exitButton_Click
, type:
this.Close();
- Your code will now look like this:
private void exitButton_Click(object sender, EventArgs e) { this.Close(); }
- Go back to your design view by clicking Form1.cs[Design]*
- Click File on your program and then click Exit on your program
- Make sure the properties show up for the Exit you just clicked
- Click the Events button which is the lightning bolt
- Next to Click, click the drop-down arrow and select
exitButton_Click
: - Now we get to run your program for the first time
- Click the green arrow in the Standard Toolbar to run your program:
- Click the Exit button and watch your program close with the code we entered
- Run the program again and click File > Exit
- The program also closes because we linked the Exit button to the MenuStrip Exit we made by clicking the lightning bolt and linking the two together
- Go back to the Toolbox and drag a Label to your form
- Click the
label1
and copy it - Paste the
label1
2 times, so you are looking at 3 labels - Move the labels around so they look like this:
- Click
label1
and view its properties - Find the (Name) property and replace the text
label1
withfirstNameLabel
- Scroll down to the
Text
property and replacelabel1
with First Name: - Click
label2
and view its properties - Find the (Name) property and replace the text
label2
withmiddleNameLabel
- Scroll down to the
Text
property and replacelabel2
with Middle Name: - Click
label3
and view its properties - Find the (Name) property and replace the text
label3
withlastNameLabel
- Scroll down to the
Text
property and replace the textlabel3
with Last Name: - Align the labels so they still look good like this:
- Go back to the
Toolbox
and drag aTextBox
over to your form - Copy the
TextBox
and paste it 2 times - Align the 3
TextBox
es to be next to eachLabel
- Click the first
TextBox
and change the (Name) property tofirstNameTextBox
- Click the second
TextBox
and change the (Name) property tomiddleNameTextBox
- Click the third
TextBox
and change the (Name) property tolastNameTextBox
- Click each
TextBox
and make it longer and your outcome should look like this: - Go back to the
Toolbox
and add aFontDialog
control to your form - The control appears at the bottom in the Component Tray
- Click Edit > then double click Font
- Type this code which will allow you to change the font of all the labels and buttons:
private void fontToolStripMenuItem_Click(object sender, EventArgs e) { fontDialog1.Font = this.Font; fontDialog1.ShowDialog(); this.Font = fontDialog1.Font; }
- Run your program after you type the code for the Font control
- Click Edit > Font and just play around and see what you can do now
- When you're done having fun, exit the program
- Go back to Design View
- Go to the
Toolbox
and add aColorDialog
control - Click Edit > and double click Color
- Add the following code which will allow you to change the color of all the labels and buttons:
private void colorToolStripMenuItem_Click(object sender, EventArgs e) { colorDialog1.Color = this.ForeColor; colorDialog1.ShowDialog(); this.ForeColor = colorDialog1.Color; }
- Run your program again and play with the Font and Color features you now have
- When you're done, exit the program:
- In the Standard Toolbar click the drop-down arrow next to Add New Item
- Select Add New Item from the list:
- When the Dialog Box appears, select About Box in the list of items
- Leave the Name the same and click Add:
- When the About Box Form appears, double click the OK button:
- Type the following code which when you click OK will close the box:
private void okButton_Click(object sender, EventArgs e) { this.Close(); }
- Go back to Design View for the main form and click Help in your program, then double click About in your program
- Type the following code to have the About Box appear when you click Help > About:
private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { AboutBox1 aboutForm = new AboutBox1(); aboutForm.Show(); }
- Run your program and click Help > About
- The About Box appears but there is no information. Let's fix this.
- Exit the program
- Click Project > EasyProject Properties in the Menu Bar:
- Click Assembly Information
- Fill out all the information you would like to appear:
- Click OK
- Run the program again and click Help > About and now you can see the information you just entered
- If your title appears twice in the About Box, here is how to fix it
- Go to the AboutBox.cs tab at the top of your project. If it isn't there, just open the About Box designer and double click on OK
- In the line, delete one of the
{0}
s:
this.Text = String.Format("About {0} {0}", AssemblyTitle);
- Your code should now look like this:
this.Text = String.Format("About {0}", AssemblyTitle);
- Run your program. Click Help > About and notice that only one title now appears
- Exit your program
- Go back to Design View for the main form
- Go to the Toolbox and drag another Button to the left of Exit
- Go to the new Button’s properties
- Change the (Name) to
displayButton
- Scroll down to the
Text
property and change it toDisplay
- Double click on the Display button
- Once you see the source code, scroll to the very top
- Type the following line here:
- Scroll back down to
displayButton_Click
and insert the following code:
private void displayButton_Click(object sender, EventArgs e) { nameString = firstNameTextBox.Text + " " + middleNameTextBox.Text + " " + lastNameTextBox.Text; string messageString = "My name is: "; MessageBox.Show(messageString + nameString, "Name"); }
- After you have entered the code, run your program
- Play with all the features you added
- Type in your full name, hit Display and watch what happens:
- That's it. You did it!
Feel free to comment if you have any questions or problems with the program.
History
- 5th June, 2008: Initial post