Example for Windows Application
The following example creates a win. Form with three text boxes and two button controls. When user enters two numbers in to first two text boxes and clicks on first button then sum has to be calculated for those two numbers and display the result in third text box.
1. Create a windows application and design the form as follows.
Open Visual Studio ->File -> New Project ->Visual C#-> select Windows Forms Application
Give the name of the application and click on OK
2. Set the following properties for the controls on the form.
TextBox1 Name : TxtNum1
TextBox2 Name : TxtNum2
TextBox3 Name : TxtResult
Button1 Name : BtnSum
Text : Sum
Button2 Name : BtnClose
Text : Close
3. Double click on the sum button and write the following code in the click event of that button to calculate sum and display the result in result textbox
private void BtnSum_Click(object sender, EventArgs e)
{
TxtResult.Text = (Convert.ToInt32(TxtNum1.Text) + Convert.ToInt32(TxtNum2.Text)).ToString();
}
4. Double click on close button and write the following code in the click event of that button to close the form.
private void BtnClose_Click(object sender, EventArgs e)
{
this.Close();
}
5. Run the Application with shortcut F5.
6. in the above example if you want to calculate sum when user press enter key and close the form when user press escape key, set sum button as acceptbutton for the form and close button as cancelbutton for the form.

No comments:
Post a Comment