Solved

Selection with TEdit component

Posted on 2002-04-21
9
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

689 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