Events, Objects and Classes

  • Thread starter Thread starter Gonçalo Boléo
  • Start date Start date
G

Gonçalo Boléo

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
 
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.TextChanged
'Do your thing...
End Sub

Public Sub Txt_KeyPress (sender As Object, e As KeyPressEventArgs) _
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
 
* "Gonçalo Boléo said:
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.

Why can't you declare a variable as 'System.Windows.Forms.TextBox'?
How can i do this?

You can skip the 'WithEvents' declaration and use 'AddHandler' and
'RemoveHandler' for adding/removing handlers to an object instead. Have
a look at the documentation on these commands. If you have a question,
feel free to post it here.

You will find a sample for creating handlers for some buttons
dynamically here:

<http://groups.google.com/[email protected]>
 
Gonçalo Boléo said:
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?

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)
 
Addendum:

Usage of the class below:

\\\
Dim f As New FooBar()
f.TheTextBox = Me.TextBox1
///

Class handling the event (notice the 'Handles' part of the event handler):

\\\
Public Class FooBar
Private WithEvents m_TheTextBox As TextBox

Public Property TheTextBox() As TextBox
Get
Return m_TheTextBox
End Get
Set(ByVal Value As TextBox)
m_TheTextBox = Value
End Set
End Property

Private Sub m_TheTextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles m_TheTextBox.KeyDown
MsgBox("Key Down")
End Sub
End Class
///
 
Back
Top