I have a form that creates a row of checkboxes depending on the type of analysis the user wants to perform on the incoming file. I need to be able to determine if the checkbox is "checked" but I don't know how to do that when the controls are created at runtime.

So right now I have a loop going through all the controls on the form and selecting the appropriate case: for instance chkbox1, combobox1, etc. Now I can access the values of the control by using

Me.Controls.Item(chkbox1)

but after that the only thing resemebling checked is .Text which wouldn't be accurate.


So my question is: how do you workaround the .Checked property of a checkbox when it is created at runtime?

You have to cast the items to the checkbox type in order to access its abilities.

Use:

CType(Me.Controls.Item(chkbox1),CheckBox)

or

DirectCast(Me.Controls.Item(chkbox1),CheckBox)

or

TryCast(Me.Controls.Item(chkbox1),CheckBox)

If you have other controls, then you need to do a check before you cast:

For each ctrl as control in me.controls
if TypeOf(ctrl) is CheckBox then
'case and do work here
end if
next

Note: Aircoded.

Hope this helps.

Thank you. I just used the first one and it worked like a charm. In the future I'll use what you posted last to make it uniform.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.