jimshort
asked on
Controlling edit box entering
I have tried using TMaskEdit to mask input from edit boxes, but it is too restrictive. What I need to do is know where abouts the cursor is in the box, i.e. where abouts the character being entered is going to go. e.g. I want user to enter a string MONH21 where if the first letter is m then mon appears, if it is t tue appears, I also only want the letters H or B to be entered in the 4th position and the TEditMask deals with the two digits on the end.
I have another similar situation, where I want floats to be entered to 4dp with 3places to the left of the dp. So - when 3 digits have been entered and there isn't a dp there yet, one appears in the right place, also if someone presses '.' then I want the dp to move to where the user is positioned. I thought that the position of the cursor could be determined by the StartSel property of edit boxes and the SelLength would be zero if there was only a cursor flashing.
Does anyone know how to find the cursor pos?
Thanks in advance -
jimshort@ndirect.co.uk
I have another similar situation, where I want floats to be entered to 4dp with 3places to the left of the dp. So - when 3 digits have been entered and there isn't a dp there yet, one appears in the right place, also if someone presses '.' then I want the dp to move to where the user is positioned. I thought that the position of the cursor could be determined by the StartSel property of edit boxes and the SelLength would be zero if there was only a cursor flashing.
Does anyone know how to find the cursor pos?
Thanks in advance -
jimshort@ndirect.co.uk
ASKER
Sorry, I'm afraid I knew that. The bit I want to know about is the bit you didn't answer. I can do a pos(text, 'c') in the onchange event after they have put the letter in, but what if I want to use numbers like in the second example - how do I tell which is the last zero put in in a string of zeros? I am sure in some language the seltext gives you the cursor pos, but it doesn't in Delphi1. It seems as though I will have to validate the text in the edit box after every alteration is made, but the combinations of these are huge - how many different states can the edit box be in - hundreds.
So are you saying that there is no way of finding the cursor position in an edit box in Delphi?
Cheers for the answer though.
So are you saying that there is no way of finding the cursor position in an edit box in Delphi?
Cheers for the answer though.
I don't know a direct way. Sorry
This can sound silly but maybe it's a solution.
1.Use the OnKeyPress event.
2.Use a variable of type string. OLDWORD
3.Look for the key into text of the editbox.
4.Look the change in text of editbox and OLDWORD
5. --> GIVES you the right position of the new key
6.Put the text of the editbox into the var OLDWORD
Have fun,
ZifNab;
This can sound silly but maybe it's a solution.
1.Use the OnKeyPress event.
2.Use a variable of type string. OLDWORD
3.Look for the key into text of the editbox.
4.Look the change in text of editbox and OLDWORD
5. --> GIVES you the right position of the new key
6.Put the text of the editbox into the var OLDWORD
Have fun,
ZifNab;
Maybe there is a solution:
Try this 2 Edit box sample below.
unit Cursor;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
procedure Edit1Change(Sender: TObject);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
CurPos : integer;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Edit1Change(Sender: TObject);
begin
CurPos := Edit1.SelStart;
edit2.Text := IntToStr(CurPos);
end;
procedure TForm1.Edit1KeyDown(Sender : TObject; var Key: Word;
Shift: TShiftState);
begin
If Key = VK_LEFT then dec(CurPos);
if Key = VK_RIGHT then inc(CurPos); {RIGHT Arrow}
edit2.text := inttostr(CurPos);
end;
Have fun,
c.u. ZifNab;
Try this 2 Edit box sample below.
unit Cursor;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
procedure Edit1Change(Sender: TObject);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
CurPos : integer;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Edit1Change(Sender:
begin
CurPos := Edit1.SelStart;
edit2.Text := IntToStr(CurPos);
end;
procedure TForm1.Edit1KeyDown(Sender
Shift: TShiftState);
begin
If Key = VK_LEFT then dec(CurPos);
if Key = VK_RIGHT then inc(CurPos); {RIGHT Arrow}
edit2.text := inttostr(CurPos);
end;
Have fun,
c.u. ZifNab;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Use Onkeypress event.
In event use method GetTextLen, it gives you the length of the total entered characters in the editbox. -> So if user just types in you can use this method to find the position of the cursor.
If user uses insert. Maybe you can make then a smart finder of the entered character. You've got the entered character (Key), you've got the whole text (Text). And you've got a function for finding a key in a string (Pos).
Need help, just ask;
ZifNab;