Handling all textbox event in one Handler

后端 未结 4 1914
感动是毒
感动是毒 2020-12-18 11:27

I do know how to handle event of textboxes in my form. But want to make this code shorter because I will 30 textboxes. It\'s inefficient to use this:

Private         


        
4条回答
  •  猫巷女王i
    2020-12-18 11:40

    Say If you are having that 30 textboxes inside a panel(PnlTextBoxes), Now you can create handler for your textboxes dynamically like this below

    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
    

提交回复
热议问题