?
Solved

Selection with TEdit component

Posted on 2002-04-21
9
Medium Priority
?
242 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
Technology Partners: 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 200 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 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