|
-
March 30th, 2001, 06:57 PM
#1
Registered User
Another Easy Visual Basic Question....
I need to use VB code to select all the text in a text box on my form?
Is there a way to do this?
-
March 30th, 2001, 07:28 PM
#2
To select all the text in a text box, where the name of the text box is Text1, and assign it to a string variable x:
Dim x as String
x=Text1.Text
Is this what you wanted to do? I'm unclear on your purpose
-
March 31st, 2001, 04:58 AM
#3
Registered User
i'm with furlong47, i dont understand exactly what you want to do here.
Do you want to highlight all the text in a textbox?
-
March 31st, 2001, 07:55 PM
#4
I think you mean select some text. Here is a little code curtisy of MS. Just make a text box and your good to go. The search IS CaSe SeNsItIvE. hope it helps
Private Sub Form_Click()
Dim Search, Where ' Declare variables.
' Get search string from user.
Search = InputBox("Enter text to be found:")
Where = InStr(Text1.Text, Search) ' Find string in text.
If Where Then ' If found,
Text1.SetFocus
Text1.SelStart = Where - 1 ' set selection start and
Text1.SelLength = Len(Search) ' set selection length.
Else
MsgBox "String not found." ' Notify user.
End If
End Sub
-
April 1st, 2001, 11:22 AM
#5
Cool LagMonster, I hadn't learned about SelStart and SelLength yet.
In that same vein then, to select ALL the text in a textbox would look like this:
Private Sub Form_Load()
Dim x As String
x = Text1.Text
Text1.SelStart = 0
Text1.SelLength = Len(x)
End Sub
-
April 2nd, 2001, 01:46 PM
#6
Registered User
Sorry about the confusion. I did want to highlight all the text in the text box.
Now I'm having a problem with the SetFocus command now.
LagMonster's code worked the first time and furlong47's worked after I added the Text1.SetFocus command, but after inserting it in the Form_Load Procedure, I get: Invalid procedure call or argument.
I then tried the SetFocus command on other objects in my form and get the same error.
Is there anyway to renable it?
I tried restarting, but not reinstalling VB yet.
-
April 2nd, 2001, 01:52 PM
#7
Registered User
Correction on my last post. SetFocus apparently will cause this error when put in the: Form1_Load
procedure or if another procedure contains the line and is called from the Form1_load procedure. It works other wise. Just not when the form initally is loaded.
-
April 10th, 2001, 12:16 AM
#8
sub whateveryoursubnameis()
dim a as string (or what ever data type u want)
a = text1.text
msgbox a
end sub
most of the time you can get or set an objects properties a runtime depending if it's a read only property.
object.property = values
or
values = object.property
if you have any question feel free to email me.
some good resources are http://news.devx.com, IRC #visualbasic, http://planetsoucecode.com
-
April 10th, 2001, 10:37 PM
#9
Originally posted by DVader:
Correction on my last post. SetFocus apparently will cause this error when put in the: Form1_Load
procedure or if another procedure contains the line and is called from the Form1_load procedure. It works other wise. Just not when the form initally is loaded.
I believe this is because at the point Form1_Load is called, the controls (like the text box) on Form1 have not been loaded. If you put a timer control on the form and set the interval to something like 1, that should work. I'm not too sure about this, but I think putting Form1.Refresh before the SetFocus commnand might make it work.
The best way, I think, is to load the form from a module (Sub Main) and put any commands that reference controls on the Form after calling Form1.Show.
Hope this helps,
Lee
-
April 19th, 2001, 03:19 AM
#10
An easier way to do this (and probably the best way, in my opinion) is to select all the text in the box when it gets the focus. That way you know that the form is loaded and the controls (ie, the text box in question) is available for you to play with.
An example would be:
Code:
Private Sub txtMyBox_GotFocus()
txtMyBox.SelStart = 0
txtMyBox.SelLength = Len(txtMyBox.Text)
End Sub
Hope this helps!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|
Bookmarks