“Windows forms applications” are “GUI (Graphical User Interface)” based
applications that can be developed in .NET.
• Windows forms applications are also called as “WinForms”.
• Windows forms applications are created using c#.net.
• Windows forms applications are user-friendly and easy to use.
• Windows forms applications are created based on Object-oriented programming
concepts.
• Windows forms application is a collection of windows forms.
in Windows Form Applications we can work with all controls like
textbox,button,radio button ,listbox etc
• 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 can interact with databases using ADO.NET.
• Windows forms applications support “local GUI” that runs on windows operating
system locally.
IDE(intregated Development Environment)
===========================
IDE Contains vaious components like 1.solution Explorer,tool
Box,ServerExplorer,properties Window,output Window, Error list window Etc...
A form is a container which can hold all other controls with in it
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.
Executes after immediately after form constructor
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;
}
}
==================================
Text Box Control
=============
this control is used to accept the data from the user and also display the data
dynamically to the user
Event
====
An event is an member of a class used to perfrom required action.
event is similar to function but is pointed by delegate or function pointer.
every event will have perticular period of time that is to be called known as event
raising or firing
delegates are completely back bone for the events.
Button” is a class; “System.Windows.Forms” is a namespace.
• The “Button” class / control is used to display an option on the windows
public partial class Form1 : Form
{
//create a reference variable
Button button1;
public Form1()
{
InitializeComponent();
//form properties
this.Size = new System.Drawing.Size(500, 300);
this.Text = "Button Example";
//create an object
button1 = new Button();
//set properties to the button
button1.Text = "Click me please";
button1.Font = new System.Drawing.Font("Tahoma", 30);
button1.Size = new System.Drawing.Size(200, 60);
button1.Location = new System.Drawing.Point(100, 100);
button1.Cursor = Cursors.Hand;
//add events to the button
button1.Click += button1_Click;
button1.MouseEnter += Button1_MouseEnter;
button1.MouseLeave += Button1_MouseLeave;
//add button to the form
this.Controls.Add(button1);
}
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "Clicked";
}
private void Button1_MouseEnter(object sender, EventArgs e)
{
button1.BackColor = System.Drawing.Color.LightGreen;
}
private void Button1_MouseLeave(object sender, EventArgs e)
{
button1.BackColor = System.Drawing.SystemColors.Control;
}
}
}
text box example
=============
1. when focus leaves from text box1
action:-Display "welcome" in text box 4 and change the back ground clor of text box
3
2.when the user clicks on text box 1
change the back groud color of form
radio button
========
this control is used to provide selection of only one option from the given group
of options
the radio button can display text or image or both
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";
}
}
}