Solved

Making Edit1 always replace, and not insert

Posted on 2001-07-26
12
318 Views
Last Modified: 2010-04-06
Hi.  Is there an easy way to program the Edit1 component so that everything I type into it, *always* gets replaced? (i.e it must replace the character at the correct position with the new one, and not insert it)

I tried thinking of an easy solution but couldn't come up with one.  I thought of KeyPerform and OnKeyPressed, but it doesn't give you a position as parameter, only the key pressed. ;(

Any suggestions would be appreciated, thanks
Lmoen
0
Comment
Question by:Lmoen
12 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 100 total points
ID: 6324494
This might be useful for you.  The following code will determine the position of the caret in the edit control (it is stored in CPos).  This value should be the index after the character that was just inserted.  You can then use that to manipulate the text in the edit control.

This snippet of code will display the value in CPos in the Form caption.


procedure TForm1.Edit1Change(Sender: TObject);
var
    LPos, CPos :integer;
begin
    Lpos := SendMessage(Edit1.Handle,EM_LINEFROMCHAR,Edit1.SelStart,0);
    Cpos := SendMessage(Edit1.Handle,EM_LINEINDEX,Lpos,0);
    CPos := Edit1.SelStart-CPos;

    //CPos now contains the index of the caret

    Caption := IntToStr(CPos);

end;
0
 
LVL 4

Expert Comment

by:YodaMage
ID: 6324501
Couldn't you grab the value of edit1.text with the onchange event, store it as a string, error check as needed, etc?
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 6324511
Note:

the above works on a Memo as well.  If you are only using an Edit, which is only one line, you can omit the Lpos := ...

procedure TForm1.Edit1Change(Sender: TObject);
var
   CPos :integer;
begin
   Cpos := SendMessage(Edit1.Handle,EM_LINEINDEX,0,0);
   CPos := Edit1.SelStart-CPos;

   //CPos now contains the index of the caret

   Caption := IntToStr(CPos);

end;

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6325547
simple use this

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key in [VK_LEFT,VK_RIGHT] then //maybe morekeys
    //nothing
  else
    edit1.sellength := 1;
end;

meikl ;-)
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 6325610
That's pretty clever =)
0
 

Author Comment

by:Lmoen
ID: 6325684
Thanks for the help
Appreciated
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 6325688
seems, i was a bit too late :-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6325755
oops, wrong symbol
:-)->:-(

;-)
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 6325768
LOL
0
 
LVL 4

Expert Comment

by:StevenB
ID: 6325775
 The early dog gets the meat.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6325782
:-) yup, scrap
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6325787
i have a corner in my eyes :-)

yup, steven
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to center only a line in richedit? 4 52
Delphi application Soap connection 5 96
HTML text in the body of an email (delphi code) 12 96
SUM 2 INTEGER ARRAYS INTO 1 10 93
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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