Solved

Controlling edit box entering

Posted on 1997-10-22
5
232 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:jimshort
  • 3
5 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1348575
SelLength end StartLen are used when you select a text in the editbox.

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;


0
 

Author Comment

by:jimshort
ID: 1348576
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.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1348577
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;
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1348578
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;

0
 
LVL 1

Accepted Solution

by:
millerw earned 100 total points
ID: 1348579
OK, I have your first one done.  This is all the code for it.  Just make each of the three events for the TEdit and paste the code into each (OnKeyDown, OnEnter, OnKeyPress).  You need to make sure to do three things after you paste the code for your TEdit box (Not a TMaskEdit) in.  Here they are:

1.  In Delphi 2 make sure the form holding the TEdit has it KeyPreview property set to TRUE.
2.  Set the TEdit's OnClick to run the OnEnter event also.
3.  Set the TEdit's OnDblClick to run the OnEnter event as well.

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
     Case Edit1.SelStart of
     0 : begin
              Case Key of
              VK_RIGHT : Begin
                              Edit1.SelStart := 3;
                              Edit1.SelLength := 1;
                         end;
              Ord('T'), Ord('t') : begin
                                        Edit1.SelText := 'TUE';
                                        Edit1.SelLength := 1;

                                   end;
              Ord('M'), Ord('m') : begin
                                        Edit1.SelText := 'MON';
                                        Edit1.SelLength := 1;
                                   end;
              end;
              If Key <> VK_TAB then
                 Key := 0;
         end;
     3 : begin
              Case Key of
              VK_LEFT  : Begin
                              Edit1.SelStart := 0;
                              Edit1.SelLength := 3;
                         end;
              VK_RIGHT : Begin
                              Edit1.SelStart := 4;
                              Edit1.SelLength := 1;
                         end;
              Ord('H'), Ord('h') : begin
                                        Edit1.SelText := 'H';
                                        Edit1.SelLength := 1;
                                   end;
              Ord('B'), Ord('b') : begin
                                        Edit1.SelText := 'B';
                                        Edit1.SelLength := 1;
                                   end;
              end;
              If Key <> VK_TAB then
                 Key := 0;
         end;
     4 : begin
              Case Key of
              VK_LEFT  : Begin
                              Edit1.SelStart := 3;
                              Edit1.SelLength := 1;
                         end;
              VK_RIGHT : Begin
                              Edit1.SelStart := 5;
                              Edit1.SelLength := 1;
                         end;
              Ord('0')..Ord('9') : begin
                                        Edit1.SelText := Chr(Key);
                                        Edit1.SelLength := 1;
                                   end;
              end;
              If Key <> VK_TAB then
                 Key := 0;
         end;
     5 : begin
              Case Key of
              VK_LEFT  : Begin
                              Edit1.SelStart := 4;
                              Edit1.SelLength := 1;
                         end;
              Ord('0')..Ord('9') : begin
                                        Edit1.SelText := Chr(Key);
                                        Edit1.SelLength := 1;
                                   end;
              end;
              If Key <> VK_TAB then
                 Key := 0;
         end;
     6 : begin
              Case Key of
              VK_LEFT  : Begin
                              Edit1.SelStart := 5;
                              Edit1.SelLength := 1;
                         end;
              end;
              If Key <> VK_TAB then
                 Key := 0;
         end;
     end;
end;

procedure TForm1.Edit1Enter(Sender: TObject);
begin
     Case Edit1.SelStart of
     0 : Edit1.SelLength := 3;
     3..5 : Edit1.SelLength := 1;
     6 :  Edit1.SelLength := 0;
     else
     begin
          Edit1.SelStart := 0;
          Edit1.SelLength := 3;
     end;
     end;
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
     If Key <> #9 then
        Key := #0;
end;

You should be able to figure out what everything does.  If not just write me.  

Oh, and about the cursor position.  The cursor position is the addition of SelStart and SelLength.  The cursor is always at the end--right side--of the selected text.  That is how to find the cursor position at all times.

Hope this helps,
Scott
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

18 Experts available now in Live!

Get 1:1 Help Now