sorry if this is not the right place to post this
I was wondering how I could highlight everything in a textbox just by clicking on it.
example: textbox1 has "0" as text, I want to click(it automatically highlights/selects all) and type 1, instead of having to click, delete the 0, and then type 1
ty vm for any help
7 27125 Banfa 9,065
Expert Mod 8TB
I was under the impression that text (edit?) boxes did this anyway.
However if that is not the case then you should be able to do something like
Implement the OnClick handler
In the handler select all Sel(0, -1) I think
I say this as a C programmer rather than a VB programmer though.
Not sure what you are trying to do and I'll take a simple stab at it.
Lets say you have a control Text1
When text 1 gets the focus of the system, you want to highlight the text, and change the number inside of the control to 1 from 0.
That in itself is real simple with code: -
Private sub text1_GotFocus()
-
-
'when control receives the focus, set the back color to color of choice
-
text1.backcolor = vbyellow '[vb(color of your choice) OR hex value for color]
-
'set the value of the text box to 1
-
text1.text = "1"
-
-
end sub
-
The only problem with this code is that you do not have control if you accidently tab into the control from changing the value to 1. I would suggest just doing the data entry manually. If your data is important enough to change, then its also important enough to protect from error. Tab in, change the value with the keyboard.
Thanks for that, Kolus.
Based on the original question I'd say darkk can use what you've shown, and just skip the text1.text = "1".
darkk, if you are going to highlight the textbox as suggested by Kolus (by setting the background colour, in the example) don't forget to set it back to the original colour in the LostFocus event. Note, if you need to remember the original colour so it can be restored, the .Tag property of the control is a convenient place, though it's a string.
Banfa: Not sure about VB.Net, but in VB6 textboxes certainly do not select all by default. And I, for one, am glad that they don't. It's simple enough to implement if you want, but it would be a nuisance if you had to remember to go in and remove the selection each time.
Oops!
Sorry, I didn't read Kolus's post in enough detail. I thought the background-colour highlight was being done as well as selecting the text, not instead of it. Try this... - Private Sub Text1_GotFocus()
-
-
' When control receives the focus, auto-select the complete contents.
-
With Text1
-
.SelStart = 0
-
.SelLength = 1000
-
End With
-
-
' You can also set the background colour if you want, of course.
-
-
End Sub
Note that this is all VB6 code. The details (such as the definition of the Sub) will vary in later versions of VB, but the concepts should still be valid.
Also, where I used 1000 for the SelLength - this can be any number, as long as it is at least equal to the length of the text. You can use the Len() function to set it to the correct length, but anything longer will also work. Sometimes I set it to 65535, just to be sure.
the correct way: -
''''''' Text1.SetFocus
-
Text1.SelStart = 0
-
Text1.SelLength = Len(Text1.Text)
-
Okay, that works when you will always want to replace *everything* in a text box, but what about when sometimes you want to replace everything but other times you want to insert or correct text at a certain point in the text box? For example, when you've entered a search phrase, only to realize you need to modify it to get better results. Here's the functionality I've seen that meets this need:
1st click in the text box - highlights everything
2nd click in the text box - places cursor at the point of the click
This is the way many text boxes work these days, such as the address bar in Mozilla browsers, Google toolbar (for mozilla), etc...
Anybody know how to do this in VB?
-Sam
Private Sub TextBox1_gotfocus() Handles TextBox1.GotMouseCapture
Me.TextBox1.SelectionStart = 0
Me.TextBox1.SelectionLength = Len(TextBox1.Text)
End Sub
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Tony |
last post by:
How can i highlight the whole text in a textbox control when a user tab or
shift+tab into the control?
Thanks
Tony
|
by: Cox |
last post by:
How would I go about highlighting text in a textbox?
|
by: Robert M |
last post by:
Hello everybody,
I am having a problem that I did not have before.. I can not highlight a
text in a Textbox on a ASP.NEt form ..
Can anybody help..
Thank you
|
by: parm |
last post by:
How do I highlight text in my first (tabindex = 1) asp control (eg:
textbox) on the webform on page load. If I try to set focus using
javascript then I get script error "Cannot move focus to...
|
by: abc my vclass |
last post by:
which properties to focus automatic highlight text on text when on fous?
|
by: tranky |
last post by:
Hola boys, i've a question for you!
It's possible to highlight some text inside a textarea? (not all text,
but a portion!)
For example, i've this text inside a textarea.
INSERISCI TESTO
i...
|
by: Ian Davies |
last post by:
I am trying to move some text by clicking a button. When clicked the button
runs the java script and this will then take the value in the text field and
add 10 to it to create a new value, this is...
|
by: Dave |
last post by:
Is there a way to selectively highlight text in an OverLib popup? I'd
like to be able to make some text stand out from the rest of the text
that is displayed.
I tried using a one-cell table with...
|
by: mbatestblrock |
last post by:
I have no idea how to even phrase this.. I assume this is something easy.
http://www.picment.com/articles/css/funwithforms/
just an example. When you highlight text with your mouse (as if you...
|
by: geosmy |
last post by:
Hi everyone,
Is it possible to highlight (a portion of the) text in a textbox without setting focus to it. What I'm interested is marking some portion of the text for reference while my cursor is...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |