Solved

Selection with TEdit component

Posted on 2002-04-21
9
236 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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
 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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