From: Swansea Uni Psychology Techs on
Hi all,

I'm writing a VB2008 program that contains a number of different controls on
a form. However, I need to reset the value of two controls (a textbox and a
checkbox). The essential part of the code is below. The problem is that if I
declare the ctrl variable, I get a compiler error indicating that the ctrl
doesn't have the checked property, even though the code compiles and runs
fine if I don't declare the variable. I'd do things differently if I could
use control arrays a la vb6, and I know why this error is occurring, but am
unsure how to resolve it.

Can anyone suggest a better way to code this:

Dim ctrl As Control ' remove line to prevent compile error
For Each ctrl In Me.Controls
If TypeOf (ctrl) Is TextBox Then
ctrl.text = ""
Else
If TypeOf (ctrl) Is CheckBox Then
ctrl.checked = False
End If
End If
Next

Cheers.


From: Ralph on

"Swansea Uni Psychology Techs" <pstech(a)swansea.ac.uk> wrote in message
news:h5uj54$5je$1(a)south.jnrs.ja.net...
> Hi all,
>
> I'm writing a VB2008 program that contains a number of different controls
on
> a form. However, I need to reset the value of two controls (a textbox and
a
> checkbox). The essential part of the code is below. The problem is that if
I
> declare the ctrl variable, I get a compiler error indicating that the ctrl
> doesn't have the checked property, even though the code compiles and runs
> fine if I don't declare the variable. I'd do things differently if I could
> use control arrays a la vb6, and I know why this error is occurring, but
am
> unsure how to resolve it.
>
> Can anyone suggest a better way to code this:
>
> Dim ctrl As Control ' remove line to prevent compile error
> For Each ctrl In Me.Controls
> If TypeOf (ctrl) Is TextBox Then
> ctrl.text = ""
> Else
> If TypeOf (ctrl) Is CheckBox Then
> ctrl.checked = False
> End If
> End If
> Next
>

Try ...
Dim c As CheckBox
...
c = CType(c, CheckBox)
c.Checked = False



 | 
Pages: 1
Prev: Windows 7 right to left support
Next: API-Guide