Lab 6
Lab 6
Faculty of Engineering
Department of Communication and Computer Engineering
Objectives:
Overviewofthe Microsoft.NETPlatform:
User Interfaces
The .NET Framework supports three types of user interfaces:
- Web Forms, which work through ASP.NET
- Windows Forms, which run on Win32 client computers
- Console Applications.
In this Lab we focus on Windows Forms Applications. You can use the System.Windows.Forms namespace
classes to build the client user interface (UI). This class lets you implement the standard Windows UI in your
.NET-based applications. Many functions that were previously only accessible by means of application
programming interface (API) calls are now available as part of the forms themselves, making development
much easier and more powerful.
Any language that conforms to the common language specification (CLS) can run on the common language
runtime. In the .NET Framework, Microsoft provides Visual Basic, Visual C++, Microsoft Visual C#™, Visual J#,
and Microsoft JScript® support. In this lab we focus on C# programming language.
C# was designed for the .NET Platform and is the first modern component–oriented language in the C and C++
family. It can be embedded in ASP.NET pages. Some of the important features of this language include classes,
interfaces, delegates, boxing and unboxing, namespaces, properties, indexers, events, operator overloading,
versioning, attributes, unsafe code, and XML documentation generation. No header or Interface Definition
Language (IDL) files are needed.
Creating asimpleC#WindowsForm:
Windows Applications make use of something called a Form. The Form is blank at first. You then add control
to your form, things like buttons, text boxes, menus, check boxes, radio buttons, etc. To get your first look at a
Windows Form, do the following:
1) Click File from the menu bar at the top of Microsoft Visual Studio.
2) From the File menu, select New, Then select Project.
3) The New Project Window will be opened for you.
4) From the left pane (Project types) select Visual C#.
5) From the right pane (Templates) select Windows Forms Application.
6) Keep the Name on the default of WindowsFormsApplication1 and then click OK.
When you click OK, a new Windows Application project will be created for you:
AddingControlstoaC#Form:
Controls are added to the form to provide the user with the proper interface. You can use the Toolbox on the
left of Visual Studio. Move your mouse over to the Toolbox, and click the plus symbol (or small triangle
symbol) next to Common Controls to expand the controls. If the Toolbox is not displayed, press on the view
menu then select Toolbox. In This Lab You will create a simple form to find the summation of two decimal
numbers as displayed in the next form.
First, you should add the controls you need in the form. In this exercise you need three labels, three text boxes
and two buttons. Drag and drop these controls from the toolbox to the form, and organize them so that your
form to looks similar to the one in the figure above. There is a set of properties for every control that controls
its look and behavior. The property window is usually located in the lower right corner of visual studio, if the
properties windows is not there, don’t worry we will see how to open it now.
To change the displayed text in the buttons and labels, the Text property needs to be modified. To do this,
right click on the label or the button you want to modify, then select properties from the displayed menu. If
not opened, the property window will open, and the text property will be selected. If the text property is not
highlighted, make sure you select it first, and then change its value accordingly. Modify the text property for
the three labels and two buttons to make your form look similar to the one in the figure above.
Another important property is the Name property, which specifies the name used to refer to a particular
control in your program. Names should be meaningful to ease understanding the code. Give names to the two
buttons in the properties window such that the name property for the button labeled “Find Sum” is Find_Sum
(note that spaces are not allowed in names), and the name for the button labeled “Exit” is Exit.
When the program runs the user can interact with the provided controls. Usually controls produce events,
which trigger actions to handle these events. For example, when the user clicks on a button it produces a
“Click” event. The events produced by a control can be seen by clicking the lightning bolt icon in the
properties window. Each event should be handled by a function, which should appear to the right of the
event. In our example, you want the program to calculate the summation of the two numbers when the user
clicks the “Find Sum” button. Therefore, we need to add a handler for the Click event for the Find_Sum button.
To add code to your button, double click the Find_Sum button. The coding window will open, and the cursor
will be flashing inside of the button code. Write the following code to read the two inserted decimal numbers
and display their summation:
private void Find_Sum_Click(object sender, EventArgs e)
{
float num1 = float.Parse(textBox1.Text);
float num2 = float.Parse(textBox2.Text);
textBox3.Text =(num1 + num2).ToString();
}
In the Exit button code call the close member function as follow:
To see the run of your form the debug window select start debugging or press F5.
ProjectFiles(Components):
The project files are displayed in the solution explorer (right pane), which can be viewed by selecting “Solution
Explorer” from the view menu. The solution explorer displays a tree of folders or nodes rooted at you project.
Usually the following folders are present by default:
Properties:
The Properties node represents configuration settings that apply to your entire project and are stored in the
.csproj file in your solution folder.
References:
In the context of a project, a reference simply identifies a binary file that your application requires in order to
run. Typically, a reference identifies a DLL file such as one of the .NET Framework class library files.
Forms:
When you create a Windows Forms project, Visual C# adds one form to the project by default and calls it
Form1. The two files that represent the form are called Form1.cs and Form1.designer.cs. You write your code
in Form1.cs, and the designer.cs file is where the Windows Forms Designer writes the code that implements
all the actions that you performed by dragging and dropping controls from the Toolbox.
Exercise:
There are two main temperature scales, the Fahrenheit Scale (used in the US), and the Celsius Scale (part of
the Metric System, used in most other countries). They both measure the same thing (temperature), but use
different scales:
So to convert from one scale to another we use one of the following formulas:
• From °F to °C: Deduct 32, multiply by 5, and then divide by 9
• From °C to °F: Multiply by 9, divide by 5, and then add 32
Design and implement a simple Windows program to do the math. Your application should look as follows:
To indicate which text box has the converted value, make the back color of this text box red. For example if
you converted from C to F the Fahrenheit box color should become red, while the color of the Celsius text box
is white. After that, if you convert from F to C, the C box should be red, while the F text box is white. To change
color, modify the “BackColor” property value to “Color.Red” or “Color.White”.
Submission:
You need to submit a report to your lab supervisor that contains the following:
2) Illustration of all the steps you performed in designing the previous exercise and including the
code for the two buttons (°C to °F) and (°F to °C). Also you need to run your application in
front of the lab supervisor.
3) Conclusions