CheckedListBox
Multiple items will be listed in a single control in the form of checkboxes.
By default you can select an item by double click it. To select by one click change CheckOnClick property to true.
To add items in the design mode click on the smart tag(Play symbol at the top of the control). Click on edit items. Enter text of items one per line to create multiple items.
Default event: SelectedIndexChanged which is fired when user selects and item
By default you can select an item by double click it. To select by one click change CheckOnClick property to true.
To add items in the design mode click on the smart tag(Play symbol at the top of the control). Click on edit items. Enter text of items one per line to create multiple items.
Default event: SelectedIndexChanged which is fired when user selects and item
Example
to display all the selected items text on a message box
private void
checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (string item in checkedListBox1.CheckedItems)
{
s+= item.ToString();
}
MessageBox.Show(s);
}
CheckBox
It allows you to check multiple or even all of the checkboxes.
Default event: CheckedChanged
Checked changed event is fired every time when a user select or deselect the checkbox.
Checked: Determines if the checkbox is checked.
CheckState: Tells whether the checkbox is Checked, Unchecked.
ThreeState: If true, the checkbox can accept an Intermediate state.
Example to show selected items using checkboxes on button click
Default event: CheckedChanged
Checked changed event is fired every time when a user select or deselect the checkbox.
private void
checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
checkBox1.Text = "Selected";
}
else
{
checkBox1.Text = "Not
Selected";
}
}
Checked: Determines if the checkbox is checked.
CheckState: Tells whether the checkbox is Checked, Unchecked.
ThreeState: If true, the checkbox can accept an Intermediate state.
Example to show selected items using checkboxes on button click
private void button1_Click(object sender, EventArgs e)
{
string items = String.Empty;
if (checkBoxSoap.Checked)
items += "\n
Soap";
if (checkBoxShampoo.Checked)
items += "\n
Shampoo";
if (checkBoxToothpaste.Checked)
items += "\n
Toothpaste.";
MessageBox.Show("You have bought: " + items);
}
Button
It is used to execute when button is clicked.
Default event is Click.
To show message box on button click with text as Hello world:
Example for other events like MouseEnter and MouseLeave
Default event is Click.
To show message box on button click with text as Hello world:
private void
button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello world");
}
To change the text on the button:
private void
button1_Click(object sender, EventArgs e)
{
button1.Text = "Click
here";
}
To make button invisible:
private void button1_Click(object sender, EventArgs e)
{
button1.Visible =false;
}
We
can also use other events of button like MouseEnter and MouseLeave.
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.Text = "Mouse
entered";
}
private void button1_MouseLeave(object
sender, EventArgs e)
{
button1.Text = "Mouse
left";
}
Form
Every form is a class. You can create multiple forms in a single windows application. The namespace System.Windows.Forms contains components you will need for creating forms and controls.
Form load event is fired when the form is finished loading just before it is displayed.
MinimizeBox: Tells whether the minimize box located at the top right is displayed.
Name: The name of the form that is used to reference it in the code.
Text: The text that is shown in the caption bar of the form.
StartPosition: The starting position of the form when it is initially shown.
Right click on the root folder of the project in solution explorer.
Click on Add – New Item
Select Windows Form from the list of items
Enter form name with .cs as extension and click on Add. Example: SecondForm.cs
Navigation from one form to another form
Code in Button click of first form to navigate to second form on button click.
private void button1_Click(object sender, EventArgs e)
{
SecondForm f = new SecondForm();
f.Show();
this.Hide();
}
Show method is used to display the new form for which object is created.
Hide method is used to hide the existing form by using this keyword which refers to the current form in which object is created.
Every windows application will have a main method in Program class where execution of application starts. In main method Run method is used to call the start-up form.
Form1 is the Startup form name used in the above example.
Replace Form1 with new form name to display that new form when application is launched.
Form load event is fired when the form is finished loading just before it is displayed.
Properties of Form:
MaximizeBox: Tells whether the maximize box located at the top right is displayed.MinimizeBox: Tells whether the minimize box located at the top right is displayed.
Name: The name of the form that is used to reference it in the code.
Text: The text that is shown in the caption bar of the form.
StartPosition: The starting position of the form when it is initially shown.
Creating new form
Right click on the root folder of the project in solution explorer.
Click on Add – New Item
Select Windows Form from the list of items
Enter form name with .cs as extension and click on Add. Example: SecondForm.cs
Navigation from one form to another form
Code in Button click of first form to navigate to second form on button click.
private void button1_Click(object sender, EventArgs e)
{
SecondForm f = new SecondForm();
f.Show();
this.Hide();
}
Show method is used to display the new form for which object is created.
Hide method is used to hide the existing form by using this keyword which refers to the current form in which object is created.
Startup form
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Every windows application will have a main method in Program class where execution of application starts. In main method Run method is used to call the start-up form.
Form1 is the Startup form name used in the above example.
Replace Form1 with new form name to display that new form when application is launched.
Win forms controls
Winforms controls are the GUI controls
which are used to design windows forms applications.
They can be drag and drop from the toolbox to the design form and double on the controls will create default events in the code behind page.
They can be drag and drop from the toolbox to the design form and double on the controls will create default events in the code behind page.
Common controls
·
Pointer
·
Button
·
CheckBox
·
CheckedListBox
·
DateTimePicker
·
Label
·
LinkLabel
·
ListBox
·
ListView
·
MaskedTextBox
·
MonthCalendar
·
NotifyIcon
·
NumericUpDown
·
PictureBox
·
ProgressBar
·
RadioButton
·
RichTextBox
·
ToolTip
·
TreeView
·
WebBrowser
Containers
·
FlowLayoutPanel
·
GroupBox
·
Panel
·
SplitContainer
·
TabControl
·
TableLayoutPanel
Menus & Toolbars
·
ContextMenuStrip
·
MenuStrip
·
StatusStrip
·
ToolStrip
·
ToolStripContainer
Dialogs
·
ColorDialog
·
FolderBrowserDialog
·
FontDialog
·
OpenFileDialog
·
SaveFileDialog
Creating Windows application
1. Open Visual Studio
Figure: VisualStudio
2. Click on new project
3. Select a language as C#.NET from the left side.
4. Select windows application from the list of applications
Figure: WindowsApp
5. Give the name to the project
6. Select the location to save the project. click on ok.
Now default environment to create windows application is displayed.
7. F5 is the shortcut key to start debugging or to execute a program (or) click on play symbol button present in the center of top menu of visual studio tool.
8. Shift + F5 for stop debugging or execution.
Figure: VisualStudio
2. Click on new project
3. Select a language as C#.NET from the left side.
4. Select windows application from the list of applications
Figure: WindowsApp
5. Give the name to the project
6. Select the location to save the project. click on ok.
Now default environment to create windows application is displayed.
7. F5 is the shortcut key to start debugging or to execute a program (or) click on play symbol button present in the center of top menu of visual studio tool.
8. Shift + F5 for stop debugging or execution.
Introduction to WinForms
Win Forms is a technology under .NET framework used to develop windows applications.
Windows applications are the GUI applications which can be deployed in the form of exe files.
Example: VLC Player, Calculator.
Controls are user to develop windows applications.
They has to installed in a system to use them in that system.
Windows applications are the GUI applications which can be deployed in the form of exe files.
Example: VLC Player, Calculator.
Controls are user to develop windows applications.
They has to installed in a system to use them in that system.