DVader
March 30th, 2001, 06:57 PM
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?
Is there a way to do this?
|
Click to See Complete Forum and Search --> : Another Easy Visual Basic Question.... DVader March 30th, 2001, 06:57 PM 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? furlong47 March 30th, 2001, 07:28 PM 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 dead March 31st, 2001, 04:58 AM 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? LagMonster March 31st, 2001, 07:55 PM 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 furlong47 April 1st, 2001, 12:22 PM 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 DVader April 2nd, 2001, 02:46 PM 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. DVader April 2nd, 2001, 02:52 PM 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. opiate April 10th, 2001, 01:16 AM 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 TipTap April 10th, 2001, 11:37 PM 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 antonye April 19th, 2001, 04:19 AM 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: Private Sub txtMyBox_GotFocus() txtMyBox.SelStart = 0 txtMyBox.SelLength = Len(txtMyBox.Text) End Sub Hope this helps! windrivers.com
Copyright WebMediaBrands Inc., All Rights Reserved. |