Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

CSharp.NET Tutorial – Day 18

Tags: property


In previous article, we have discussed How to Create a Notepad application in Visual Studio, how to use Menustrip control, how to add menu items to Menustrip control, how to create dialog controls and how to use them, how to create About Box control and how to use it, how to set different kind of actions in Menu strip control etc along with syntaxes and examples

Please find below the link for accessing the article

CSharp.NETTutorial - Day 17

Now, in this article we will discuss what is PictureBox Control and its 'Size Mode' Property, what are RadioButton and CheckBox and its common Boolean property Checked, some events like CheckedChanged, what is Tag Property, what are ComboBox, ListBox, and CheckedListBox, How to add values to the controls like ComboBox, ListBox and CheckedListBox, Multiple selection for ListBox and its Selection Mode, How to find the Items selected under the controls, SelectedIndexChanged along with some examples.

PictureBox Control

PictureBox control is mainly used for displaying images within a windows application in order to bind an image to the control that we can set with any of the below-mentioned properties.

ImageLocation = <image path>
Image = Image.FromFile (<image path>)
Image = Image.FromStream (Stream stream)

Using its 'Size Mode' property, we can control how the PictureBox handle image placement and size which we can set with any of the below values.

a) Normal
b) Stretch image
c) Auto size
d) Center image

RadioButton and CheckBox

Both these two controls RadioButton and CheckBoxare used for users to choose from a list of available values.  Here the difference is RadioButton allows only single selection, whereas CheckBox allows multi-selection.

Tip:
As RadiButton allows single selection, if we want to use it under multiple questions or options, we need to make all Radiobuttons in a group so that we can select only one from each individual group.  In order to group Radio buttons, we need to place them on a separate container controls like Panel, GroupBox etc.

Note:  Both RadioButton and CheckBox controls provide a common Boolean property i.e. 'Checked'.  Using Checked property, we can identify whether the particular control is selected or not.  It will return 'true' if selected else it will return 'false'.


Button1_Click:

if (radioButton1.Checked)
MessageBox.Show ("RadioButton1 is clicked");
else if (radioButton2.Checked)
MessageBox.Show ("RadioButton2 is clicked");
else if (radioButton3.Checked)
MessageBox.Show ("RadioButton3 is clicked");

Button2_Click:
if (checkBox1.Checked)
MessageBox.Show ("CheckBox1 is Selected");
if (checkBox2.Checked)
MessageBox.Show ("CheckBox2 is Selected");
if (checkBox3.Checked)
MessageBox.Show ("CheckBox3 is Selected");
-------------------------------------------------------------------------------------------------------------

CheckedChanged
CheckedChanged is the default event of both RadioButton and CheckBox controls, which will get raised when the particular control is selected or deselected.


Go to the Properties of 'Fees' TextBox, set its name as 'txtFees', 'ReadOnly' property as 'True' and also 'Text' as '0'.

For each and every CheckBox and RadioButton, set the property tag with the corresponding fee amount ('0' for Normal).

In the properties of normal RadioButton, set its 'Checked' property as 'True' so that by default it gets selected.

Tag Property
The Tag property is same as 'Text' property used for associating same user defined data to a control.  But the difference is 'Tag' is not visible to the user like as 'Text' does.

Now we will see how to define a 'CheckedChanged' event for one checkbox and bind the event procedure with all the remaining checkboxes.

Also, how to define a 'CheckedChanged' event for one RadioButton and bind the event procedure with all the other RadioButtons.

Now write the below code.

Declarations:
int count=0;

CheckBoxes CheckedChanged event:

radioButton1.Checked=true;
int amt=int.Parse(txtFees.Text);
checkBox cb=sender as CheckBox;
if(cb.Checked)
{
count +=1;
amt +=Convert.ToInt32(cb.Tag);
}
else
{
count -=1;
amt -=convert.ToInt32(cb.Tag);
}
txtFees.Text=amt.ToString();

RadioButton CheckedChanged Event:

int amt=int.Parse(txtFees.Text);
if(amt>0)
{
RadioButton rb=sender as RadioButton;
if(rb.checked)
amt +=(Convet.ToInt32(rb.Tag)*count);
txtFees.Text=amt.ToString();
}
else
radioButton1.Checked=true;

Clear Button Click:
foreach (Control ctrl in groupBox1.controls)
{
if (ctrl.GetType().Name=="CheckBox")
{
checkBox cb=ctrl as CheckBox;
cb.Checked=false;
}
}

Close Form Button:
this.Close();

ComboBox, ListBox, CheckedListBox

The ComboBox, ListBox, and CheckedListBox controls are used basically for providing the users to select from a list of available values.  We will see some basic detail on each mentioned control.

