More Efficient Code
-
The current program I am working on has a lot of user Checkboxs and currently when ever I change a property I end up coding something like this: CheckBox1->Enabled=true; CheckBox2->Enabled=true; ... CheckBox30->Enabled=true; this is obviously not a very efficient way of doing this... so I'm looking for a more efficient way... maybe something along the lines of for(int c=0;c!=31;c++) { CheckBox(c)->Enabled=true; } this code will obviously not work but any suggestions on how to make something like this work??? :~ Cheers* Debs
-
The current program I am working on has a lot of user Checkboxs and currently when ever I change a property I end up coding something like this: CheckBox1->Enabled=true; CheckBox2->Enabled=true; ... CheckBox30->Enabled=true; this is obviously not a very efficient way of doing this... so I'm looking for a more efficient way... maybe something along the lines of for(int c=0;c!=31;c++) { CheckBox(c)->Enabled=true; } this code will obviously not work but any suggestions on how to make something like this work??? :~ Cheers* Debs
If all of your checkboxes are located in a container control (panel, groupbox) or if they are the only controls on your form then you could use a foreach statement. foreach(Control aControl in form1.controls) { CheckBox aCheckBox = (CheckBox) aControl; aCheckBox.Enabled = true; } I did not test the above code. It's just off the top of my head. You could also create and arraylist of checkboxes and interate through then the same way. www.lovethosetrains.com -- modified at 13:10 Thursday 22nd September, 2005
-
The current program I am working on has a lot of user Checkboxs and currently when ever I change a property I end up coding something like this: CheckBox1->Enabled=true; CheckBox2->Enabled=true; ... CheckBox30->Enabled=true; this is obviously not a very efficient way of doing this... so I'm looking for a more efficient way... maybe something along the lines of for(int c=0;c!=31;c++) { CheckBox(c)->Enabled=true; } this code will obviously not work but any suggestions on how to make something like this work??? :~ Cheers* Debs
You could put all CheckBoxes into an array and iterate over it:
//instance member
CheckBox[] _allCheckBoxes;//somewhere once all CheckBoxes are created
_allCheckBoxes = new CheckBox[31];
_allCheckBoxes[0] = CheckBox1;
_allCheckBoxes[1] = CheckBox2;
//...//then if you have to change all of them
foreach (CheckBox cb in _allCheckBoxes)
cb.Enabled = true; -
You could put all CheckBoxes into an array and iterate over it:
//instance member
CheckBox[] _allCheckBoxes;//somewhere once all CheckBoxes are created
_allCheckBoxes = new CheckBox[31];
_allCheckBoxes[0] = CheckBox1;
_allCheckBoxes[1] = CheckBox2;
//...//then if you have to change all of them
foreach (CheckBox cb in _allCheckBoxes)
cb.Enabled = true;Are you sure thats possible? When I try to do it it wont work, it give's me A LOT of errors and all kinds of warnings. Niklas Ulvinge aka IDK
-
Are you sure thats possible? When I try to do it it wont work, it give's me A LOT of errors and all kinds of warnings. Niklas Ulvinge aka IDK
Well... yes Im sure :). If you want I can mail you a small sample.
-
Well... yes Im sure :). If you want I can mail you a small sample.
-
Well... yes Im sure :). If you want I can mail you a small sample.
Is it possible to do it with menuitems? And sure, mail the sample to me... My email is: ulvinge@gmail.com Niklas Ulvinge aka IDK
-
The current program I am working on has a lot of user Checkboxs and currently when ever I change a property I end up coding something like this: CheckBox1->Enabled=true; CheckBox2->Enabled=true; ... CheckBox30->Enabled=true; this is obviously not a very efficient way of doing this... so I'm looking for a more efficient way... maybe something along the lines of for(int c=0;c!=31;c++) { CheckBox(c)->Enabled=true; } this code will obviously not work but any suggestions on how to make something like this work??? :~ Cheers* Debs
Thanks for all the suggestions I managed to do do it by the following::-D TCheckBox *ChkBx[34]; ChkBx[0]=CheckBox1; //Declaring them all once into an array ChkBx[1]=CheckBox2; ... ChkBx[33]=CheckBox33; for(int i=0;i!=4;i++) { ChkBx[i]->Checked=true; } I also used this concept with Graph Line Series ie. TChartSeries *Ser[4]; ... Thanks Again Debs -- modified at 13:11 Friday 23rd September, 2005