0% found this document useful (0 votes)
30 views12 pages

Theory C#

Uploaded by

faisalsalhyhyat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views12 pages

Theory C#

Uploaded by

faisalsalhyhyat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Visual Programming using C#

Label Control
Labels are one of the most frequently used C# control. We can use the Label control to
display text in a set location on the page. Label controls can also be used to add
descriptive text to a Form to provide the user with helpful information.

Drag label from the toolbox and Change the label name to label control text property

Button Control
Windows Forms controls are reusable components that encapsulate user interface
functionality and are used in client side Windows applications. A button is a control,
which is an interactive component that enables users to communicate with an
application. The Button class inherits directly from the ButtonBase class. A Button can
be clicked by using the mouse, ENTER key, or SPACEBAR if the button has focus.

MCS (3rd Semester) Page 1


Visual Programming using C#

When you want to change display text of the Button , you can change the Text property
of the button.

TextBox Control
A TextBox control is used to display, or accept as input, a single line of text. This control
has additional functionality that is not found in the standard Windows text box control,
including multiline editing and password character masking.

A text box object is used to display text on a form or to get user input while a C#
program is running. In a text box, a user can type data or paste it into the control from
the clipboard.

ComboBox Control
C# controls are located in the Toolbox of the development environment, and you use
them to create objects on a form with a simple series of mouse clicks and dragging
motions. A ComboBox displays a text box combined with a ListBox, which enables the
user to select items from the list or enter a new value.

MCS (3rd Semester) Page 2


Visual Programming using C#

The user can type a value in the text field or click the button to display a drop down list.
You can add individual objects with the Add method. You can delete items with the
Remove method or clear the entire list with the Clear method.

How to add value in ComboBox

How to retrieve value from ComboBox

If you want to retrieve the displayed item to a string variable , you can code like this

How to remove an item from ComboBox

You can remove items from a combobox in two ways. You can remove item at a the
specified index or giving a specified item by name.

The above code will remove the second item from the combobox.

The above code will remove the item "Friday" from the combobox.

DropDownStyle

The DropDownStyle property specifies whether the list is always displayed or whether
the list is displayed in a drop-down. The DropDownStyle property also specifies whether
the text portion can be edited.

MCS (3rd Semester) Page 3


Visual Programming using C#

ComboBox Selected Value

How to set the selected item in a comboBox

You can display selected item in a combobox in two ways.

ListBox Control
The ListBox control enables you to display a list of items to the user that the user can
select by clicking.

Setting ListBox Properties

You can set ListBox properties by using Properties Window. In order to get Properties
window you can Press F4 or by right-clicking on a control to get the "Properties" menu
item.

In this method you add item in the listbox by edit the item collection this process is to
add items in the listbox at design time.

MCS (3rd Semester) Page 4


Visual Programming using C#

Add Items in a Listbox

There is other method to add item in the list box.

Listbox Selected Item

If you want to retrieve a single selected item to a variable , use the following code.

OR you can use

MCS (3rd Semester) Page 5


Visual Programming using C#

OR

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
listBox1.Items.Add("Data");
listBox1.Items.Add("Information");
listBox1.Items.Add("Processing");
}

MCS (3rd Semester) Page 6


Visual Programming using C#

private void button1_Click(object sender, EventArgs e)


{
string item = listBox1.Text;
MessageBox.Show(item,"ListBox");
}
}
}
Selecting Multiple Items from Listbox

The SelectionMode property determines how many items in the list can be selected at
a time. A ListBox control can provide single or multiple selections using the
SelectionMode property . If you change the selection mode property to multiple select ,
then you will retrieve a collection of items from ListBox1.SelectedItems property.

The ListBox class has two SelectionMode. Multiple or Extended .

In Multiple mode, you can select or deselect any item in a ListBox by clicking it. In
Extended mode, you need to hold down the Ctrl key to select additional items or the
Shift key to select a range of items.

The following C# program initially fill seven days in a week while in the form load event
and set the selection mode property to MultiSimple . At the Button click event it will
display the selected items.

MCS (3rd Semester) Page 7


Visual Programming using C#

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ListBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
listBox1.Items.Add("Sunday");
listBox1.Items.Add("Monday");
listBox1.Items.Add("Tuesday");
listBox1.Items.Add("Wednesday");
listBox1.Items.Add("Thursday");
listBox1.Items.Add("Friday");
listBox1.Items.Add("Saturday");
listBox1.SelectionMode = SelectionMode.MultiSimple;

private void button1_Click(object sender, EventArgs e)


{
string items = " ";
foreach (var item in listBox1.SelectedItems)
{
items += item.ToString() +"\n";

MessageBox.Show(items,"Listbox",MessageBoxButtons.OK,MessageBoxIcon.Informati
on);
}

MCS (3rd Semester) Page 8


Visual Programming using C#

}
}

RadioButton Control
A radio button or option button enables the user to select a single option from a group of
choices when paired with other RadioButton controls. When a user clicks on a radio
button, it becomes checked, and all other radio buttons with same group become
unchecked.

Program:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

MCS (3rd Semester) Page 9


Visual Programming using C#

private void button1_Click_1(object sender, EventArgs e)


{
if (radioButton1.Checked == true)
{
MessageBox.Show("You are selected Yellow !! ");
}
else if (radioButton2.Checked == true)
{
MessageBox.Show("You are selected White !! ");
}
else
{
MessageBox.Show("You are selected Black !! ");
}
}

}
}

CheckBox Control
CheckBoxes allow the user to make multiple selections from a number of options.
CheckBox to give the user an option, such as true/false or yes/no. You can click a
check box to select it and click it again to deselect it.

MCS (3rd Semester) Page 10


Visual Programming using C#

The CheckBox control can display an image or text or both. Usually CheckBox comes
with a caption, which you can set in the Text property.

The radio button and the check box are used for different functions. Use a radio button
when you want the user to choose only one option. When you want the user to choose
all appropriate options, use a check box. The following C# program shows how to find a
checkbox is selected or not.

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void checkBox3_CheckedChanged(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{
string msg = "";

if (checkBox1.Checked == true)
{
msg = "Black\n";
}

if (checkBox2.Checked == true)
{
msg = msg +"Blue\n";
}

MCS (3rd Semester) Page 11


Visual Programming using C#

if (checkBox3.Checked == true)
{
msg = msg + "Green";
}

if (msg.Length > 0)
{
MessageBox.Show(msg,"Message");
}
else
{
MessageBox.Show("No checkbox is selected","Message");
}
}
}
}

MCS (3rd Semester) Page 12

You might also like