Lecture5C GUI
Lecture5C GUI
Scripting
Lecture 6: C# GUI Development
Blank Form
Discussion
Code Explained
using System;
using System.Drawing;
using System.Windows.Forms;
Discussion
10
11
Form Properties
12
Form Events
13
Adding Numbers
14
Generated Code
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
partial class Form1 : Form
{
private System.ComponentModel.IContainer
components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
Systems Prog. & Script. - Heriot Watt Univ
15
16
17
18
System.Windows.Forms.Label label1;
System.Windows.Forms.Label label2;
System.Windows.Forms.TextBox textBox1;
System.Windows.Forms.TextBox textBox2;
System.Windows.Forms.Button button1;
System.Windows.Forms.Label label3;
// event handlers
private void InitializeComponent(object sender, EventArgs e)
{
// put intialization code here
}
private void button1_Click(object sender, EventArgs e)
{
string inValue1, inValue2;
double val1, val2, result;
inValue1 = textBox1.Text;
inValue2 = textBox2.Text;
val1 = double.Parse(inValue1);
val2 = double.Parse(inValue2);
result = val1 + val2;
label3.Text = result.ToString();
}
}
19
20
<summary>
Required method for Designer support - do not modify
the contents of this method with the code editor.
</summary>
21
22
23
24
}
#endregion
private
private
private
private
private
private
System.Windows.Forms.Label label1;
System.Windows.Forms.Label label2;
System.Windows.Forms.TextBox textBox1;
System.Windows.Forms.TextBox textBox2;
System.Windows.Forms.Button button1;
System.Windows.Forms.Label label3;
}
}
25
Event Handler
private void button1_Click(object sender, EventArgs e)
{
string inValue1, inValue2;
double val1, val2, result;
inValue1 = textBox1.Text;
inValue2 = textBox2.Text;
val1 = double.Parse(inValue1);
val2 = double.Parse(inValue2);
result = val1 + val2;
label3.Text = result.ToString();
}
26
Code
Since you use Visual C# to develop this
form, Visual Studio will generate the basic
coding for all the items that you place in the
form.
You have to write your own code when you
want to perform any operations on the items,
i.e. to handle any events, change the
properties etc.
Systems Prog. & Script. - Heriot Watt Univ
27
Exercise
Create a form with several buttons, text
boxes, change the properties and define
different events associated with the
buttons.
28
Useful Links
Various C# tutorials:
www.functionx.com/vcsharp/index.htm
Mono C# Winforms Tutorial:
http://zetcode.com/tutorials/monowinformstutorial/
29