TextBox_Change a little premature!

  • Thread starter Post Tenebras Lux
  • Start date
P

Post Tenebras Lux

I have created some dynamic controls on my userform. I have also managed
(with the help of someone's posted code) how to add events to these controls
at runtime by using class modules. The events seem to work well. However,
for the textboxes, they work too well.

To test the procedure, the routine now is just to open a msgbox (not the
eventual purpose, but does make sure it's happening).

I've put the code for the textbox below. My problem is that, for the
textbox, EVERY single digit (or letter) entered into the textbox triggers the
change event sub. So for example, if I want to enter 200 in the textbox,
and then trigger the change event, what is currently happening is:
I enter 2 -> msgbox appears
I enter 0 -> msgbox appears
I enter 0 -> msgbox appears

I sure that's not how nature and Microsoft intended it to be (well, I'm sure
about nature).

Can anyone suggest how I can remedy this so that the change event for a text
box is only triggered once, either on leaving the changed cell, or when the
full change in the textbox is completed.

The same class module and routines for the combobox work perfectly. They
have exactly the same syntax (except text = combo).

Thanks for any suggestions!


'in the class module clsActiveTextBox
Private WithEvents ActiveTextBox As msforms.TextBox
'this sets each TextBox control which is passed to it, as an element of the
class array
Sub Set_ControlArrayElement(ByVal cmb As msforms.TextBox)
Set ActiveTextBox = cmb
End Sub

Private Sub ActiveTextBox_Change()
MsgBox "You clicked " & ActiveTextBox.Name
End Sub


'In the UserForm_Initialize sub

Call CreateControls
TextBox_ClassArrayInit 'see below



Sub TextBox_ClassArrayInit()

Dim CTL As msforms.Control
Dim lngCtrl As Long
Dim frm As msforms.UserForm
Dim bIsTextB As Boolean
Dim strPrefix As String

Set frm = SettingsWindow.MultiPage1.Pages(0)
For Each CTL In frm.Controls
If TypeOf CTL Is msforms.TextBox Then
lngCtrl = lngCtrl + 1
ReDim Preserve aryTextBox(lngCtrl)
Set aryTextBox(lngCtrl) = New clsActiveTextBox
'call class method (function in the class module) which we made up
aryTextBox(lngCtrl).Set_ControlArrayElement CTL
End If
Next

End Sub
 
B

Bob Phillips

Unfortunately, application events do not support the TextBox Exit event. You
could always trap the KeyDown event and check it for 13 or 9 (Enter or Tab),
and output your message box then

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
H

halimnurikhwan

Hi,

Why you don't use tha available Event that has
most functionality in each event rather than create
a class module to assign it as event of a control.

maybe you mean UserControl in VB, that you can
create your own event by that control.

Regards,

Halim


Post Tenebras Lux menuliskan:
 
P

Post Tenebras Lux

Thanks. After playing around with it, I came to the same conclusion about
trapping via the KeyDown event. Seems to work well now.
 
P

Post Tenebras Lux

thanks for the suggestion. However, I don't know if it's workable for events
assigned to a control created at runtime. That's why I had to use the class
module approach.

Bob Phillips recommendation appears to have solve the problem.
 

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