ComboBox allows single selection only and it is like a combination of TextBox and DropDownList, which will allow to either select from available list of values or enter a new value.

By default, ListBox allows only single selection, but it can be changed to multi-selection as well.

By default, CheckedListBox allows multi-selection.

How to add values to the controls
We can add values to the above-mentioned three controls in four different mechanisms.

1) Go to the items collection property of the control  and click on the button beside it, which will open a window, then enter required values each in a new line.

2) Using "Items.Add" method of the control we can add a new item to the control one at a time.

<control>.Items.Add(object value)

3) Using "Items.AddRange" method of the control, we can add an array of values at a time to the control.

<control>.Items.AddRange(objedt[ ] values)

4) Using the "DataSource" property of the control, we can bind a table to the control so that all the records of the table gets added to the control, but we can display only one column of the table, which should be specified explicitly by using the "DisplayMember" property.

<control>.DataSource=<data table>
<control>.DisplayMember=<cd name>

Multiple selection for ListBox
By default ListBox allows only single selection.  If we want multi-selection for ListBox, first of all we need to set the 'SelectionMode' property of the control to either 'MultiSimple' Or 'MultiExtended'.

SelectionMode
-None
-One (default)
-MultiSimple [Only Mouse click]
-MultiExtended [Ctrl+ Mouse click]

How to find the Items selected under the controls
We can find the item or index of the item which is selected using the below properties.

ComboBox:
Text String
SelectedItem Object
SelectedIndex Int

ListBox:
SelectedItem Object
SelectedIndex Int
Selecteditems Object [ ]
SelectedIndices Int [ ]

CheckedListBox:
CheckedItems object [ ]
CheckedIndices Int [ ]


Go to the items collection property of 'ComboBox' and add a list of countries in the window open.

To add values in the 'ComboBox' dynamically in runtime, write the below code under 'KeyPress' event of the 'ComboBox'.

//MessageBox.Show (Convert.ToInt32 (e.KeyChar).ToString ());
if(Convert.ToInt32(e.KeyChar)==13)
if(ComboBox1.FindString(ComboBox1.Text)== -1)
ComboBox1.Items.Add(ComboBox1.Text)

Form Load:
listBox1.Items.Add("AP");
listBox1.Items.Add("UP");
listBox1.Items.Add("Karnataka");
listBox1.Items.Add("Tamilanadu");
listBox1.Items.Add("Maharashtra");
string[ ] cities={"Hyderabad","Lucknow","Banglore","Chennai","Mumbai"};
checkedListBox1.Items.Add(cities);

Show Selected Country Button:
MessageBox.Show(comboBox1.Text);
MessageBox.Show(comboBox1.SelectedItem.ToString());
MessageBox.Show(comboBox1.SelectedIndex.ToString());

Show Selected States Button:
foreach(object obj in listBox1.SelectedItems)
MessageBox.Show(obj.ToString());

Show Selected Cites Button:
string str=null;
foreach(object obj in checkedListBox1.checkedItemes)
str +=obj.ToString()+ ",";
str =str.ShowString(0,str.Length-2);
int pos=str.LastIndexOf(",");
if(pos !=-1)
{
str=str.Remove(pos,1);
str=str.  Insert(pos,"and");
}
MessageBox.Show (str);

SelectedIndexChanged
SelectedIndexChanged is the default event of all the above-mentioned three controls, which will get raised or occur whenever an item is selected from them.


Go to the items collection property of First ComboBox and add a list of values in it like Quarter1, Quarter2, Quarter3, Quarter4 and write the below code.

Declarations:
string [ ] q1={"January","February","March"};
string [ ] q2={"April","May","June"};
string [ ] q3={"July","August","September"};
string [ ] q4={"October","November","December"};

ComboBox1 SelectedIndexChanged:
comboBox2.Items.Clear();
switch(comboBox1.SelectedIndex)
{
case 0:
comboBox2.Items.AddRange(q1);
break;
case 1:
comboBox2.Items.AddRange(q2);
break;

case 2:

comboBox2.Items.AddRange(q3);
break;
case 3:
comboBox2.Items.AddRange(q4);
break;
}

Happy Learning….!!!!!



This post first appeared on Dot Net Programming (C#, Asp.Net, ADO.Net, WCF, WPF, Ajax, LINQ), please read the originial post: here

Share the post

CSharp.NET Tutorial – Day 18

×

Subscribe to Dot Net Programming (c#, Asp.net, Ado.net, Wcf, Wpf, Ajax, Linq)

Get updates delivered right to your inbox!

Thank you for your subscription

×