Textbox event fired in class module

J

Jess

How can I get a given control event -let say the textbox keypress event- to
be fired in a class module? I believe this is called multicasting in VB6.

The below code is the one I am unsuccessfully using: the TextBox_KeyPress is
not fired in the class module.

Form:

Dim amount As CTextBxN

Private Sub Form_Load()

Set amount = New CTextBxN

‘text1 is a textbox control on this form
Set amount.TextBox = Text1

End Sub

CTextBxN Class:


Public WithEvents TextBox as TextBox


public Sub TextBox_KeyPress(KeyAscii As Integer)

….event code…

End Sub

I have noticed that if the form has a declaration for the keypress event,
the latter is fired first in the form, and then in the class module.

In other words. I the below snippet of code is added to the form, the
keypress event is fired first in the form, and second in the class module.

public Sub TextBox_KeyPress(KeyAscii As Integer)


End Sub

Can anybody help me with this?

Thanks in advance
 
S

Stefan Hoffmann

hi Jess,
Jess said:
How can I get a given control event -let say the textbox keypress event- to
be fired in a class module? I believe this is called multicasting in VB6.
Start with this:

The forms code:
---
Option Compare Database
Option Explicit

Private m_TextBox As clsTextBox

Private Sub Form_Close()

Set m_TextBox = Nothing

End Sub

Private Sub Form_Load()

Set m_TextBox = New clsTextBox
Set m_TextBox.TextBox = Text0

End Sub

Private Sub Text0_KeyPress(KeyAscii As Integer)

MsgBox KeyAscii & " from form."

End Sub
---

The class' (clsTextBox) code:
---
Option Compare Database
Option Explicit

Private WithEvents m_TextBox As Access.TextBox

Public Property Set TextBox(AValue As Access.TextBox)

Set m_TextBox = AValue

End Property

Private Sub m_TextBox_KeyPress(KeyAscii As Integer)

MsgBox KeyAscii & " from class."

End Sub
 
J

Jess

Stephan,

Thanks for your answer. I have a question:

Is there a way to accomplish this without declaring the keypress event in
the form?

I would like the keypress event to be trapped in the class module for every
textbox. Do I have to declare the keypress event for every textbox in the
form?

Thanks
 
S

Stefan Hoffmann

hi Jess,
I would like the keypress event to be trapped in the class module for every
textbox. Do I have to declare the keypress event for every textbox in the
form?
Ah, okay. I didn't understand it the first time. Afaik you need this
modification (in my example):

clsTextBox:
---
Public Property Set TextBox(ATextBox As Access.TextBox)

Set m_TextBox = ATextBox

If m_TextBox.OnKeyPress = "" Then
m_TextBox.OnKeyPress = "[Event Procedure]"
End If

End Property
---

Caveat: If a matching event procedure exists in the code behind the
form, then it is called also.


mfG
--> stefan <--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top