Solved

Making Edit1 always replace, and not insert

Posted on 2001-07-26
12
317 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

11 Experts available now in Live!

Get 1:1 Help Now