Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / C#
Article

Very Easy Program for Beginners

Rate me:
Please Sign up or sign in to vote.
4.72/5 (28 votes)
5 Jun 2008CPOL6 min read 53K   511   36   11
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.

  1. Open Visual Studio
  2. Click New Project:

    1.JPG

  3. Select Windows Form Application from the list
  4. In the Name text box, change the name to EasyProject
  5. 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.
  6. Click OK
  7. Right click the form and select Properties from the list:

    3.JPG

  8. In the Properties Pane, scroll down to Text property
  9. Replace the words Form1 with Easy Project and hit enter
  10. If your Toolbox is not on the left side of the screen, click View in the menu bar and then select Toolbox:

    4.JPG

  11. In the Toolbox pane, scroll down to Menus & Toolbars and drag a MenuStrip control to your form and let go:

    5.JPG

  12. Click in the white text box that says Type Here
  13. When you see an insertion point, type File
  14. Click the white text box to the right of file and type Edit
  15. Click in the white text box to the right of edit and type Help

    6.JPG

  16. Click File again and a white text box that says Type Here appears under File
  17. Click where it says Type Here and type Exit
  18. Under Edit type in Font and under Font type in Color
  19. Under Help type in About:

    7.JPG

  20. Click the Toolbox again and find the Button control and drag one over to your form
  21. View the button1 properties and go to the (Name) property
  22. Replace button1 with exitButton
  23. Still in the properties for the button find the Text property
  24. Replace button1 with Exit
  25. Now to insert the first line of code into your program:

    9.JPG

  26. Double click on the new Exit button you just made
  27. Now this is where you will see the code for your project:

    10.JPG

  28. Under exitButton_Click, type:

    C#
    this.Close();
  29. Your code will now look like this:

    C#
    private void exitButton_Click(object sender, EventArgs e)
    {
        this.Close();
    }  
  30. Go back to your design view by clicking Form1.cs[Design]*
  31. Click File on your program and then click Exit on your program
  32. Make sure the properties show up for the Exit you just clicked
  33. Click the Events button which is the lightning bolt

    11.JPG

  34. Next to Click, click the drop-down arrow and select exitButton_Click:

    12.JPG

  35. Now we get to run your program for the first time
  36. Click the green arrow in the Standard Toolbar to run your program:

    13.JPG

  37. Click the Exit button and watch your program close with the code we entered
  38. Run the program again and click File > Exit
  39. 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
  40. Go back to the Toolbox and drag a Label to your form
  41. Click the label1 and copy it
  42. Paste the label1 2 times, so you are looking at 3 labels
  43. Move the labels around so they look like this:

    14.JPG

  44. Click label1 and view its properties
  45. Find the (Name) property and replace the text label1 with firstNameLabel
  46. Scroll down to the Text property and replace label1 with First Name:
  47. Click label2 and view its properties
  48. Find the (Name) property and replace the text label2 with middleNameLabel
  49. Scroll down to the Text property and replace label2 with Middle Name:
  50. Click label3 and view its properties
  51. Find the (Name) property and replace the text label3 with lastNameLabel
  52. Scroll down to the Text property and replace the text label3 with Last Name:
  53. Align the labels so they still look good like this:

    15.JPG

  54. Go back to the Toolbox and drag a TextBox over to your form
  55. Copy the TextBox and paste it 2 times
  56. Align the 3 TextBoxes to be next to each Label
  57. Click the first TextBox and change the (Name) property to firstNameTextBox
  58. Click the second TextBox and change the (Name) property to middleNameTextBox
  59. Click the third TextBox and change the (Name) property to lastNameTextBox
  60. Click each TextBox and make it longer and your outcome should look like this:

    16.JPG

  61. Go back to the Toolbox and add a FontDialog control to your form
  62. The control appears at the bottom in the Component Tray
  63. Click Edit > then double click Font
  64. Type this code which will allow you to change the font of all the labels and buttons:

    C#
    private void fontToolStripMenuItem_Click(object sender, EventArgs e)
    {
        fontDialog1.Font = this.Font;
        fontDialog1.ShowDialog();
        this.Font = fontDialog1.Font;
    } 
  65. Run your program after you type the code for the Font control
  66. Click Edit > Font and just play around and see what you can do now
  67. When you're done having fun, exit the program
  68. Go back to Design View
  69. Go to the Toolbox and add a ColorDialog control
  70. Click Edit > and double click Color
  71. Add the following code which will allow you to change the color of all the labels and buttons:
    C#
    private void colorToolStripMenuItem_Click(object sender, EventArgs e)
    {
        colorDialog1.Color = this.ForeColor;
        colorDialog1.ShowDialog();
        this.ForeColor = colorDialog1.Color;
    } 
  72. Run your program again and play with the Font and Color features you now have
  73. When you're done, exit the program:

    17.JPG

  74. In the Standard Toolbar click the drop-down arrow next to Add New Item
  75. Select Add New Item from the list:

    18.JPG

  76. When the Dialog Box appears, select About Box in the list of items
  77. Leave the Name the same and click Add:

    19.JPG

  78. When the About Box Form appears, double click the OK button:

    20.JPG

  79. Type the following code which when you click OK will close the box:

    C#
    private void okButton_Click(object sender, EventArgs e)
    {
        this.Close();
    } 
  80. Go back to Design View for the main form and click Help in your program, then double click About in your program
  81. Type the following code to have the About Box appear when you click Help > About:
    C#
    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
        AboutBox1 aboutForm = new AboutBox1();
        aboutForm.Show();
    } 
  82. Run your program and click Help > About
  83. The About Box appears but there is no information. Let's fix this.
  84. Exit the program
  85. Click Project > EasyProject Properties in the Menu Bar:

    21.JPG

  86. Click Assembly Information
  87. Fill out all the information you would like to appear:

    22.JPG

  88. Click OK
  89. Run the program again and click Help > About and now you can see the information you just entered
  90. If your title appears twice in the About Box, here is how to fix it
  91. 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
  92. In the line, delete one of the {0}s:

    C#
    this.Text = String.Format("About {0} {0}", AssemblyTitle);  
  93. Your code should now look like this:

    C#
    this.Text = String.Format("About {0}", AssemblyTitle);
  94. Run your program. Click Help > About and notice that only one title now appears
  95. Exit your program
  96. Go back to Design View for the main form
  97. Go to the Toolbox and drag another Button to the left of Exit
  98. Go to the new Button’s properties
  99. Change the (Name) to displayButton
  100. Scroll down to the Text property and change it to Display

    25.JPG

  101. Double click on the Display button
  102. Once you see the source code, scroll to the very top
  103. Type the following line here:

    26.JPG

  104. Scroll back down to displayButton_Click and insert the following code:

    C#
    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");
    } 
  105. After you have entered the code, run your program
  106. Play with all the features you added
  107. Type in your full name, hit Display and watch what happens:

    27.JPG

  108. 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Other
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Amir Mohammad Nasrollahi11-Aug-13 20:21
professionalAmir Mohammad Nasrollahi11-Aug-13 20:21 
QuestionVery excellent Pin
Zamshed Farhan12-Jan-13 15:41
Zamshed Farhan12-Jan-13 15:41 
Questionso txs!!!! Pin
sarah helm19-Jun-11 3:42
sarah helm19-Jun-11 3:42 
GeneralThank u very much Pin
stoney_rocks13-Mar-09 4:53
stoney_rocks13-Mar-09 4:53 
GeneralThanks a Lot Pin
Abu Syed Khan20-Feb-09 20:51
Abu Syed Khan20-Feb-09 20:51 
GeneralThank you Pin
oneplus6-Jan-09 2:12
oneplus6-Jan-09 2:12 
Generalregrarding instruction no.64 Pin
Lim Yuxuan31-Jul-08 16:43
Lim Yuxuan31-Jul-08 16:43 
Your code :

private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
fontDialog1.Font = this.Font;
fontDialog1.ShowDialog();
this.Font = fontDialog1.Font;
}

My question :

I understand that by changing all "this.Font" to "exitbutton.Font" will cause it to change only the exitbutton. But why did "this.Font" changes all font ?
AnswerRe: regrarding instruction no.64 Pin
gicif13-Aug-08 19:10
gicif13-Aug-08 19:10 
GeneralRe: regrarding instruction no.64 [modified] Pin
Lim Yuxuan13-Aug-08 20:47
Lim Yuxuan13-Aug-08 20:47 
GeneralExcellent Article Pin
marinaccio5-Jun-08 12:32
marinaccio5-Jun-08 12:32 
QuestionRe: Excellent Article Pin
psmruti9310-Sep-08 18:17
psmruti9310-Sep-08 18:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.