Grouping controls logically

brett_eden

New member
Joined
Jun 15, 2003
Messages
2
Location
Perth, Australia
In the interest of keeping my code compact and easy to maintain, I need some advice on how to group controls (in this case, checkboxes) and change their state without having to make a specific reference to each control. :D

I have:

chkMelbourne
chkPerth
chkAdelaide
chkBrisbane
chkHobart
chkDarwin
chkSydney

I need to alter their Enabled property based on the state of other checkboxes in my application, but would rather reference them in a group as opposed to individually. Some are enabled, some arent. The longhand way (if constructs and select case clauses)are making the code far too verbose. :-(

I suspect that adding these controls to a collection and then iterating through them is the better solution, but I do not know how.

Suggestions and/or sample code much appreciated.

Regards,
B.E.
 
How about some of your existing code. It is not really clear as to the circumstances that you want these check boxes to be disabled.
 
Some example code. Sure. Heres a snippet from another program I am writing.

If myCharacter.Strength < 6 Or myCharacter.Constitution < 8 Or _
myCharacter.Intelligence < 6 Then
If chkGnome.Enabled = True Then
chkGnome.Checked = False
chkGnome.Enabled = False
ElseIf chkGnome.Enabled = False Then
End If
Else : chkGnome.Enabled = True
End If Minimum ability scores for Gnome

If myCharacter.Dexterity < 6 Or myCharacter.Constitution < 6 Or _
myCharacter.Intelligence < 4 Then
If chkHalfElf.Enabled = True Then
chkHalfElf.Checked = False
chkHalfElf.Enabled = False
ElseIf chkHalfElf.Enabled = False Then
End If
Else : chkHalfElf.Enabled = True
End If Minimum ability scores for Half Elf

If myCharacter.Strength < 7 Or myCharacter.Dexterity < 7 Or _
myCharacter.Constitution < 10 Or myCharacter.Intelligence < 6 Then
If chkHalfling.Enabled = True Then
chkHalfling.Checked = False
chkHalfling.Enabled = False
ElseIf chkHalfling.Enabled = False Then
End If
Else : chkHalfling.Enabled = True
End If Minimum ability scores for Halfling

There are many similar pieces of code right throughout the program, doing various tests and then checking checkboxes or enabling/disabling controls. This way seems terribly verbose :(

Regards,
B.E.
 
Well, the thing is the code is as clean as it can be since you arent using the same properties every time. Although it seems a little wordy, the structure of it functions well. You can take out the second else if in the middle IF statement though. it is not needed.

If myCharacter.Strength < 7 Or myCharacter.Dexterity < 7 Or _
myCharacter.Constitution < 10 Or myCharacter.Intelligence < 6 Then

If chkHalfling.Enabled = True Then
chkHalfling.Checked = False
chkHalfling.Enabled = False
End If

Else

chkHalfling.Enabled = True

End If Minimum ability scores for Halfling
 
You could make a reusable method that handles your checkboxes since they all seem to fit the same pattern for enabling/disabling. That way you you can just call a method and pass the checkbox to it. Something like this;

C#:
// Sets chk.checked and chk.enabled to opposite of bool value
// if chk.enabled == bool value.
private void _reverseChk(Checkbox chk, bool enabled) {
   if (chk.Enabled == enabled) {
      chk.Checked = !(enabled);
      chk.Enabled = !(enabled);
   }
}

Then you can replace your code with;

C#:
if (myCharacter.Strength < 6 || myCharacter.Constitution < 8 ||
myCharacter.Intelligence < 6) 
{
   _reverseChk(chkGnome, true);
}
else {
   chkGnome.Enabled = true;
}

if (myCharacter.Dexterity < 6 || myCharacter.Constitution < 6 ||
myCharacter.Intelligence < 4)
{
   _reverseChk(chkHalfElf, true);
}
else {
   chkHalfElf.Enabled = true;
}

if (myCharacter.Strength < 7 || myCharacter.Dexterity < 7 || 
myCharacter.Constitution < 10 || myCharacter.Intelligence < 6)
{
   _reverseChk(chkHalfling, true);
} 
else {
   chkHalfling.Enabled = true;
}

Btw.. D&D :cool:
 
Last edited by a moderator:
Back
Top