2013-03-12 5 views
2

내 양식에서 텍스트 상자의 이벤트를 처리하는 방법을 알고 있습니다. 그러나 30 개의 텍스트 상자를 사용하기 때문에이 코드를 더 짧게하고 싶습니다. 다음을 사용하는 것은 비효율적입니다.하나의 핸들러에서 모든 텍스트 이벤트 처리

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged, TextBox5.TextChanged, TextBox6.TextChanged, TextBox7.TextChanged, TextBox8.TextChanged, TextBox9.TextChanged, TextBox10.TextChanged 
    Dim tb As TextBox = CType(sender, TextBox) 

    Select Case tb.Name 
     Case "TextBox1" 
      MsgBox(tb.Text) 
     Case "TextBox2" 
      MsgBox(tb.Text) 
    End Select 
End Sub 

처리기를 단축 할 방법이 있습니까?

+3

나는 [질문]이라고 생각합니다. (http://stackoverflow.com/questions/15336379/get-text-inside-buttons-when-pressed- 어제는 using-only-one-event-handler를 사용했다. –

+0

내가 가장 단축 된 버전을 찾고 있는데^_^ – conquistador

답변

7

프로그래밍 방식으로 Controls.OfType + AddHandler을 사용할 수 있습니다.

Dim textBoxes = Me.Controls.OfType(Of TextBox)() 
For Each txt In textBoxes 
    AddHandler txt.TextChanged, AddressOf txtTextChanged 
Next 

하나 핸들러 모든 : 예를 들어

Private Sub txtTextChanged(sender As Object, e As EventArgs) 
    Dim txt = DirectCast(sender, TextBox) 
    Select Case txt.Name 
     Case "TextBox1" 
      MsgBox(txt.Text) 
     Case "TextBox2" 
      MsgBox(txt.Text) 
    End Select 
End Sub 
+2

내가 선택한 문이 필요 없다고 생각한다. 대신이'MsgBox (DirectCast (sender, TextBox) .Text)'와 같은 한 줄을 쓸 수있다. :) –

+2

@GLOIERTECH : 방금 OP 코드를 복사했습니다. 샘플 코드 일 뿐이므로 실제로 이러한 경우를 다르게 처리하려고합니다. 나는 원래 'TextBox'를 식별하는 것이 가능하다는 것을 보여주고 싶었다. (OP는 이미 그것을 알고 있었지만). –

+0

Ok :) 저는이 사이트에 새로운 사람입니다. 많은 사람들이 'OP'라는 단어를 사용하고 있다는 것을 알고 있었습니까? (전체 양식) –

3

말 당신이있는 경우 그 패널 (PnlTextBoxes), 이제 동적

다음과 같이 당신의 textboxes에 대한 handler를 만들 수 있습니다 내부 30 textboxes
For each ctrl in PnlTextBoxes.controls 
If TypeOf ctrl is TextBox then 
    AddHandler ctrl.TextChanged, AddressOf CommonClickHandler 
end if 
Next 


Private Sub CommonHandler(ByVal sender As System.Object, _ 
ByVal e As System.EventArgs) 

     MsgBox(ctype(sender,TextBox).Text) 

End Sub 
3

디자이너로 매우 Textbox를 만들었다면 더 좋은 방법이라고 생각하지 않습니다. 동적으로 텍스트 상자를 만든 경우

그러나,이 방법으로 AddHandler에해야

For i = 0 to 30 
    Dim TB as New Texbox 
    AddHandler TB.TextChanged, TextBox1_TextChanged 
    'Set every Property that you need 
    Me.Controls.Add(TB) 
Next 
0

가장 방법은 우선, TextBox에서 상속하는 것 그 OnTextChanged method 사용자 정의 처리 코드를 추가하려면 내장 된 TextBox 컨트롤 대신 양식에서 사용하십시오.

그런 식으로 모든 이벤트 처리 코드의은 하나의 장소에 있으며 추상화가 증가합니다. 이 동작은 컨트롤을 포함하는 폼이 아니라 컨트롤 클래스 자체 내에서 정의되며 정의됩니다. 그리고 물론, 추악하고 유지하기 힘든 Handles 문장을 사용하지 못하도록하거나 더 나쁘고 느린 심지어 더러운 For 루프를 사용하지 않아도됩니다. 당신이 다시 컴파일 한 후, 당신은 당신의 기존 TextBox 컨트롤을 대체 할 수 있어야

Public Class CustomTextBox : Inherits TextBox 

    Protected Overridable Sub OnTextChanged(e As EventArgs) 
     ' Do whatever you want to do here... 
     MsgBox(Me.Text) 

     ' Call the base class implementation for default behavior. 
     ' (If you don't call this, the TextChanged event will never be raised!) 
     MyBase.OnTextChanged(e) 
    End Sub 

End Class 

다음 :

예를 들어, 프로젝트에 새 파일에 새 사용자 정의 텍스트 상자 컨트롤을 정의하는 코드를 추가 새로 정의 된 CustomTextBox 컨트롤이 내장되어 있습니다.

관련 문제