Solved

Selection with TEdit component

Posted on 2002-04-21
9
235 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi 2010 Export to pdf 2 285
delphi prevent click fast 2 189
Press three keys together and trigger a function 3 50
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 55
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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
A short film showing how OnPage and Connectwise integration works.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 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

13 Experts available now in Live!

Get 1:1 Help Now