Results 1 to 1 of 1

Thread: Classic VB - How can I pass a control (textbox/listbox/..) to a sub or function?

  1. #1

    Thread Starter
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Classic VB - How can I pass a control (textbox/listbox/..) to a sub or function?

    Rather than write the same code for different controls, it is better to use a Sub or Function to do the work, and simply pass the control as a parameter. This not only makes your code shorter (and so easier to write), but it also means that any changes you make will be changed for all of the controls at the same time (saving the time of repeating the change).


    Let's pretend that you have code like the following for several textboxes (it highlights all of the text when the textbox gets the focus):
    Code:
    Private Sub Text1_GotFocus()
      Text1.SelStart = 0
      Text1.SelLength = Len(Text1.Text)
    End Sub
    ..not a long example, but it will prove the idea!

    You can easily convert this to a sub, which works only on one control (Text1):
    Code:
    Public Sub HighlightText
      Text1.SelStart = 0
      Text1.SelLength = Len(Text1.Text)
    End Sub
    
    Private Sub Text1_GotFocus()
      HighlightText
      'or:
      'Call HighlightText
    End Sub
    ..so how can you make this work for any textbox?

    You need to set up a parameter to the sub/function with the relevant data type (in this case, TextBox), and replace the specific control name (Text1) inside the sub with the name of the parameter, like this:
    Code:
    Public Sub HighlightText(TheTextBox As TextBox)
      TheTextBox.SelStart = 0
      TheTextBox.SelLength = Len(TheTextBox.Text)
    End Sub
    When you have this, you need to modify the code that calls it to also specify the textbox to work with:
    Code:
    Private Sub Text1_GotFocus()
      HighlightText Text1 
      'or:
      'Call HighlightText (Text1)
    End Sub
    
    Private Sub Text2_GotFocus()
      HighlightText Text2
    End Sub
    Note that what we are passing to the sub is the control (or object variable) Text1, rather than the string "Text1" or the text that the textbox contains.



    What if the sub/function is in a different form/module to the control?
    Well it doesn't actually matter.. you still use exactly the same code as above. This is because you are passing an object (a reference to the control itself), rather than the name of the control.

    The control knows which form it is on, so you do not need to specify the form (just like when you use it within its own form). If you try to pass the form aswell, you may end up with code like this:
    Code:
    Public Sub HighlightText(TheForm as Form, TheTextBox As TextBox)
      TheForm.TheTextBox.SelStart = 0  'error!!
    Why doesn't this work? Well the code is looking for a control which is called "TheTextBox" on the form you specified, not a control with the same name as the control you passed. There is no need whatsoever for the form to be passed, simply use the method which was shown above.


    What if the control is in a different form to the code that calls the sub/function?
    This is the only time that something needs to be changed - now you need to specify the form, but only on the line that calls the sub/function, as that is what specifies the object to use.

    Assuming that the control is on Form2, the code would be like this:
    Code:
      HighlightText Form2.Text1 
      'or:
      'Call HighlightText (Form2.Text1)
    Last edited by si_the_geek; Mar 1st, 2007 at 03:26 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width