Windows Forms with C# using Visual Studio 2012

About this Tutorial –

Objectives –

Delegates will learn to develop web applications using C# 4.0. After completing this course, delegates will be able to:

  • Use Visual Studio 2012 effectively
  • Windows Forms with C# using Visual Studio 2012

Audience

This course has been designed primarily for programmers new to the .Net development platform. Delegates experience solely in Windows application development or earlier versions of ASP.Net will also find the content beneficial.

Prerequisites

Before attending this workshop, students must:

  • Be able to manage a solution environment using the Visual Studio 2012 IDE and tools
  • Be able to program an application using a .NET Framework 4.0 compliant language

Contents

Copyright 20/12/12 – David Ringsell

Download Solutions

Java tutorial

Lab 1. Building Applications with Windows Forms
Exercise 1
Creating the Customer Form
In this exercise, you will create a form that to show customer information. You will later add a MenuStrip, ToolStrip and other controls to this form.

Customer Entry

  1. Create the Windows Application project:
    • Start Visual Studio 2012.
    • Select the File menu, then New, then Project.
    • Make sure (if you are using the Professional Version) that Visual C# and
      then Windows is selected at the left of the screen under Installed Templates.
    • Name the project and the solution: Customers.
    • Browse to the place on your computer where you wish Visual Studio
      to create the directory for your solution (keeping the tick box selected).
    • Select Windows Forms Application.
  2. Add a Windows Form to the project called frmCustomers:
    • Right-click on the project name: Customers.
    • Select Add.
    • Select New Item.
    • Select Windows form.
  3. Add the list box and text boxes to the form as shown above.
  4. Set the Anchor properties of the text box controls.
  5. Test the project confirm that all controls are anchored correctly.
  6. Using the Toolbox, add a MenuStrip control.
  7. Add a File and a Help menu to the MenuStrip control.
  8. Add the following sub-menus:
    • File: Load Customers, Exit.
    • Help: About.

Exercise 2
Enhancing the Customer Form
In this exercise, you will enhance the customer form by adding a ToolStrip and a ToolTip control. You will also add functionality to the MenuStrip and ToolStrip.

  1. Using the Toolbox, add a ToolStrip control.
  2. Add 3 buttons to the ToolStrip.
  3. Set the Text and the ToolTipText properties for the buttons to:
    • Load Customers
    • Exit
    • Help
  4. Add an About box to the project by right clicking the project name in Solution Explorer.
  5. Add event handlers for the menu items. For example:
    private void loadCustomersToolStripMenuItem_Click(object sender, EventArgs e)
    {
    this.Close();
    }
  6. Add event handlers for the tool strip button. For example:
    private void ToolStripButton2_Click(object sender, EventArgs e)
    {
    //The Exit toolstrip button calls the Exit menu event handler
    loadCustomersToolStripMenuItem_Click(sender, e);
    }
  7. Add ToolTip user assistance by dragging a ToolTip control on to the form designer, then setting the list box’s ToolTip property.

Exercise 3
Explore further Windows Forms features
In this exercise, you will enhance the customers form to open a second form. You will add container controls & provider controls to this second form.

Custom Layout

  1. Add another form to the project called frmCustomerDetails and open this form from the customers form:
    • using the ShowDialog method (modal).
    • as an owned form (non-modal).
  2. You can do this by double-clicking the button and adding this code to the event handler:
    //Show a second form
    frmCustomerDetails f = new frmCustomerDetails();
    //Show as a dialog box (modal)
    f.ShowDialog();
    //Show as a owned form (non-modal)
    //this.AddOwnedForm(f);
    //f.Show();

    By adding the forward-slash to the beginning of the sentence, the following text in the sentence is not treated as code by the compiler; it is commented out. For it to compile and execute as code, just remove the forward slash characters from the sentence. By commented out code you can choose which code you want to have activated in your program.

  3. Add some container controls to the new form, for example:
    • SplitContainer
    • TableLayoutPanel
    • FlowLayoutPanel
  4. Populate the container controls with further controls.
  5. Add an ErrorProvider and HelpProvider control to the form.
  6. Configure the ErrorProvider and HelpProvider control to show appropriate messages for associated controls.
  7. View code file.

