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.
Printable View
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.
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
Or to pass a textbox to a function:
It would be called like:Code:
Public Function DoSomething(pTextBox as TextBox) as String
DoSomething = pTextBox.Text
End Function
Hope that helpedCode:lstrTemp = DoSomething(Text1)
- gaffa
Hi!
Try this:
And somewhere in your code 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
Enjoy!Code:PlayWithTextBox Text1 ' Where Text1 is a TextBox
PlayWithTextBox Text2 ' Where Text2 is a 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'-------
I put this in a form with a text box and a button and it worked perfect.
How are you using it?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
[Edited by kokopeli on 10-05-2000 at 01:05 AM]
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]
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