Textbox Class Object

  • Hi All,


    I recently had a request for a better method of controlling multiple textboxes on a userform. "I have lots of textboxes on a userform I want the user to only be able to enter numbers in all of them!"


    The solution: Making use of a class module. The method below will alow you to handle the event for all textboxs at once without having to add events for each individual textbox on the form.


    Add a class module and name it "clsObjHandler"



    Add a userform with as many textboxes as necessary and the following code:



    Hope this helps someone out.

  • Re: Textbox Class Object


    Nice job, Reafidy!


    I'm going to have to ponder the code to understand it better, but do have one question: shouldn't

    Code
    Dim clsObject As New clsObjHandler


    be

    Code
    Dim clsObject As clsObjHandler


    .. so that you don't create an orphaned instance of the class when clsObject is declared?


    Edit: Well, two questions: Would you explain how this line works:

    Code
    Set clsObject.Control = ctlLoop

    [SIZE=1]Entia non sunt multiplicanda sine necessitate.[/SIZE]

  • Re: Textbox Class Object


    Ahh silly me - Yes it certainly should be thanks.


    Im not very good at explaining things.

    Code
    Set clsObject.Control = ctlLoop


    it tells the class to handle events for the textbox.
    An alternative would be:

    Code
    Set clsObject.tbxCustom1 = ctlLoop


    and changing the withevents to public and removing the property set control:

    Code
    Public WithEvents tbxCustom1 As MSForms.TextBox   
    'Public Property Set Control(tbxNew As MSForms.TextBox)
    '    Set tbxCustom1 = tbxNew
    'End Property
  • Re: Textbox Class Object


    Hi,
    Reafidy sorry for digging up the thread, but I've got a question. In your example we've got 4 textboxes. What should I do if I want put value for example
    from TextBox1 to cell A1,
    from TextBox2 to cell A2,
    from TextBox3 to cell A3
    and so on.


    I came up with

    Code
    row = Right(tbxCustom1.Name, 1)


    and use it later as

    Code
    cells(row, 1).value = tbxCustom1.value

    but it's very non proffesional way :D

  • Hello Reafidy ...:wink:


    Just discovered you very handy TextBox class ...!!!


    Like it very much ...


    Thanks a lot Reafidy :smile:

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

Participate now!

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