Introduction to Widows Forms and Visual Studio IDE

2017/10/05 05:45
Visual Studio .NET provides a user-friendly graphic designer for building user interfaces. With it, within a few minutes, a user interface design can be built, whether it's a Windows Forms window, a Web page, or a mobile application interface

Compilation
When compiling, VS.NET automatically creates the required assemblies and resource files. It also takes care of satellite assemblies (if any), refreshes references to external files and classes and performs many other tasks.
Besides syntax errors, the compilation process captures some semantics. Additionally, it may also display warning messages about suspicious or bad code. You can control the level of filtering of these alerts and even set the environment to consider them as errors and break the compilation process for them.
VS.NET offers two compilation modes:
  • Debug - In this mode, the compiler creates debug symbols for all methods in the application and writes them to a separate file with .pdb extension. Through it, debugging can be done. This compilation mode is recommended for the application development and testing process.
  • Release - In this compilation mode, Visual Studio creates code, ready for production and distribution to customers. It removes all the debugging and testing functions. No .pdb files are generated and generally has better performance than the Debug version. By contrast, debugging opportunities are reduced.

Running and testing
Because Visual Studio .NET integrates the development of applications with different technologies, we can run one of the application types in a unified way. What you need to do is simply click the Start button (or Debug / Start). The environment checks if there are any changes to the project files, if any, it recompiles the application and then starts the process.
VS.NET supports so-called solutions. One solution may have one or more projects. There is an opportunity to indicate a project to be launched at the start of the solution.

Error tracking
The error tracking process, or more commonly called debugging, is very easy with Visual Studio .NET. The environment provides many built-in tools to accomplish this task. The tools are available from the Debug menu and include the following options:
  • Breakpoints - List of breakpoints. You can remove, create and adjust the parameters of each point individually.
  • Running documents - a list of all the files currently used by the application. Used mainly for debugging web applications.
  • Call stack - displays the method call stack by the time. It is perfect for analyzing program logic and finding the place where an exception has occurred.
  • Autos - shows all the variables currently in range.
  • Local - shows all local variables.
  • Immediate / Command Window - allows to execute instructions and change values of variables during execution.
  • Watch - displays a list of all the variables that are requested to be monitored. It can also change their values during program execution.
  • Quick Watch - shows the value of a debugged variable.
  • Step control - provides the means for stepwise execution of the code in row-by-row and step-by-step. The order of execution can be selected; to implement methods by entering them or waiting to finish and moving to the next step; it is possible to control which rows of code are executed and, if necessary, to move the forward and reverse cursor.
  • Exception control - you can specify whether the application should go into debug mode when an exception occurs and stop immediately after the exception occurred without leaving a breakpoint.

Sample task
1. Creating new Visual Studio project
Start Visual Studio and choose New Project…



2. To create Windows Forms application, from the templates in the left window choose Installed > Templates > Visual C# > Windows. From the list on the right choose Windows Forms Application. Set name and location for your project and select OK.



After the project is created you and loaded you should see empty Windows form. On the left of the screen find and open the Toolbox tab. From the list expand the All Windows Forms section, find the Button control and double click on it. This will place the button on the top left corner of the form.

   

Adding a visual component (control) to the form is able by two ways: double clicking on the desired control or drag-and-drop of the control. Then the only thing that lefts is to configure the properties of the control and to add events to it.

3. Select the button by clicking on it, choose the Properties tab on the right of the screen (or right click on the button and from the contextual menu choose Properties) and create the following changes to its properties:
  • Find the Font property and click on the ‘…’ button. Choose font size to be 11 and click OK.
  • Find the Text property double click on it and type Congatulations.

   

4. Double click on the button. A new file, with name Form1.cs, will open. This is the form class file and all C# code will be written here. Copy and paste the following code.

using System;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("\t Hello Students, ... \n \n \t \t This is your first C# Program ... \n \n \t \t \t Be Happy :)))");
        }
    }
}

button1_Click is the event that will fire when the button is clicked.
MessageBox is class from the System.Windows.Forms namespace. It displays message window (dialog window /box/), which displays a message to the user.

5. Start the application – this could be done in two ways:
  • Whit the pressing of the button F5 on your keyboard.
  • With clicking on the Start button –  – from Visual Studio Toolbar.

When the application loads click the “Congratulations” button to see the message.
To close the application click on the “OK” button then close Form1 from the ‘X’.

Self-assignments:
1) Study the Visual Studio interface in greater details.
2) Change the color of the button from the example above. For this use the BackColor property. Change the text formatting for the entire form and every control, using the Font property.
3) Study and play around with other of properties of the form and the button.