hi....
i have written following code which displays dynamic check boxes for X and Y co-ordinates...
now, i need help in adding some logic and functionality in them...
i want that the user can check only 1 check box for each co-ordinate i.e 1 for X axis and 1 for Y axis...
also if user select same check boxes for both co-ordinates i.e checkbox 2 for X and also check box 2 for Y error message should be displayed...
i also want to get the index number of checked box as an integer...
as i am new to the language em not getting these things rite...
help is required...
thankyou in advance...
my code is....
------------------------------------------------------------------------------------------------------------------------------------------------------------------
public partial class Form1 : Form
{
CheckBox[] CB2;
CheckBox[] CB;
string[] names = new string[] { "sepal length", "sepal width", "petal length", "petal width" };
int No_Columns;
public Form1()
{
InitializeComponent();
No_Columns=4;
CB=new CheckBox[No_Columns];
for (int i = 0; i < No_Columns; i++)
{
CB[i] = new CheckBox();
CB[i].Left = 20;
CB[i].Top = (i + 1) * 30;
CB[i].Name = "CB" + i.ToString();
CB[i].Text = names[i];//"CB" + i.ToString();
CB[i].CheckedChanged += new EventHandler(CheckedChanged);
// this.Controls.Add(CB[i]);
}
CB2=new CheckBox[No_Columns];
for (int i = 0; i < No_Columns; i++)
{
CB2[i]= new CheckBox();
CB2[i].Left = 190;
CB2[i].Top = (i + 1) * 30;
CB2[i].Name = "CB2" + i.ToString();
CB2[i].Text = names[i];//"CB2" + i.ToString();
CB2[i].CheckedChanged += new EventHandler(CheckedChanged2);
//this.Controls.Add(CB2[i]);
}
for (int i = 0; i < No_Columns; i++) {
this.Controls.Add(CB[i]);
this.Controls.Add(CB2[i]);
}
}
-------------------------- Event HAndlers-------------------------------
private void CheckedChanged(object sender, EventArgs e)
{
CheckBox button = (CheckBox)sender;
//System.Windows.Forms.MessageBox.Show(button.Text.ToString());
for (int i = 0; i < No_Columns; i++)
{
if ("CB" + i.ToString() == button.Text.ToString())
{
}
else
{
CB[i].Checked = false;
}
}
}
private void CheckedChanged2(object sender, EventArgs e)
{
CheckBox button = (CheckBox)sender;
//System.Windows.Forms.MessageBox.Show(button.Text.ToString());
for (int i = 0; i < No_Columns; i++)
{
if ("CB2" + i.ToString() == button.Text.ToString())
{
}
else
{
CB2[i].Checked = false;
}
}
//CheckBox button = (CheckBox)sender;
//button.Checked = true;
}
{
CheckBox[] CB2;
CheckBox[] CB;
string[] names = new string[] { "sepal length", "sepal width", "petal length", "petal width" };
int No_Columns;
public Form1()
{
InitializeComponent();
No_Columns=4;
CB=new CheckBox[No_Columns];
for (int i = 0; i < No_Columns; i++)
{
CB[i] = new CheckBox();
CB[i].Left = 20;
CB[i].Top = (i + 1) * 30;
CB[i].Name = "CB" + i.ToString();
CB[i].Text = names[i];//"CB" + i.ToString();
CB[i].CheckedChanged += new EventHandler(CheckedChanged);
// this.Controls.Add(CB[i]);
}
CB2=new CheckBox[No_Columns];
for (int i = 0; i < No_Columns; i++)
{
CB2[i]= new CheckBox();
CB2[i].Left = 190;
CB2[i].Top = (i + 1) * 30;
CB2[i].Name = "CB2" + i.ToString();
CB2[i].Text = names[i];//"CB2" + i.ToString();
CB2[i].CheckedChanged += new EventHandler(CheckedChanged2);
//this.Controls.Add(CB2[i]);
}
for (int i = 0; i < No_Columns; i++) {
this.Controls.Add(CB[i]);
this.Controls.Add(CB2[i]);
}
}
-------------------------- Event HAndlers-------------------------------
private void CheckedChanged(object sender, EventArgs e)
{
CheckBox button = (CheckBox)sender;
//System.Windows.Forms.MessageBox.Show(button.Text.ToString());
for (int i = 0; i < No_Columns; i++)
{
if ("CB" + i.ToString() == button.Text.ToString())
{
}
else
{
CB[i].Checked = false;
}
}
}
private void CheckedChanged2(object sender, EventArgs e)
{
CheckBox button = (CheckBox)sender;
//System.Windows.Forms.MessageBox.Show(button.Text.ToString());
for (int i = 0; i < No_Columns; i++)
{
if ("CB2" + i.ToString() == button.Text.ToString())
{
}
else
{
CB2[i].Checked = false;
}
}
//CheckBox button = (CheckBox)sender;
//button.Checked = true;
}
VulpesPosted Apr 12, 2014, 4:32 PM
kainat saleemPosted Apr 12, 2014, 3:17 PM
VulpesPosted Apr 6, 2014, 11:57 AM
We also know that this character is a digit, '0' to '9', and so its ASCII (or Unicode) value will be between 48 and 57.
When you subtract an integer from a character, the latter is first converted to its unicode equivalent.
So, button.Name[2] - 48 will return an integer between 0 and 9. In other words we're converting the chars '0' to '9' into actual numbers :)
kainat saleemPosted Apr 6, 2014, 11:43 AM
kainat saleemPosted Apr 5, 2014, 10:45 AM
VulpesPosted Apr 4, 2014, 6:50 AM
Anyway, try this code. It's not straightforward to program this because if you need to change a CheckBox's checked status because there's already another CheckBox checked on that axis or the corresponding CheckBox on the other axis is already checked, then it causes the CheckedChanged event to be immediately fired again! You therefore need to deal with the event handler being re-entered in this way: