1

I want to add a handler to a control that i find by its name. The Problem is that it is not possible to Dim an control from a Button or RadioButton or something like this...

Dim control As Control = FindName(MyObject.Name.ToString)
            AddHandler control.MouseEnter, Sub()
                                               Try
                                                   Dim Tooltip As New ToolTip()
                                                   Tooltip.SetToolTip(control, control.Name.ToString)
                                               Catch

                                               End Try

                                           End Sub

In the code i could dim control as Button but then e.g. RadioButtons would not work. And I do not want to have a code that always checks for the ObjectType and then go into an if part like

If TypeName(MyObject).ToString = "Button" then
...
else if TypeName(MyObject).ToString = "Label" then
...
else if TypeName(MyObject).ToString = "RadioButton" then
...
End If

Is there a better solution then doing it this way?

E.g. something like

Dim Control as TypeName(MyObject).ToString = FindName(MyObject.Name.ToString)
  • Think you need to expand a little bit as I'm trying to understand what `MyObject` is. Is it something you have declared or are you looping through controls? – Bugs Oct 28 '16 at 10:28
  • I am looping through controls and get their children until no child is left - then go to the next one.. and for each child I want to try to add this Handler – LM_DMKHNGST Oct 28 '16 at 10:30
  • Think then you just need to reference the control. MouseEnter is pretty standard for controls. In your loop `For Each ctr As Control` place the code `AddHandler ctr.MouseEnter`. Do the same for the child controls. It looks like you're already doing this or close to it. Might need to include more code. – Bugs Oct 28 '16 at 10:37

1 Answers1

1

Is this what you're after?

For Each ctr As Control In Me.Controls
    AddHandler ctr.MouseEnter, Sub()
                                   Try
                                       Dim Tooltip As New ToolTip()
                                       Tooltip.SetToolTip(ctr, ctr.Name.ToString)
                                   Catch

                                   End Try
                               End Sub
Next

You would need to expand on this to each child if you're looping through parent objects like panels etc but the concept should work.

EDIT:

This should work for any control that has children:

Declare this at the top of your form/class:

Private _controls As New List(Of Control)

Use this to add the handler:

For Each ctr As Control In Me.Controls
    AddHandler ctr.MouseEnter, Sub()
                                   Try
                                       Dim Tooltip As New ToolTip()
                                       Tooltip.SetToolTip(ctr, ctr.Name.ToString)
                                   Catch

                                   End Try
                               End Sub
    If ctr.HasChildren Then
        _controls = New List(Of Control)
        GetChildren(ctr)
        For Each childCtr As Control In _controls
            AddHandler childCtr.MouseEnter, Sub()
                                                Try
                                                    Dim Tooltip As New ToolTip()
                                                    Tooltip.SetToolTip(childCtr, childCtr.Name.ToString)
                                                Catch

                                                End Try
                                            End Sub
        Next
    End If
Next

And something like this will populate the _controls list with the child controls:

Private Sub GetChildren(ByVal container As Control)

    For Each childCtr As Control In container.Controls
        _controls.Add(childCtr)
        If childCtr.HasChildren Then
            GetChildren(childCtr)
        End If
    Next

End Sub
Bugs
  • 4,491
  • 9
  • 32
  • 41