473,324 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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


Nov 20 '05 #1
4 1108
"Gonçalo Boléo" <gb****@netcabo.pt> schrieb
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)
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
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
Nov 20 '05 #3
* "Gonçalo Boléo" <gb****@netcabo.pt> scripsit:
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/groups?selm=eQYj9FAQDHA.2244%40TK2MSFTNGP11.phx.gb l>

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #4
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
///

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: scsharma | last post by:
Hi, I am working on creating a webapplication and my design calls for creating main webform which will have menu bar on left hand side and a IFrame which will contain all the forms that are shown...
4
by: Ben | last post by:
Hello, Can anyone tell me what is difference between raising your own events and writing functions. I can do the same thing in a function, so why would I want to raise an event? I am not very...
4
by: Erik Ekedahl | last post by:
I am a intermediate VB6 programmer moving to VB.NET using the book "Programming Microsfot Visual Basic.net" (Microsoft Press) I have understood so far what is going on but I am haveing a bit of...
7
by: Jay Douglas | last post by:
Greetings, I have a Windows form application that (naturally) instantiates all sorts of objects. I have a base object that contains an event. Lots of other objects inherit from this event. ...
2
by: frank | last post by:
Good Morning All, I am working on a CAD application. At the time of this writing I am currently working on the GraphicsEngine. The structure is as follows: GraphicsObject (Base Class, Must...
7
by: nick.fletcher | last post by:
I have a custom collection which derives from Collection<which stores a number of objects. Before each item is added to the collection - an event which it exposes is hooked by the collection and...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
5
by: raylopez99 | last post by:
I understand delegates (static and non-static) and I agree they are very useful, and that the "Forms" used in the Windows .NET API could not work without them. That said, I'm curious as to how...
5
by: Sin Jeong-hun | last post by:
class Manager { public event ItemEventHandler ItHappened; public Manager { Item i; i.ItHappend+=new ItemEventHandler(OnItHappened); } void OnItHappened(...) {
3
Frinavale
by: Frinavale | last post by:
Background An Event is a message sent out by an object to notify other objects that an action/trigger/state-change (ie. an event) has taken place. Therefore, you should use an event when an object's...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.