RE: AddHandler
From: Iouri Simernitski (iouris_at_micro.nospam.soft.com)
Date: 06/01/04
- Next message: Iouri Simernitski: "RE: Menu event called twice"
- Previous message: Howard Kaikow: "Re: Office 2003: Does installing VSTO disable New Project and Open Project in Office VBA File menu?"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 01 Jun 2004 21:20:45 GMT
Great question!
First of all, make sure that you keep a reference to your text boxes as
long as you need your event handler to run.
If you only use a local variable to hold a reference to the text box, your
handler will stop to work after the garbage collector sweeps it away.
Having just one member variable for all the textboxes would not work other.
Store them in an ArrayList.
Now, back to your question. Indeed, the handler for the Change event does
not have the sender parameter.
You can work around this limitation by creating a helper class that holds a
reference to the textbox.
Here's the code snippet (I used a Word project, but Excel is nearly
identical) :
-------------
Private keepAlive As New System.Collections.ArrayList
' Called when the document is opened.
Private Sub ThisDocument_Open() Handles ThisDocument.Open
' Stuff here ..........
Dim tBox As MSForms.TextBox
tBox = FindControl("TextBox1")
' keep a reference to the control to
' prevent garbage collection
keepAlive.Add(tBox)
'Instead of 'AddHandler tBox.Change, AddressOf ChangeHandler
AddHandler tBox.Change, New DelegateHelper(tBox).ChangeDelegate
End sub
End Class
----------------------------
' Helper class
Public Class DelegateHelper
Public WithEvents sender As MSForms.TextBox
Friend ReadOnly Property ChangeDelegate() As
MSForms.MdcTextEvents_ChangeEventHandler
Get
Return New MSForms.MdcTextEvents_ChangeEventHandler(AddressOf
OnChange)
End Get
End Property
Friend Sub New(ByVal sender As MSForms.TextBox)
Me.sender = sender
End Sub
Private Sub OnChange()
' do stuff
' MsgBox(Me.sender.Text)
End Sub
End Class
---------------
Hope this helps,
iouri
----------
This posting is provided "AS IS" with no warranties, and confers no rights.
- Next message: Iouri Simernitski: "RE: Menu event called twice"
- Previous message: Howard Kaikow: "Re: Office 2003: Does installing VSTO disable New Project and Open Project in Office VBA File menu?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|