• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Simple Visual Basic Question

AtlantaBob

Golden Member
So I want to loop through the controls collection in a visual basic form, and, if the control is of type "checkbox" then I want it to be unchecked and disabled.

Pseudo code (and very visual basic like):

1 dim obj as control
2 for each obj in Controls
3 if obj is of type checkbox then
4 obj.checkstate = checkstate.unchecked
5 obj.enabled = false
6 else
7 end if

What's the VB syntax for line 3? I can't seem to find it in help or google.

Thanks.

 
I have something similar to what you're looking for, but it's in C# and it's for webcontrols. This may give you an idea though.
 
MajorMullet,

Thanks for the code! Sadly, I just can't figure out what the $*(# syntax is for this in Visual Basic. I suppose that's another good reason to learn C#, though!

 
Thanks guys. I got the "If typeof obj is checkbo"x to work... however, new problem has come up. Because obj is a control, VB won't let me address the obj.checkstate property. (the next line; objects of type control don't have a checkstate property).

How's the best way to get around this? Have a entirely new collection come up that is made up of only things that meet the "TypeOf... is CheckBox", and then iterate through that?

It seems like there's got to be a better way.
 
beggerking,

Sorry, but I'm not sure how to do that one... (I'm looking in the documentation now).
Obviously, I have to do it after I test for it being a checkbox, right?

Thanks,
 
Argh. I really am a beginner at this.

CType(obj, CheckBox) gives me the issue "Expression is not a method"
if I change it to obj = CType(obj, CheckBox), I don't get that issue anymore, but the change doesn't seem to take hold, if you will--the program still tells me that "checkstate is not a member of system.windows.forms.control."
 
Beggerking,

Saw that you edited that. The first thing you suggested -- use ctype(obj, checkbox).checkstate = checkstate.unchecked, got around the compiler's griping, but then I got a stack overflow message: (original code below):

Dim obj As New Control
For Each obj In Controls
If TypeOf obj Is CheckBox Then
CType(obj, CheckBox).CheckState = CheckState.Unchecked
obj.Enabled = False
Else
End If
Next

As for the latest, did you mean obj = ctype(obj, checkbox) ? I've tried that before, and that's the change does doesn't seem to "stick"

Thanks again!
 
MajorMullet posted approximately the answer I think:
CType(control, CheckBox).CheckState = CheckState.Unchecked

The reason "obj = CType(obj, CheckBox)" doesn't work is because the obj variable is still of the type Control even if the instance is a CheckBox.

Edit: beaten 😛
 
Originally posted by: kamper
MajorMullet posted approximately the answer I think:
CType(control, CheckBox).CheckState = CheckState.Unchecked

The reason "obj = CType(obj, CheckBox)" doesn't work is because the obj variable is still of the type Control even if the instance is a CheckBox.

Edit: beaten 😛

Argh, my Apologies to MajorMullet. Somehow I glanced over that...

Now does anyone have any advice for the stack overflow? 🙂
 
Argh,
Nevermind, I'm an idiot. I see now that the event the sub handled was "Checkstate Changed" so of course the program would make an infinite loop. Everytime I ran it, it would call itself recursively...
Dolt!

I've got it straightened out now...

Thanks again everyone.


 
Back
Top