Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 661
  • Last Modified:

Select all text when mouse click in an Edit Box

Is there a way in C++ to select all data in a control (TEdit), when the OnEnter event fires?  If the user TAB's into the field all data is automatically selected, but if they click in the field the | cursor is simply inserted where they clicked, I would like to have the OnEnter select all text in the control, so the mouse click works as the TAB does.

I have already tried (to no avail):
FieldName->SelStart =0;
FieldName->SelLength = FieldName->Text.Length();

I also tried the following 2 variations:
SendMessage(FieldName->Handle, EM_SETSEL, 0, -1);                  
SendMessage(FieldName->Handle, EM_SETSEL, 0, FieldName->Text.Length());                  

I use C++Builder v6 if that matters
0
BarrieAtTheBeach
Asked:
BarrieAtTheBeach
  • 2
  • 2
1 Solution
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
It seems that even if you call the onenter event when you click into the tedit the internal onclick is fired for last (so the cursor position is changed).
Have you tried writing your code into the OnMouseDown event instead?
0
 
BarrieAtTheBeachAuthor Commented:
I have not attempted to use any OnMouse event, but will explore that avenue and post back.
0
 
BarrieAtTheBeachAuthor Commented:
Thank you Ferruccio68. Your suggestion led to the solution I will employ.

On Enter
//Set a global variable that field has been entered
  bGlobalFeildEntered = true;

On Exit
  bGlobalFeildEntered = false;

On Mouse Down
if (bGlobalFeildEntered)
{
  bGlobalFeildEntered = false;
  SendMessage(FieldName->Handle, EM_SETSEL, 0, -1);
}
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Glad to have helped you.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now