Can I create a handler against a windows contol class?

  • Thread starter Thread starter Graham Blandford
  • Start date Start date
G

Graham Blandford

Hi guys,

I'm sure I'm just not thinking far enough outside the box - am a VB6er
trying to enter this .NET OOP world.....

This is what I have...

For Each c As Control In Me.Controls
If c.GetType.ToString = "System.Windows.Forms.TextBox" Then
If c.Enabled = False Then
c.BackColor = System.Drawing.Color.White
End If
ElseIf c.GetType.ToString = "System.Windows.Forms.GroupBox" Then
For Each gc As Control In c.Controls
If gc.GetType.ToString = "System.Windows.Forms.TextBox" Then
If gc.Enabled = False Then
gc.BackColor = System.Drawing.Color.White
End If
End If
Next
End If
Next

As you an see, I am trying to change the colors for any textbox (inclouding
those sittiing in other controls) that is disabled - not very OO - in VB6 I
would probably have created a control array and done something like 'For
each Textbox as tb in Me.txtarray'....

However, I can see some really good possibilities in here, in that true OO
woild be to do this upon a event - such as painting/disabling the control.

Is there a more graceful way of doing this? Maybe in a handler?

Any ideas would be appreciated.

Thanks,
Graham
 
Hi Graham,

Setting all textboxes.text to space on a form and all controlboxes index
to -1

It is not important where they are,
Watch the start with doclean(me) that is the top of the form.

I hope this helps?

Cor
\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
doclean(Me)
End Sub
Private Sub doclean(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
If TypeOf ctr Is TextBox Then
ctr.Text = ""
ElseIf TypeOf ctr Is ComboBox Then
DirectCast(ctr, ComboBox).SelectedIndex = -1
End If
doclean(ctr)
Next
End Sub
///
 
* "Graham Blandford said:
I'm sure I'm just not thinking far enough outside the box - am a VB6er
trying to enter this .NET OOP world.....

This is what I have...

For Each c As Control In Me.Controls
If c.GetType.ToString = "System.Windows.Forms.TextBox" Then
If c.Enabled = False Then
c.BackColor = System.Drawing.Color.White
End If
ElseIf c.GetType.ToString = "System.Windows.Forms.GroupBox" Then
For Each gc As Control In c.Controls
If gc.GetType.ToString = "System.Windows.Forms.TextBox" Then

\\\
If TypeOf gc Is TextBox Then
...
End If
///
If gc.Enabled = False Then
gc.BackColor = System.Drawing.Color.White
End If
End If
Next
End If
Next

As you an see, I am trying to change the colors for any textbox (inclouding
those sittiing in other controls) that is disabled - not very OO - in VB6 I
would probably have created a control array and done something like 'For
each Textbox as tb in Me.txtarray'....

Maybe this class helps you to simplify your code:

<URL:http://dotnet.mvps.org/dotnet/samples/controls/downloads/EnumerateControls.zip>
 
Back
Top