In Class1 module KeyPress_Event How do i get the name of textbox ?

  • Hello


    Any idea In Class1 KeyPress_Event How do i get the name of textbox whose name property has been changed to "txtbx" & i when loaded on Userform1 at run time.
    Able to load the form with 25 textboxes but dont know how to get the name of textbox in keypress event of Class Module. After getting the name i want that textbox to KeyAscii = 0


    [Code]
    Private Sub UserForm_Initialize()
    Dim i As Integer
    Dim newTextbox As MSForms.TextBox
    Dim x As Integer
    Dim y As Integer
    x = 20
    y = 20


    For i = 1 To 25
    Set newTextbox = Controls.Add("forms.textbox.1")


    With newTextbox
    .Name = "txtbx" & i
    .Top = y + 20
    .Height = 18
    .Left = x
    .Width = 150
    .Font.Size = "10"
    .Font.Name = "Calibri"
    End With
    y = y + 20
    Next i


    End Sub
    [Code]


    Regards
    Sam

  • Thanks John_W for the link by Reafidy.



    in clsObjHandler


    Thanks Sam

  • You don't seem to have copied the code correctly. In clsObjHandler you need:


    [VBA]Public Property Set Control(tbxNew As MSForms.TextBox)
    Set tbxCustom1 = tbxNew
    End Property
    [/VBA]And in the For Each loop (whose innards you could incorporate into the For i loop, rather than separate loops) change:


    [VBA]Set clsObject.tbxCustomEvent = ctlLoop
    [/VBA]to:


    [VBA]Set clsObject.Control = ctlLoop[/VBA]Then checking the textbox name inside the Keypress event should be:
    [VBA]If tbxCustom1.Name = "txtbx2" Then[/VBA]Also, you could declare colTbxs outside the procedures so you can clear it in the UserForm_Terminate event, as shown in the linked code.

  • Thanks, Yes i did miss out setting on control. But then this was learning for me and indeed works fine.

    Quote


    And in the For Each loop (whose innards you could incorporate into the For i loop, rather than separate loops) change:


    Sorry I did not understand. May i request you to kindly elaborate on the same with eg.

  • Thank you so much John for wonderful guidance.:rock: As Class Module is begining for me. will take time to absorb. If there are any further issues faced for the same should i create a new thread or continue in the same.

  • One question


    Will it be advisable to create new class for different user forms or can i use the same clsObjectHandler for diffferent userform.
    Eg if userform2 is loaded with Textboxes and out of them some boxes with same type of Numeric keypress and ReadOnly but in userform the Textbox will have different control name. will this do


    Thank you so much for your guidance. :) Now Slowly getting grip with class events.

  • It sounds like you could use the same class. But if my guess (you don't say exactly) that the idea with


    Code
    If tbxCustom1.Name = "txtbx2" Then
            KeyAscii = 0
        End If


    Is to prevent the user typing in or changing the txtbx2 field, why don't you set txtbx2 to read only at run-time by setting its Locked property to True:


    Code
    With newTextbox
                .Name = "txtbx" & i
                .Top = y + 20
                .Height = 18
                .Left = x
                .Width = 120
                .Font.Size = "10"
                .Font.Name = "Calibri"
                If .Name = "txtbx2" Then .Locked = True
            End With

    But without knowing what you are trying to do I can only guess.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!