Windows FOrm Introduction
Windows FOrm Introduction
• A windows form (win form) is a GUI container and a window that contains UI (user
interface), using which the user can interact with the system. Ex: Login form
• Windows forms applications support “local GUI” that runs on windows operating
system locally.
namespace FormConstructorExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MessageBox.Show("Message from Form1 Constructor");
}
}
}
=======================
namespace TextExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Text = "Hello";
}
}
}
=============================
namespace FormBackColorExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.BackColor = Color.Green;
}
}
}
=============================
namespace FormSizeExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Size = new Size(600, 350);
}
}
}
=============================
Load Event
===========
Executes after loading the form object into memory at run time.
namespace FormLoadExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("Message from Form Load event.");
}
}
}
========================================
Click Event
==========
Executes when the user clicks on the form
namespace FormClickExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Size = new System.Drawing.Size(600, 300);
this.Click += Form1_Click;
}
private void Form1_Click(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
string msg = "You have clicked the form at " + dt.ToString();
this.Text = msg;
}
}
}
DoubleClick Event
==============
Executes when the user double clicks on the form
namespace FormDoubleClickExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Size = new System.Drawing.Size(600, 200);
this.DoubleClick += Form1_DoubleClick;
}
private void Form1_DoubleClick(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
string msg = "You have double clicked the form at " + dt.ToString();
this.Text = msg;
}
}
}
MouseClick Event
===========
Same as “Click” event; but we can identify the mouse pointer position also in this
event.
namespace FormMouseClickExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Size = new System.Drawing.Size(600, 200);
this.MouseClick += Form1_MouseClick;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
s
string msg = e.X + ", " + e.Y;
this.Text = msg;
}
}
}
namespace FormBackColorExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.BackColor = Color.Green;
}
}
==================================
Event
====
An event is an member of a class used to perfrom required action.
every event will have perticular period of time that is to be called known as event
raising or firing
action:-Display "welcome" in text box 4 and change the back ground clor of text box
3
radio button
========
this control is used to provide selection of only one option from the given group
of options
namespace RadioButtonExample
{
public partial class Form1 : Form
{
//create reference variables
Label label1, label2;
RadioButton radiobutton1, radiobutton2;
public Form1()
{
InitializeComponent();
/* form properties */
this.Text = "RadioButton Example";
this.Font = new Font("Tahoma", 20);
this.Size = new Size(650, 380);
/* creating label1 */
label1 = new Label();
label1.Text = "Gender: ";
label1.AutoSize = true;
label1.Location = new Point(50, 50);
this.Controls.Add(label1);
/* creating label2 */
label2 = new Label();
label2.Text = "Selected option here";
label2.AutoSize = true;
label2.Location = new Point(50, 120);
this.Controls.Add(label2);
/* creating radiobutton1 */
radiobutton1 = new RadioButton();
radiobutton1.Text = "Male";
radiobutton1.AutoSize = true;
radiobutton1.Location = new Point(180, 50);
radiobutton1.CheckedChanged += Radiobutton1_CheckedChanged;
this.Controls.Add(radiobutton1);
/* creating radiobutton2 */
radiobutton2 = new RadioButton();
radiobutton2.Text = "Female";
radiobutton2.AutoSize = true;
radiobutton2.Location = new Point(280, 50);
radiobutton2.CheckedChanged += Radiobutton2_CheckedChanged;
this.Controls.Add(radiobutton2);
}
private void Radiobutton1_CheckedChanged(object sender, EventArgs e)
{
if (radiobutton1.Checked == true)
label2.Text = "Male selected";
else
label2.Text = "Female selected";
}
private void Radiobutton2_CheckedChanged(object sender, EventArgs e)
{
if (radiobutton1.Checked == true)
label2.Text = "Male selected";
else
label2.Text = "Female selected";
}
}
}