i need a logic or code on how to store multiple checkbox values(t or f) into combobox by clicking a button, then retrieving the chechbox values when the selectedindex is changed ...
any ideas?... thank you
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
del etePosted Jun 16, 2007, 9:08 PM
thak u :)
AlanPosted Jun 16, 2007, 9:04 AM
I take it this is related to your earlier question on this forum.
If the combobox has 5 entries, say, then create a two dimensional boolean array at class level:
private bool[,] cbSettings = new bool[5, 10];
If your combobox is called comboBox1 and your 10 checkboxes have the default names of checkBox1 to checkBox10, place this code (which assumes you have .NET 2.0) in the button click eventhandler:
int currentIndex = comboBox1.SelectedIndex;
if (currentIndex > -1)
{
for(int i = 0; i < 10; i++)
{
CheckBox cb = (CheckBox)this.Controls["checkBox" + (i + 1).ToString()];
cbSettings[currentIndex, i] = cb.Checked;
}
}
Now place this code in the combobox's SelectedIndexChanged eventhandler:
int currentIndex = comboBox1.SelectedIndex;
if (currentIndex > -1)
{
for(int i = 0; i < 10; i++)
{
CheckBox cb = (CheckBox)this.Controls["checkBox" + (i + 1).ToString()];
cb.Checked = cbSettings[currentIndex, i];
}
}
If the checkBoxes are not directly on the form but in a different container (such as a Groupbox), then replace 'this' in the above code with a reference to the container.
This code is untested so watch out for bugs!