III.4- Using OptionButton
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.
Source Code:
private void
rbtRed_CheckedChanged(object sender, EventArgs e)
{
if (rbtRed.Checked ==
true)
lblColor.ForeColor = Color.Red;
}
private void
rbtBlue_CheckedChanged(object sender, EventArgs e)
{
if (rbtBlue.Checked
== true)
lblColor.ForeColor = Color.Blue;
}
private void
rbtYellow_CheckedChanged(object sender, EventArgs e)
{
if (rbtYellow.Checked
== true)
lblColor.ForeColor = Color.Yellow;
}
}
III.5- Using ListView Control
The ListView control is an ItemsControl that is derived from ListBox
private void
btnAdd_Click(object sender, EventArgs e) { //Declaring
list item & add to listitem ListViewItem
IstIem = listView1.Items.Add(txtID.Text); //Add Sub item
IstIem.SubItems.Add(txtName.Text);
IstIem.SubItems.Add(cboSex.Text); clearText(); } private void
btnEdit_Click(object sender, EventArgs e) { int i
= listView1.SelectedIndices[0]; listView1.Items[i].Text =
txtID.Text;
listView1.Items[i].SubItems[1].Text = txtName.Text;
listView1.Items[i].SubItems[2].Text = cboSex.Text; clearText(); } private void
btnDelete_Click(object sender, EventArgs e) { int i
= listView1.SelectedIndices[0]; listView1.Items.RemoveAt(i); } private void
listView1_SelectedIndexChanged(object
sender, EventArgs e) { if
(listView1.SelectedIndices.Count == 0) { MessageBox.Show("Double
click item to edit"); return; } int i
= listView1.SelectedIndices[0]; txtID.Text =
listView1.Items[i].Text; txtName.Text =
listView1.Items[i].SubItems[1].Text; cboSex.Text =
listView1.Items[i].SubItems[2].Text; } //Clear text after save private void
clearText() { txtID.Clear(); txtName.Clear(); cboSex.SelectedIndex = -1; } |
0 Comments