Disable multiple fields

  • Thread starter Thread starter Dan Shepherd
  • Start date Start date
D

Dan Shepherd

Is there a way to disable a number of fields (50+) using Visual Basic 2005?
I have a window that used an imported control in vb 6 but now relies on .Net
control.. The old logic was:

Dim mycontrol As SWIMAPI.SIVControl
If RTrim(bXUNAPPRPROF.uprofileid) <> "" Then
For Each mycontrol In SWIMAPIVbaObjectModel_definst.sivMyApp.Controls
If mycontrol.Properties("controltype").value = "SAFMaskedText" Or
mycontrol.Properties("controltype").value = "SAFCombo" Or
mycontrol.Properties("controltype").value = "SAFDate" Or
mycontrol.Properties("controltype").value = "SAFMaskedFloat" Then
If mycontrol.Properties("name").value <> "cprofilereference" Then
mycontrol.Properties("enabled").value = False
End If
End If
Next mycontrol
Else
For Each mycontrol In SWIMAPIVbaObjectModel_definst.sivMyApp.Controls
If mycontrol.Properties("controltype").value = "SAFMaskedText" Or
mycontrol.Properties("controltype").value = "SAFCombo" Or
mycontrol.Properties("controltype").value = "SAFDate" Or
mycontrol.Properties("controltype").value = "SAFMaskedFloat" Then
If mycontrol.Properties("name").value <> "cuprofileid" And
VB.Right(RTrim(mycontrol.Properties("name").value), 3) <> "qty" And
mycontrol.Properties("name").value <> "cuapprovstatus" And
mycontrol.Properties("name").value <> "cuapprovalnumber" And
mycontrol.Properties("name").value <> "cuapprovaldate" And
mycontrol.Properties("name").value <> "cuapproveexpiry" Then
mycontrol.Properties("enabled").value = True
End If
End If
Next mycontrol
End If

How would I rewrite this in VB 2005?
 
You looking for something like this:

here is a link where i ripped the code for you:
http://video.techrepublic.com.com/5100-10878_11-6157618.html

Private Sub SetControls()

Dim cControl As Control
For Each cControl In Me.Controls
If (TypeOf cControl Is TextBox) Then
cControl.Text = "abc"
cControl.enabled = false
End If
Next cControl

End Sub

You can loop through all your controls with me.controls.
Perhaps make a list of your parameters ( control type ) and you can do it
all in one shot.
 
Miro:

Thanks for pointing me in the right direction.. basically what I did to fix
it is I used the Dim cControl and for each cControl in Me.Controls line..
then I substituted Interop.SAF.SAFMaskedText, etc

Dim cControl as Control
For Each cControl in me.Controls
If (TypeOf cControl Is Interop.SAF.SAFMaskedText or TypeOf cControl is
Interop.SAF.SAFDate) Then
cControl.enabled = False
End If

Else...
 
Miro:

Thanks for pointing me in the right direction.. basically what I did to fix
it is I used the Dim cControl and for each cControl in Me.Controls line..
then I substituted Interop.SAF.SAFMaskedText, etc

Dim cControl as Control
For Each cControl in me.Controls
  If (TypeOf cControl Is Interop.SAF.SAFMaskedText or TypeOf cControl is
Interop.SAF.SAFDate) Then
cControl.enabled = False
End If

Else...

Please note you should be using "OrElse" instead of "Or" to allow
short-circuiting the if checks (so you stop once you find a match
instead of checking every branch).

Also, if you were able to upgrade to VS 2008 then you could do this
with a simple LINQ query.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Seth:

Great tip.. Thank you.

rowe_newsgroups said:
Please note you should be using "OrElse" instead of "Or" to allow
short-circuiting the if checks (so you stop once you find a match
instead of checking every branch).

Also, if you were able to upgrade to VS 2008 then you could do this
with a simple LINQ query.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Back
Top