Events, Objects and Classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gonçalo Boléo

    #1

    Events, Objects and Classes

    I wan't to make a class that as a property that references a textbox and
    then cath the events of the text box in the class.
    In VB6 i make something like this:

    Dim WithEvents m_oTxt as VB.Textbox

    Property Let ....
    Property Set ....

    And then simply had events for the m_oTxt variable

    m_oTxt_Change
    m_oTxt_KeyPress (...)

    I can't do the same in VB.Net because i can't declare a variable of the type
    TextBox and if i create a variable of the type Object it doesn't let me cath
    de events.
    How can i do this?

    thanks in advance,
    Gonçalo Boléo
    Portugal




  • Armin Zingler

    #2
    Re: Events, Objects and Classes

    "Gonçalo Boléo" <gboleo@netcabo .pt> schrieb[color=blue]
    > I wan't to make a class that as a property that references a textbox
    > and then cath the events of the text box in the class.
    > In VB6 i make something like this:
    >
    > Dim WithEvents m_oTxt as VB.Textbox
    >
    > Property Let ....
    > Property Set ....
    >
    > And then simply had events for the m_oTxt variable
    >
    > m_oTxt_Change
    > m_oTxt_KeyPress (...)
    >
    > I can't do the same in VB.Net because i can't declare a variable of
    > the type TextBox and if i create a variable of the type Object it
    > doesn't let me cath de events.
    > How can i do this?[/color]

    In order to use WithEvents, you must declare the variable as Textbox.

    Why don't you know which type of control is created? Isn't there a location
    within your application where you create the textbox? After creating the
    textbox you can use the AddHandler statement to attach event handlers to
    events. After that you can assign the textbox to a variable declared as
    object (whyever you wanna do this)


    --
    Armin




    Comment

    • Fergus Cooney

      #3
      Re: Events, Objects and Classes

      Hi Gonçalo,

      Public Class Gonçalo
      Private WithEvents m_oTxt As Textbox

      Public Sub Txt_Change (sender As Object, e As EventArgs) _
      Handles m_oTxt.TextChan ged
      'Do your thing...
      End Sub

      Public Sub Txt_KeyPress (sender As Object, e As KeyPressEventAr gs) _
      Handles m_oTxt.KeyPress
      'Do your thing...
      End Sub
      : : :
      End Class

      (The Handles is only on the second line in each case to fit this message.
      You could have it on the same line as the Sub declaration.)

      When you can assign it later with m_oTxt = New TextBox, you'll want
      to add the TextBox to the Controls collection of the Form or Panel in which
      it is to appear.

      Me.Controls.Add (m_oTxt) or Me.pnlSomething .Controls.Add (m_oTxt)

      The names of the Event handlers <don't> need to use the name of the variable
      or follow the pattern that they do in VB6. (Notice how I've dropped the 'm_o'
      from the name? It doesn't matter). The name is completely your choice, but by
      convention it follows the same pattern.

      The important part is the Handles bit with the name of the Control and the Event
      name. This is what connects the TextBox with the Event Handler.

      There is another way of doing the above but I think this one will do you.

      Regards,
      Fergus


      Comment

      Working...