Results 1 to 8 of 8

Thread: Pass a textbox to a function

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Posts
    9

    Unhappy

    When i pass a textbox to a function, it does not take a textbox but the contents of the text box. How do i pass a text box. pl. suggest.

  2. #2
    Guest
    You want to create a textbox?

    Code:
    'HeSaidJoe
    
    Option Explicit
    
    Private WithEvents txtObj As TextBox
    
    Private Sub Form_Load()
       Set txtObj = Controls.Add("VB.Textbox", "txtObj")
       With txtObj
          .Visible = True
          .Width = 3000
          .Text = "This textbox was created on the fly!"
          .Top = 1000
          .Left = 1000
       End With
    End Sub
    
    Private Sub Command1_Click()
    txtObj.Text = "You clicked the button!"
    End Sub


  3. #3
    Guest
    Or to pass a textbox to a function:

    Code:
    Public Function DoSomething(pTextBox as TextBox) as String
        DoSomething = pTextBox.Text
    End Function
    It would be called like:

    Code:
    lstrTemp = DoSomething(Text1)
    Hope that helped

    - gaffa

  4. #4
    Guest

    Talking

    Hi!

    Try this:

    Code:
    Private Sub PlayWithTextBox(ByRef tb As TextBox)
        ' Here you can change any of the properties...
        tb.Alignment = vbRightJustify
        tb.BackColor = &HFF0000
        tb.FontBold = True
        tb.ForeColor = &HFFFFFF
        tb.Left = tb.Left / 2
        tb.Text = "Fun with Textboxes"
        tb.Top = tb.Top / 2
        ' I think you got the idea now...
    End Sub
    And somewhere in your code try this:

    Code:
        PlayWithTextBox Text1  ' Where Text1 is a TextBox
        PlayWithTextBox Text2  ' Where Text2 is a TextBox
    Enjoy!

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Posts
    9

    Textbox

    Thankyou Mathew, gaffa, Roblll. But my problem is still not solved. i m doing something like this ...


    ---- call to a function. txtUserName is the name of the text box. --------

    Call SelectText(txtUserName)



    ----- This is a function residing in a module ------

    Public Function SelectText(ByRef TxtBx As TextBox) '
    TxtBx.SetFocus
    TxtBx.SelStart = 0
    TxtBx.SelLength = Len(TxtBx.Text)
    End Function



    ----- If i type 'Sylvia' in the textbox, TxtBx in the function is 'Sylvia'-------

  6. #6
    Lively Member
    Join Date
    Feb 2000
    Posts
    118

    Works for me

    I put this in a form with a text box and a button and it worked perfect.
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Call SelectText(Text1)
    End Sub
    Public Function SelectText(ByRef TxtBx As TextBox) '
    TxtBx.SetFocus
    TxtBx.SelStart = 0
    TxtBx.SelLength = Len(TxtBx.Text)
    End Function
    How are you using it?

    [Edited by kokopeli on 10-05-2000 at 01:05 AM]
    Kokopeli
    VB6 SP3

  7. #7
    Lively Member Feras's Avatar
    Join Date
    Sep 2000
    Location
    Homs, Syria
    Posts
    85
    If you mean that if we typed:
    Print TxtBx
    That will return 'Sylvia', that's because if you typed TxtBx alone thats eqal to TxtBx.Text.

    Sorry if that is not the missunderstanding.


    [Edited by Feras on 10-05-2000 at 02:46 AM]
    Yesterday is history ... Tomorrow is mistry .. Today is a gift.

    VB6 , intermidiat

  8. #8
    Guest
    Turn it into a Sub.

    Code:
    Private Sub Command1_Click()
    Call SelectText(txtUserName) 
    End Sub
    
    Private Sub SelectText(TxtBx As TextBox)
    TxtBx.SetFocus 
    TxtBx.SelStart = 0 
    TxtBx.SelLength = Len(TxtBx.Text) 
    End Sub

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