Solved

Selection with TEdit component

Posted on 2002-04-21
9
233 Views
Last Modified: 2011-10-03
Hi,

I have a TEdit component on a form and I would like to make it so that when the user initially clicks on the TEdit, all of the text is selected (so the user can easily type something to replace it).  However, the user should still be allowed to select a range with the mouse themselves without having all of the text selected.  To demonstrate what I mean, take a look at the URL edit box in Internet Explorer.

Could you please give me any sample code that would do this?

The closest I've got is this...

procedure TMain.EditBoxClick(Sender: TObject);
begin
  if EditBox.SelLength = 0 then EditBox.SelectAll;
end;

The above almost works, but clicking with the mouse to the right of the text automatically selects all of the text, which I do not want.  I only want the SelectAll to take place when the user initially clicks on the edit box.

Thanks in Advance,

Richard
0
Comment
Question by:Richard2000
9 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6957614
? use the onenter-event instead
0
 
LVL 2

Expert Comment

by:freshman3k
ID: 6957866
Hello!

Put The following code on the TEdit's MouseDown event and it should do the Trick:

Edit1.SelLength := -Edit1.SelStart;

The minus turns it into a negative number

Good Luck!  :-)
0
 
LVL 2

Expert Comment

by:freshman3k
ID: 6957930
Hello!

Put The following code on the TEdit's MouseDown event and it should do the Trick:

Edit1.SelLength := -Edit1.SelStart;

The minus turns it into a negative number

Good Luck!  :-)
0
 

Author Comment

by:Richard2000
ID: 6958238
Hi,

Thanks for your comments.  I've tried them out, but I have not been able to get them to work.

The problem with using OnEnter is that the event never seems to fire when a component is clicked on with the mouse, only when the focus moves to the component using the tab key.

I tried using Edit1.SelLength := -Edit1.SelStart; but this caused the selection to always appear from the left to the current position whenever the component is clicked on.

Any other ideas?

Richard
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:hagur
ID: 6958463
How about this?

var
  Form1: TForm1;
  blnFirst : boolean;


procedure TForm1.Edit1Click(Sender: TObject);
begin
  if blnFirst then begin
    if Edit1.SelLength = 0 then Edit1.SelectAll;
    blnFirst := false;
  end;
end;

procedure TForm1.Edit1Exit(Sender: TObject);
begin
  blnFirst := true;
end;


Note: So the onExit event will fire, you must have other components on the form, which I'm sure you do, so the Edit can loose focus.

When ever the Edit looses focus, blnFirst is set to true so next time the user clicks the Edit box and it gets the focus, all the text will be selected.




0
 
LVL 9

Expert Comment

by:ginsonic
ID: 6958883
I think that hagur solution is what you wish.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6959364
? didn't understand,
even the ie-combo does not select all twice
0
 
LVL 17

Expert Comment

by:geobul
ID: 6959445
Hi,

procedure TForm1.Edit1Click(Sender: TObject);
begin
  if (Sender is TEdit) then
    with (Sender as TEdit) do
      if SelLength = 0 then
        SelectAll;
end;

Regards, Geo
0
 
LVL 9

Accepted Solution

by:
ITugay earned 50 total points
ID: 6959654
Hi Richard2000,


procedure TForm1.Edit2Enter(Sender: TObject);
begin
  PostMessage(Edit2.Handle, EM_SETSEL, 0, Length(Edit2.Text));
end;

---
Igor.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now