473,769 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

select text in textbox

cj
I asked this question a couple of days ago but am just now looking at it
again.

I used to use the textbox gotfoucs event to have all the text in the
textbox selected when it gotfocus. That doesn't seem to work in .net
when the textbox receives focus via a mouse click.

Jeffrey and Shane both advised how to get a mouse click to select all
the text (thank you both) but using the mousedown or mouseup events
doesn't work the way I want it to. I want it to work like the address
bar in MS Explorer (just an example of a "text box" that works like I
want -- this has nothing to do with the internet). When the box
receives focus with a click everything in the box is selected. If you
then click in the box again everything is not selected and the cursor
goes to the place in the contents where you selected. Like I said I
used to do that by selecting all the text in the got focus event. It
worked well because on your second click the textbox is not receiving
focus as it already has focus.

What's the .net way to do this?
Why does selectall() not work in gotfocus?
Mar 30 '06 #1
8 21900
Something like this seems to do what you want:

public Form1()
{
InitializeCompo nent();
textBox1.Text = "textBox1";
textBox2.Text = "textBox2";
shouldSelect = true;
textBox1.GotFoc us += new System.EventHan dler(this.textB ox1_GotFocus);
textBox1.LostFo cus += new System.EventHan dler(this.textB ox1_LostFocus);
textBox1.MouseU p += new
System.Windows. Forms.MouseEven tHandler(this.t extBox1_MouseUp );
}

private bool shouldSelect;

private void textBox1_MouseU p(object sender, MouseEventArgs e)
{
if (shouldSelect)
{
textBox1.Select All();
shouldSelect = false;
}
}

private void textBox1_GotFoc us(object sender, EventArgs e)
{
textBox1.Select All();
}

private void textBox1_LostFo cus(object sender, EventArgs e)
{
shouldSelect = true;
}

/claes

"cj" <cj@nospam.nosp am> wrote in message
news:OU******** ******@TK2MSFTN GP14.phx.gbl...
I asked this question a couple of days ago but am just now looking at it
again.

I used to use the textbox gotfoucs event to have all the text in the
textbox selected when it gotfocus. That doesn't seem to work in .net when
the textbox receives focus via a mouse click.

Jeffrey and Shane both advised how to get a mouse click to select all the
text (thank you both) but using the mousedown or mouseup events doesn't
work the way I want it to. I want it to work like the address bar in MS
Explorer (just an example of a "text box" that works like I want -- this
has nothing to do with the internet). When the box receives focus with a
click everything in the box is selected. If you then click in the box
again everything is not selected and the cursor goes to the place in the
contents where you selected. Like I said I used to do that by selecting
all the text in the got focus event. It worked well because on your
second click the textbox is not receiving focus as it already has focus.

What's the .net way to do this?
Why does selectall() not work in gotfocus?

Mar 30 '06 #2
cj
I don't know. That's C code and I'm not quite smart enough to know
where and what to change to put it in a vb program so I can see what it
does.

Darn this is irritating. It worked just fine in VB4.

Claes Bergefall wrote:
Something like this seems to do what you want:

public Form1()
{
InitializeCompo nent();
textBox1.Text = "textBox1";
textBox2.Text = "textBox2";
shouldSelect = true;
textBox1.GotFoc us += new System.EventHan dler(this.textB ox1_GotFocus);
textBox1.LostFo cus += new System.EventHan dler(this.textB ox1_LostFocus);
textBox1.MouseU p += new
System.Windows. Forms.MouseEven tHandler(this.t extBox1_MouseUp );
}

private bool shouldSelect;

private void textBox1_MouseU p(object sender, MouseEventArgs e)
{
if (shouldSelect)
{
textBox1.Select All();
shouldSelect = false;
}
}

private void textBox1_GotFoc us(object sender, EventArgs e)
{
textBox1.Select All();
}

private void textBox1_LostFo cus(object sender, EventArgs e)
{
shouldSelect = true;
}

/claes

"cj" <cj@nospam.nosp am> wrote in message
news:OU******** ******@TK2MSFTN GP14.phx.gbl...
I asked this question a couple of days ago but am just now looking at it
again.

I used to use the textbox gotfoucs event to have all the text in the
textbox selected when it gotfocus. That doesn't seem to work in .net when
the textbox receives focus via a mouse click.

Jeffrey and Shane both advised how to get a mouse click to select all the
text (thank you both) but using the mousedown or mouseup events doesn't
work the way I want it to. I want it to work like the address bar in MS
Explorer (just an example of a "text box" that works like I want -- this
has nothing to do with the internet). When the box receives focus with a
click everything in the box is selected. If you then click in the box
again everything is not selected and the cursor goes to the place in the
contents where you selected. Like I said I used to do that by selecting
all the text in the got focus event. It worked well because on your
second click the textbox is not receiving focus as it already has focus.

