I cannot handle the enter event..Unbelievable or so obvious?

ktab

Well-known Member
Joined
Apr 21, 2005
Messages
1,297
Hello,

This code is at UF initialize event:
Code:
Option Explicit
Dim TextBoxes() As New Class1
Dim ComboBoxes() As New Class1

Private Sub UserForm_Initialize()
Dim TCount As Long, CCount As Long, c As Control
TCount = 0
CCount = 0
'Set TabIndexCollection = New Collection
For Each c In Me.Controls
'    TabIndexCollection.Add c, CStr(c.TabIndex)
    If TypeOf c Is MSForms.TextBox Then
        TCount = TCount + 1
        ReDim Preserve TextBoxes(1 To TCount)
        Set TextBoxes(TCount).TextBoxGroup = c
    End If
    If TypeOf c Is MSForms.ComboBox Then
        CCount = CCount + 1
        ReDim Preserve ComboBoxes(1 To CCount)
        Set ComboBoxes(CCount).ComboBoxGroup = c
    End If
Next c
End Sub

And the following at class1 module:
Code:
Public WithEvents TextBoxGroup As MSForms.TextBox
Public WithEvents ComboBoxGroup As MSForms.ComboBox

Sub ComboBoxGroup_Enter()
MsgBox "Hello from " & ComboBoxGroup.Name
Exit Sub
If ComboBoxGroup.Name Like "TN*" Then
    frmTask.StatusBar1.Panels(1).Text = "NUM"
Else: frmTask.StatusBar1.Panels(1).Text = ""
End If
End Sub

Sub TextBoxGroup_Enter()
MsgBox "Hello from " & TextBoxGroup.Name
Exit Sub
If TextBoxGroup.Name Like "TN*" Then
    frmTask.StatusBar1.Panels(2).Text = "NUM"
Else: frmTask.StatusBar1.Panels(2).Text = ""
End If
End Sub
Sub TextBoxGroup_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim i As Integer
If TextBoxGroup.Name Like "TN*" Then
    nonNumberEntered = False
    If KeyCode < 49 Or KeyCode > 57 Then
        If KeyCode < 96 Or KeyCode > 105 Then
            If KeyCode <> 8 Then
                If KeyCode <> 111 Then
                    If KeyCode <> 191 Then
                        nonNumberEntered = True
                    End If
                End If
            End If
        End If
    End If
End If
end sub
Sub TextBoxGroup_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If nonNumberEntered = True Then KeyAscii = 0
End Sub

The code handles keydown, keypress events with no problem. Also similar change events, and probably more.
I cannot figure out why it does not handle enter event.. What ever i may do. Unbelievable or so obvious that i cannot see?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi Kostas,

As I understand it some events are inherited from the container object, the
UserForm here. Within the events class, you are effectively trapping a
control's events outside of that container, so there is no access to
inherited events (they aren't exposed by that control).

In summary, no joy I am afraid.
 
Upvote 0
Kostas

The Entere evgent just isn't available (exposed) I'm afraid.

PS That's a nasty looking If you have there. Have you considered Select Case?
Or perhaps even IsNumeric?
 
Upvote 0
xld: The class handles the keydown, keypress events but not then enter event. There are no _enter events assigned to controls of UF. All handled by class (too many controls)
Norie: No, the keydown-keypress events fire, so it works. A select case is already done ;) The isnumeric won't let me input some additional chars like /
Macleanb:Too much trouble for nothing. The actual # of controls is 56 (combos & textboxes). The reason that i made a class is because i did not want to go at each and every control to type code (even something like commandbutton1_click). I wanted to create something like a template to have global affect to similar controls.
 
Upvote 0
xld: The class handles the keydown, keypress events but not then enter event. There are no _enter events assigned to controls of UF. All handled by class (too many controls)

I know. What I am saying is that those other events are not exposed to an events class for the reasosn that I gave.
 
Upvote 0
BTW,

a little suggestion. rather than test for like "TN*", why not use the Tag property?
 
Upvote 0
Kostas

What exactly are you doing?

Why do you have 56 controls in the first place?

PS I don't see any Select Case in your code.:eek:
 
Upvote 0
xld, yes the tag i will use it, and let you know. Also my Eng isn't that good so missunderstood "inherited". To tell you the truth, an inherited event in our case, enter event, i used it in a new UF but worked. What could possibly have gone wrong so UF does not let me handle this event?
Is there a way to "take" away from UF the handle of this event?

Norie, the select case is done but not posted. I just uploaded the original "unfiltered" code. May controls eh? I know, and used the minimal i could !!!
It's about a project asked to do, that may handle the members of a union, about all that you could imagine a member entry could have.
To forecast your next question: No i won't build it in VB. The request was for excel :(
 
Upvote 0
Kostas

My next question/suggestion wouldn't have been that.:)

It would still be the why 56 controls?

PS If I was to suggest another application it would probably be Access.:)
 
Upvote 0

Forum statistics

Threads
1,213,567
Messages
6,114,342
Members
448,570
Latest member
rik81h

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top