2016-10-28 2 views
1

이름으로 찾은 컨트롤에 처리기를 추가하려고합니다. 대한 문제는 그 버튼이나의 RadioButton 또는 같은 것을에서 디밍 제어 불가능하다는 내가 예컨대 다음 버튼 등의 제어를 흐리게 할 수도 있지만 코드의문자열에 의한 조광 제어 및 처리기 추가

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 
이다

RadioButtons는 작동하지 않습니다. 그리고 난 항상 개체 유형 검사하는 코드를 갖고 싶어하지 않으며 이런 식으로 일을

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

더 나은 솔루션은 다음 거기에

같은 경우 부분으로 이동 한 후합니까?

예. 예 :

Dim Control as TypeName(MyObject).ToString = FindName(MyObject.Name.ToString) 
+0

내가 MyObject'이 무엇인지 '이해하려고 노력 중이 야 당신이 조금 확대 할 필요가 있다고 생각이 같은 것은 자식 컨트롤로 _controls 목록을 채 웁니다. 귀하가 선언했거나 통제를 통해 반복하고 있습니까? – Bugs

+0

컨트롤을 루핑하고 자식이 남지 않을 때까지 자식을 가져온다 - 다음 자식으로 이동한다 .. 그리고 각 자식에 대해이 핸들러를 추가하려고 시도한다 –

+0

컨트롤을 참조해야한다고 생각하면된다. MouseEnter는 컨트롤을위한 꽤 표준입니다. 'For Each ctr As Control' 루프에서'AddHandler ctr.MouseEnter' 코드를 삽입하십시오. 자식 컨트롤에 대해서도 동일한 작업을 수행하십시오. 당신이 이미이 일을하고있는 것처럼 보입니다. 더 많은 코드를 포함해야 할 수도 있습니다. – Bugs

답변

1

이게 뭐야?

패널 등의 부모 개체를 반복하면서 개념을 적용하려면 각 자식으로 확장해야합니다.

편집 :

Private _controls As New List(Of Control) 

사용이 핸들러를 추가 : 양식/클래스의 상단에이 선언

을 :

이 아이가있는 모든 컨트롤을 위해 일한다 :

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 

일부

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