Lab 2. Customising Windows Forms and Controls
Exercise 1
Creating a custom text box that only shows characters in upper case
In this exercise you create a custom text box by inheriting from the text box class. The new text box will convert characters to upper case by overriding the OnTextChanged method.

  1. Create a new Windows Class Library project called UpperText.
  2. Add a reference to the System.Windows.Forms namespace:
    • Select the Project Menu or right-click the solution name.
    • Then select Add Reference.
    • Make sure the .NET tab is selected and search for the above namespace.
    • Select it and then click ok.
    • Type using System.Windows.Forms; into the beginning of the Class1.cs file.
  3. Rename the Class1.cs file to UpperText.cs
  4. In the class declaration include an inherits statement:
    public class UpperTextBox : TextBox
  5. At the top of the UpperText.cs file import the forms namespace: using System.Windows.Forms;
  6. Override the base textbox’s OnTextChanged() method with: protected override void OnTextChanged( EventArgs e )
    {
    int sel = SelectionStart;
    Text = Text.ToUpper();
    SelectionStart = sel;
    base.OnTextChanged( e );
    }
  7. Build the project
  8. View code file.

Exercise 2
Creating a new Windows Application that tests the custom text box on a form

Upper Text

  1. Add a Windows Applications project to the solution and call it ControlTester.
  2. Set this project as the start-up project.
  3. Drag the upper text box from the toolbox to the form designer.
  4. Test the control by running the project and typing text in lower case in to the text box.
  5. Create a new Windows Applications project in a separate .Net solution.
  6. Add the custom text box to the designer toolbox by right clicking, then selecting Choose Items … from the context menu.
  7. Browse to the .dll file for the custom text box.
  8. Drag the custom text box on to the form and again test the project.

Exercise 3
Creating a user control that shows product information
In this exercise you create a user control. The control contains three buttons; each will display different product information.

Customer Details

  1. Create a new Windows Control Library project called CustomUserControl.
  2. Rename the UserControl1.cs file to ProductDetails.cs
  3. Drag three button from the toolbox on to the user controls designer.
  4. Add this code to the each of the button’s event handlers (the 1st statement goes in the 1st event handler, etc.):
    MessageBox.Show("The company name is PCTalk");
    MessageBox.Show("The product name is customers application");
    MessageBox.Show("The product version is 1.0.0.0");
  5. Run the project. The user control is shown in a TestContainer dialog box.
  6. View code file.

Exercise 4
Creating a Windows Application that tests the user control on a form
In this exercise you will test the user control you have created. You will do this on from a new Windows Application project.

  1. Add a Windows Forms Applications project to the solution and call it Controls. This is achieved by:
    • Right-clicking the solution name: ControlTester.
    • Select Add.
    • Select New Project.
  2. Set this project as the start-up project.
  3. Drag the custom control from the CustomUserControl tab on toolbox to the form designer.
  4. Run the project and test the user control by clicking the buttons.
Exercise 5
Creating a custom about box form
In this exercise you will create a custom About Box with an OK button. You will then add another form that inherits from the About Box, but changes the behavior of the button.

Inherited Form

  1. Create a new Windows Forms Application project called VisualInheritance.
  2. Add an About Box to the project called frmBase. Make the scope of the class frmBase public.
  3. Customize the About Box with appropriate labels.
  4. Add code to the button’s event handler to close the form, make the event handler virtual and have protected scope
    protected virtual void okButton_Click(object sender, EventArgs e)
  5. Make this form the start up form for the project and test it.
  6. Add a second Windows Form to the project called frmInherited.
  7. In this form’s code file (frmInherited.cs), edit the class definition to:
    public partial class frmInherited : VisualInheritance.frmBase
  8. Override the OK button’s event handler, so that it also displays a message:
    protected override void okButton_Click(object sender, EventArgs e)
    {
    MessageBox.Show("The form will close");
    this.Close();
    }
  9. Run the project and test the inherited About Box by clicking the OK button.
  10. View code file.
If you liked this post, please comment with your suggestions to help others.
If you would like to see more content like this in the future, please fill-in our quick survey.
Scroll to Top