What's the .net way to do this?
Why does selectall() not work in gotfocus?


Mar 30 '06 #3
cj wrote:
I asked this question a couple of days ago but am just now looking at it
again.

I used to use the textbox gotfoucs event to have all the text in the
textbox selected when it gotfocus. That doesn't seem to work in .net
when the textbox receives focus via a mouse click.

Jeffrey and Shane both advised how to get a mouse click to select all
the text (thank you both) but using the mousedown or mouseup events
doesn't work the way I want it to. I want it to work like the address
bar in MS Explorer (just an example of a "text box" that works like I
want -- this has nothing to do with the internet). When the box
receives focus with a click everything in the box is selected. If you
then click in the box again everything is not selected and the cursor
goes to the place in the contents where you selected. Like I said I
used to do that by selecting all the text in the got focus event. It
worked well because on your second click the textbox is not receiving
focus as it already has focus.

What's the .net way to do this?
Why does selectall() not work in gotfocus?


Hello again cj,

I understand your frustration, why did it work perfectly in non-NET VB??

The closest I can come to what you want is with the following code
(watch for line-wrapping!) -
Dim blTextBoxJustRe ceivedFocus As Boolean

Private Sub TextBox1_GotFoc us(ByVal sender As Object, ByVal e As
System.EventArg s) Handles TextBox1.GotFoc us
blTextBoxJustRe ceivedFocus = True
TextBox1.Select All() 'Experiment with removing this line.
End Sub

Private Sub TextBox1_MouseD own(ByVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles TextBox1.MouseD own
If blTextBoxJustRe ceivedFocus Then
blTextBoxJustRe ceivedFocus = False
TextBox1.Select All()
End If
End Sub

Private Sub TextBox1_KeyPre ss(ByVal sender As Object, ByVal e As
System.Windows. Forms.KeyPressE ventArgs) Handles TextBox1.KeyPre ss
blTextBoxJustRe ceivedFocus = False
End Sub
I hope someone else will come-up with a cleaner solution for you.

Regards,

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
Mar 30 '06 #4
Hi CJ,

C# and VB.NET aren't that different, now.

For your benefit, here's Claes' code converted to VB.NET.

--------------------------------------------

Private shouldSelect As Boolean

Private Sub textBox1_MouseU p(ByVal sender As Object, ByVal e As
MouseEventArgs)
If shouldSelect = True Then
textBox1.Select All()
shouldSelect = False
End If
End Sub

Private Sub textBox1_GotFoc us(ByVal sender As Object, ByVal e As
EventArgs)
textBox1.Select All()
End Sub

Private Sub textBox1_LostFo cus(ByVal sender As Object, ByVal e As
EventArgs)
shouldSelect = True
End Sub

--------------------------------------------

BTW, I see you mentioned that SelectAll() does not work for you in the
GotFocus event. I see no reason for it, not to work. Claes has also
used that method in the GotFocus event. Could you post the code you're
using ?

Hope this will help you,

Regards,

Cerebrus.

Mar 30 '06 #5
Hi,

Have you tried to trap the Enter and Leave events instead of GotFocus
and LostFocus ?

Regards,

Cerebrus.

Mar 31 '06 #6
CJ,

I have forever only one answer on your kind of question.

I never use "down" or "press" events.

I use forever "up" events.

(At least they give me more information what was pressed)

I hope this helps,

Cor

"cj" <cj@nospam.nosp am> schreef in bericht
news:OU******** ******@TK2MSFTN GP14.phx.gbl...
I asked this question a couple of days ago but am just now looking at it
again.

I used to use the textbox gotfoucs event to have all the text in the
textbox selected when it gotfocus. That doesn't seem to work in .net when
the textbox receives focus via a mouse click.

Jeffrey and Shane both advised how to get a mouse click to select all the
text (thank you both) but using the mousedown or mouseup events doesn't
work the way I want it to. I want it to work like the address bar in MS
Explorer (just an example of a "text box" that works like I want -- this
has nothing to do with the internet). When the box receives focus with a
click everything in the box is selected. If you then click in the box
again everything is not selected and the cursor goes to the place in the
contents where you selected. Like I said I used to do that by selecting
all the text in the got focus event. It worked well because on your
second click the textbox is not receiving focus as it already has focus.

What's the .net way to do this?
Why does selectall() not work in gotfocus?

Mar 31 '06 #7
Ahh, sorry about that. Missed that this was a VB.NET group. Guess that's
what happens when one participate in both C# and VB.NET forums :-)
Anyway, Cerebrus was kind enough to translate it.

/claes

"cj" <cj@nospam.nosp am> wrote in message
news:u9******** ******@TK2MSFTN GP10.phx.gbl...
I don't know. That's C code and I'm not quite smart enough to know where
and what to change to put it in a vb program so I can see what it does.

Darn this is irritating. It worked just fine in VB4.

Claes Bergefall wrote:
Something like this seems to do what you want:

public Form1()
{
InitializeCompo nent();
textBox1.Text = "textBox1";
textBox2.Text = "textBox2";
shouldSelect = true;
textBox1.GotFoc us += new System.EventHan dler(this.textB ox1_GotFocus);
textBox1.LostFo cus += new
System.EventHan dler(this.textB ox1_LostFocus);
textBox1.MouseU p += new
System.Windows. Forms.MouseEven tHandler(this.t extBox1_MouseUp );
}

private bool shouldSelect;

private void textBox1_MouseU p(object sender, MouseEventArgs e)
{
if (shouldSelect)
{
textBox1.Select All();
shouldSelect = false;
}
}

private void textBox1_GotFoc us(object sender, EventArgs e)
{
textBox1.Select All();
}

private void textBox1_LostFo cus(object sender, EventArgs e)
{
shouldSelect = true;
}

/claes

"cj" <cj@nospam.nosp am> wrote in message
news:OU******** ******@TK2MSFTN GP14.phx.gbl...
I asked this question a couple of days ago but am just now looking at it
again.

I used to use the textbox gotfoucs event to have all the text in the
textbox selected when it gotfocus. That doesn't seem to work in .net
when the textbox receives focus via a mouse click.

Jeffrey and Shane both advised how to get a mouse click to select all
the text (thank you both) but using the mousedown or mouseup events
doesn't work the way I want it to. I want it to work like the address
bar in MS Explorer (just an example of a "text box" that works like I
want -- this has nothing to do with the internet). When the box
receives focus with a click everything in the box is selected. If you
then click in the box again everything is not selected and the cursor
goes to the place in the contents where you selected. Like I said I
used to do that by selecting all the text in the got focus event. It
worked well because on your second click the textbox is not receiving
focus as it already has focus.

What's the .net way to do this?
Why does selectall() not work in gotfocus?


Mar 31 '06 #8
>> Anyway, Cerebrus was kind enough to translate it.

No problem, Claes... It happens to me as well, all the time. ;-)

