How can I implement event handlers in a class?

Answered How can I implement event handlers in a class?

  • Monday, March 12, 2012 11:08 PM
     
     

    Hi all of you,

    My project has around thirty winforms. I am thinking how can I avoid to write for every winform the following three subs (I think such code is very auto-descriptive):

    Is it possible to define form event handlers in a class?

    Public Sub Definir_focos()

            For Each c As Control In Me.Controls

                If c.GetType Is GetType(TextBox) Then

                    AddHandler DirectCast(c, TextBox).GotFocus, AddressOf conFoco
                    AddHandler DirectCast(c, TextBox).LostFocus, AddressOf sinFoco
                End If

                Select Case c.HasChildren
                    Case True
                        For Each c2 As Control In c.Controls
                            If c2.GetType Is GetType(TextBox) Then
                                AddHandler DirectCast(c2, TextBox).GotFocus, AddressOf conFoco
                                AddHandler DirectCast(c2, TextBox).LostFocus, AddressOf sinFoco
                            End If
                        Next
                End Select

            Next
       
        End Sub

    Private Sub conFoco(ByVal sender As Object, ByVal e As System.EventArgs)
            DirectCast(sender, TextBox).BackColor = Color.Yellow
    End Sub

    Private Sub sinFoco(ByVal sender As Object, ByVal e As System.EventArgs)
            DirectCast(sender, TextBox).BackColor = Color.White
    End Sub

    How could I encapsulate that logic in a class?

    Thanks for your help,


    enric


    • Edited by Enric Vives Monday, March 12, 2012 11:26 PM
    •  

All Replies

  • Tuesday, March 13, 2012 12:12 AM
     
     Answered

    Hi,

    extend the Form-class. Derive from Form, add your methods, use this custom Form as Forms in your project.

    Or add these methods to your mainform, make them public or internal, pass a reference to "other"-forms/controls as parameter to the methods -or make the methods static...

    Regards,

      Thorsten



  • Tuesday, March 13, 2012 1:01 PM
     
     

    Hi just a silly question.. or not

    Following your advices everything is fine but.. could I avoid InitializeComponent method in that class?


    enric

  • Tuesday, March 13, 2012 2:04 PM
     
     

    I mean, everytime I delete these lines, then the compiler creates them again:

    That means that Form class implement an inteface which has two compulsory methods (initializecomponent and Load??

     Private Sub FormularioEventos_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        End Sub
        Private Sub InitializeComponent()
            Me.SuspendLayout()
            '
            'FormularioEventos
            '
            Me.ClientSize = New System.Drawing.Size(292, 273)
            Me.Name = "FormularioEventos"
            Me.ResumeLayout(False)
        End Sub

    TIA

    enric

  • Thursday, March 15, 2012 5:30 AM
    Moderator
     
     
    Hi Enric,
    The InitializeComponent method has a comment preceding it, warning you not to modify the code. By modifying the code in the InitializeComponent method, you can no longer use the Forms Designer. If you do, your modifications will be lost. You should only perform these optimizations as the final stage of development, after all the design work has been done.
    If you edit anything in the designer, the InitializeComponent method will be created again.
    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

  • Thursday, March 15, 2012 1:08 PM
     
     Answered Has Code

    Rather than putting this at form level, you could subclass the standard controls themselves - TextBox, ComboBox etc. - and override this behaviour there:

    Class TextBox2 Inherits TextBox

    Public Sub New() MyBase.New() End Sub Protected Overrides Sub OnLeave( _ ByVal sender as Object _ , ByVal e as EventArgs ) Me.BackColor = Color.White End Sub Protected Overrides Sub OnEnter( _ ByVal sender as Object _ , ByVal e as EventArgs ) Me.BackColor = Color.Yellow End Sub End Class

    Then just use TextBox2 controls everywhere you would have used TextBox ones and the colouration is done automagically.


    Regards, Phill W.

    • Marked As Answer by Enric Vives Thursday, March 15, 2012 4:24 PM
    •  
  • Thursday, March 15, 2012 4:25 PM
     
     
    Thanks Pill

    enric