Resetting alll controls on a form

G

Guest

Hi Everyone.
I was using the following code in VB6, but am in the middle of converting
the app to VB.Net 2005. He is the code
Private Sub ClearAll()
On Error GoTo Error_Routine
Call appLogger("ClearAll", "FrmPatientInfo")
Dim lcol As Control
isLoading = True
For Each lcol In Me.Controls
If TypeOf lcol Is TextBox Then
lcol.Text = ""
lcol.Tag = ""
ElseIf TypeOf lcol Is Image Then
lcol.Picture = LoadPicture(DefaultImage, vbLPCustom, vbLPDefault)
lcol.Tag = ""
ElseIf TypeOf lcol Is TDBMask Then
lcol.Text = ""
lcol.Tag = ""
ElseIf TypeOf lcol Is OptionButton Then
lcol.Value = False
lcol.Tag = ""
ElseIf TypeOf lcol Is TDBCombo Then
lcol.Text = ""
lcol.Tag = ""
ElseIf TypeOf lcol Is CheckBox Then
lcol.Value = 0
lcol.Tag = ""
ElseIf TypeOf lcol Is TDBDate Then
lcol.Value = Null
lcol.Tag = ""
ElseIf TypeOf lcol Is Label Then
If lcol.BackColor = &HFFF3E8 Then
lcol.Caption = ""
lcol.Tag = ""
End If
ElseIf TypeOf lcol Is ComboBox Then
lcol.Text = ""
lcol.Listindex = -1
lcol.Tag = ""
ElseIf TypeOf lcol Is CheckBox Then
End If
Next
isLoading = False
imgPatient.Tag = ""
Exit_Routine:
Exit Sub
Error_Routine:
Debug.Assert False
Errorlog "frmPatientInfo.ClearAll"
End Sub

I'd like it to clear the controls depending on the control, as you can see
above. I'm getting errors in VB.net that a property is not a member of
Control. Oneone got some suggestions. Thanks.
Michael Lee
 
G

Guest

That is because Option Strict is on by default in VB.NET.

Either turn Option Strict Off, easier, but not recommended solution.

Or, Cast ICol to the correct type, ie. TextBox etc. (using DirectCast or
other methods) before resetting the properties for each control.
 
P

Phill W.

Michael said:
Dim lcol As Control
For Each lcol In Me.Controls
If TypeOf lcol Is TextBox Then
lcol.Text = ""
lcol.Tag = ""
ElseIf TypeOf lcol Is Image Then
lcol.Picture = LoadPicture(DefaultImage, vbLPCustom, vbLPDefault)
lcol.Tag = ""

You're /so/ nearly there.
Having asked the Control what Type it is, you just have to convince the
compiler that it should treat the given Control in that specific way.
DirectCast is quickest (AFAIK):

For Each col As Control _
In Me.Controls
If TypeOf col Is TextBox Then
With DirectCast( col, TextBox )
.Text = String.Empty
.Tag = String.Empty
End With

ElseIf TypeOf col Is Image Then
With DirectCast( col, Image )
.Picture = LoadPicture(DefaultImage, vbLPCustom, vbLPDefault)
.Tag = String.Empty
End With
. . .

IIRC, the Control Type defines the Tag property, so you could set this
one /outside/ the TypeOf checks.

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top