Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing Cursor Position in TEdit 2

Status
Not open for further replies.

GMcConville

Programmer
Feb 8, 2006
6
NZ
Any one have any idea on how to select an area of text in a TEdit and have the cursor at the beginning of the selection.

eg:

TEdit.Text := 'Delphi Rules';
TEdit.SelStart := 7;
TEdit.SelLength := 5;

TEdit should now display 'Delphi Rules' with the last word selected (or something close to that) and the cursor at the end of the text. But I need the cursor at the beginning of the selection, just before the letter 'R'.

Any ideas?
Grant
 
try something like:
Code:
  Edit1.Text := 'Delphi Rules';
  Edit1.SetFocus;
  Edit1.SelStart := 7;
  Edit1.SelLength := 5;
  Edit1.SelStart := 7;

Andrew
Hampshire, UK
 
Thanks Towerbase, but have already tried this unsuccesfully. Yes, it does move the cursor but it also unselects the selection. ANy other ideas.
 
Strange. I could have sworn it worked for me yesterday. But it certainly isn't working for me today! Sorry.

Does any application display the behaviour you want? Maybe it's simply not the Windows way of doing things.

Is there any particular reason you want the cursor at the start of the selected text?

Andrew
Hampshire, UK
 
Just one of those strange things, if does this in Microsoft Excel, although I haven't really seen it anywhere else. I am using this as an autocomplete system, and although there are other ways to do it, it annoys me when can't do things exactly as I wanted. I shall just have to admit defeat.

Thanks
Grant
 
The following works in the way you wanted - I have scraped it together from:
Code:
procedure PostKeyEx32(key: Word; const shift: TShiftState; specialkey: Boolean) ;
{
Parameters :
* key : virtual keycode of the key to send. For printable keys this is simply the ANSI code (Ord(character)) .
* shift : state of the modifier keys. This is a set, so you can set several of these keys (shift, control, alt, mouse buttons) in tandem. The TShiftState type is declared in the Classes Unit.
* specialkey: normally this should be False. Set it to True to specify a key on the numeric keypad, for example.
Description:
Uses keybd_event to manufacture a series of key events matching the passed parameters. The events go to the control with focus.
Note that for characters key is always the upper-case version of the character. Sending without any modifier keys will result in a lower-case character, sending it with [ ssShift ] will result in an upper-case character!
}
type
   TShiftKeyInfo = record
     shift: Byte ;
     vkey: Byte ;
   end;
   ByteSet = set of 0..7 ;
const
   shiftkeys: array [1..3] of TShiftKeyInfo =
     ((shift: Ord(ssCtrl) ; vkey: VK_CONTROL),
     (shift: Ord(ssShift) ; vkey: VK_SHIFT),
     (shift: Ord(ssAlt) ; vkey: VK_MENU)) ;
var
   flag: DWORD;
   bShift: ByteSet absolute shift;
   j: Integer;
begin
   for j := 1 to 3 do
   begin
     if shiftkeys[j].shift in bShift then
       keybd_event(shiftkeys[j].vkey, MapVirtualKey(shiftkeys[j].vkey, 0), 0, 0) ;
   end;
   if specialkey then
     flag := KEYEVENTF_EXTENDEDKEY
   else
     flag := 0;

   keybd_event(key, MapvirtualKey(key, 0), flag, 0) ;
   flag := flag or KEYEVENTF_KEYUP;
   keybd_event(key, MapvirtualKey(key, 0), flag, 0) ;

   for j := 3 downto 1 do
   begin
     if shiftkeys[j].shift in bShift then
       keybd_event(shiftkeys[j].vkey, MapVirtualKey(shiftkeys[j].vkey, 0), KEYEVENTF_KEYUP, 0) ;
   end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  Edit1.Text := 'Delphi Rules';
  Edit1.SetFocus;
  Edit1.SelStart := Length(Edit1.Text);
  for i := 1 to 5 do
    PostKeyEx32(VK_LEFT, [ssShift], True);
end;

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Note: I did try to put this together using SendInput (which has superseded keybd_event) but failed to get it working.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
You're welcome.

I noticed that the functionality you desired could be achieved by clicking to the right of the last character in the edit box, then whilst pressing the Shift key, the left arrow key could be pressed as many times as required. After every character is selected in this way, the caret (or cursor) appears to the left of the selected character rather than to the right.

I never actually explained what the code does, so if you haven't already worked it out here's what it's doing:

[tt]Edit1.SelStart := Length(Edit1.Text);[/tt]
Move the caret/cursor to the end of the text in Edit1

[tt]for i := 1 to 5 do
PostKeyEx32(VK_LEFT, [ssShift], True);[/tt]
Simulate a <SHIFT+LEFT_ARROW> key-press 5 times using a loop

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Fine solution, Stretch.

Let me give you another star.

buho (A).


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top