Announcement

Collapse
No announcement yet.

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

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #2
    Try incorporating https://www.ozgrid.com/forum/forum/t...x-class-object into your code. In tbxCustom1_KeyPress, tbxCustom1.Name should be the name of the current TextBox control.

    Comment


    • #3
      Thanks John_W for the link by Reafidy.

      [HTML]Try incorporating https://www.ozgrid.com/forum/forum/t...x-class-object into your code. In tbxCustom1_KeyPress, tbxCustom1.Name should be the name of the current TextBox control.
      /HTML]
      Yes i ve tried the same. Only difference is that there are pre-defined(physically placed) Four Textboxes on userform.
      and the code by me when the form is loaded the textboxes are created. Yes the name of the textbox is displayed.after trying
      What i am not able to target is after getting the Textbox Name. Not able to assign THAT TextBox Name Event for KeyAscii = 0
      infact nothing happening in KeyPress Event

      Code:
      Private Sub UserForm_Initialize()
      
      Dim ctlLoop As MSForms.Control
      Dim clsObject As clsObjHandler
      
      Set colTbxs = New Collection
      
      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 20
      Set newTextbox = Controls.Add("forms.textbox.1")
      With newTextbox
      .Name = "txtbx" & i
      .Top = y + 20
      .Height = 18
      .Left = x
      .Width = 120
      .Font.Size = "10"
      .Font.Name = "Calibri"
      '.Text = .Name '& i
      End With
      
      x = x + 142
      If i = 4 Then
      x = 20
      y = newTextbox.Height + newTextbox.Height + 10 + y
      End If
      
      If i = 8 Then
      x = 20
      y = newTextbox.Height + newTextbox.Height + 10 + y
      End If
      
      If i = 12 Then
      x = 20
      y = newTextbox.Height + newTextbox.Height + 10 + y
      End If
      
      If i = 16 Then
      x = 20
      y = newTextbox.Height + newTextbox.Height + 10 + y
      End If
      
      Next i
      'added
      For Each ctlLoop In Me.Controls
           If TypeOf ctlLoop Is MSForms.TextBox Then
               Set clsObject = New clsObjHandler
               Set clsObject.tbxCustomEvent = ctlLoop
               colTbxs.Add clsObject
            End If
      Next ctlLoop
      End Sub
      in clsObjHandler
      Code:
      Public WithEvents tbxCustomEvent As MSForms.TextBox    'Custom Textbox
      
      Private Sub tbxCustomEvent_Change()
      '    MsgBox "You added A Number To: " & tbxCustomEvent.Name
      End Sub
      
      Private Sub tbxCustomEvent_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        If tbxCustomEvent.Name = UserForm1.Controls("txtbx2") Then
              KeyAscii = 0
         End If
      End Sub
      
      Private Sub Class_Terminate()
          Set tbxCustomEvent = Nothing
      End Sub
      Thanks Sam

      Comment


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

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

        Set clsObject.tbxCustomEvent = ctlLoop
        to:

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

        Comment


        • #5
          Thanks, Yes i did miss out setting on control. But then this was learning for me and indeed works fine.
          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.


          Comment


          • #6
            Put this above the Next i:

                     Set clsObject = New clsObjHandler
            Set clsObject.Control = newTextbox
            colTbxs.Add clsObject


            Then delete the entire For Each ... Next loop.

            Comment


            • #7
              Thank you so much John for wonderful guidance. 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.

              Comment


              • #8
                Stick to this thread if any questions or issues are related to this specific task, otherwise please start a new thread.

                Comment


                • #9
                  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.

                  Comment


                  • #10


                    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.


                    Last edited by John_w; 4 weeks ago. Reason: Run-time Locked property

                    Comment

                    Working...
                    X