Regards,

Cerebrus.

Mar 31 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
21726
by: Phill | last post by:
I thought when you tabbed into a textbox that the text it contained was automatically highlighted. This use to be the default in win32. Is there an easy way to do this in .NET. I don't want this to happen if the mouse is used to select the TextBox. Seems wierd, If the Textbox has the default text in it the higlight works but not if you edit it. Thanks.
1
2077
by: Mad Scientist Jr | last post by:
I'm stuck trying to work with a HTML <SELECT> control and javascript (similar to DualList but that control doesn't offer enough options to totally control the text on the buttons and control, also I don't have c# on this machine to try and modify it).. anyway... I'm trying to get javascipt to select all items in a <SELECT> control and submit the form to an asp.net page. For some reason when the link is clicked, you can see the items all...
2
4352
by: Cindy M -WordMVP- | last post by:
Hi all While working my way through a VB.NET exercise, I came across something that didn't do what the text said it should and I'm wondering if it's supposed to behave this way, or not (and why). txtNote.Text = "the text" txtNote.Enabled = True 'txtNote.Select()
4
5437
by: Brett | last post by:
I have a textbox with a heigth of 384. I put a lot of text into it and must scroll down to see it all. When I do Ctrl + A, it doesn't highlight all of the text. I have to manually drag my mouse to get the text. Is there a way to engage the select all? Thanks, Brett
3
30168
by: abc my vclass | last post by:
My win-form have many numericupdown controls to applied. But numericupdown control don't like textbox, text box control can automatic selected text when got focus. Is there any method can let me set all numericupdown autoselect text when gotfocus?
4
8681
by: Dabbler | last post by:
Is there a way to mark the text in a TextBox control as selected so when the user types a new value the existing text is replaced? Thanks
12
2175
by: Brano | last post by:
Hi I am using vb.net 2005 this is a windows application I am using this functionality in another project I have created this simple project to show the problem Basically I have two forms: Form1 - contains a panel (System.Windows.Forms.Panel)
13
5799
by: PinkBishop | last post by:
I am using VS 2005 with a formview control trying to insert a record to my access db. The data is submitted to the main table no problem, but I need to carry the catID to the bridge table CatalogImage where imgID also needs to be placed. Below is my code behind to carry the catID using the Select @@Identity and insert imgID to the bridge table. No data is being entered into the bridge table.
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.