+ -
当前位置:首页 → 问答吧 → richedit光标定位和回车问题

richedit光标定位和回车问题

时间:2011-09-22

来源:互联网

实现的效果如下:
A=C×B
A= //按下回车时“A=”自动生成,且光标定位在“=”号右边该如何做。现在实现了“A=”自动生成功能,但是每次回车均产生2行,而不是一行。本人前面发帖请教过,但效果不理想,在一次发帖请高手帮忙。3Q!代码如下:
Delphi(Pascal) code

unit ata01;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, ComCtrls, Grids, DBGrids;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    RichEdit1: TRichEdit;
    procedure FormCreate(Sender: TObject);
    procedure RichEdit1KeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
richedit1.Text:='';
end;

procedure TForm1.RichEdit1KeyPress(Sender: TObject; var Key: Char);
var
  i,k :integer;
  s,ls:string;
begin
   if richedit1.Lines.Count>0 then
   i:=richedit1.Lines.Count-1
   else
   i:=0;
   if key=#13 then
    begin
    s:=richedit1.Lines.Strings[i];
    k:=pos('=',s);
    ls:=copy(s,1,k);
    richedit1.Lines.Add(ls);
    edit1.Text:=inttostr(richedit1.Lines.count);
 end;

    richedit1.SelStart:=k;
    richedit1.SetFocus;
    
  end;

end.

作者: lcop2011   发布时间: 2011-09-22

我不是答了你关于memo同样内容的贴了吗?怎么还没解决?

作者: gzzai   发布时间: 2011-09-23

Delphi(Pascal) code
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    Edit1: TEdit;
    procedure RichEdit1KeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  richedit1.Text:='';
end;

procedure TForm1.RichEdit1KeyPress(Sender: TObject; var Key: Char);
var i,k :integer;
    s,ls:string;
begin
  if key=#13 then
  begin
    key:=#0;
    if richedit1.Lines.Count<1 then exit;
    if richedit1.Lines.Count>0 then i:=richedit1.Lines.Count-1;
    s:=richedit1.Lines.Strings[i];
    k:=pos('=',s);
    ls:=copy(s,1,k);
    richedit1.Lines.Add(ls);
    edit1.Text:=inttostr(richedit1.Lines.Count);
    richedit1.SelStart:=length(richedit1.Text);
    richedit1.SelLength:=0;
    richedit1.SetFocus;
  end;
end;

end.

作者: gzzai   发布时间: 2011-09-23

肯定是两行,你并没有限制回车的执行,加一句

  if Key = #13 then Key := #0;

作者: bdmh   发布时间: 2011-09-23

我比较菜,delphi似懂非懂,主要还是基础太差,研究下楼上两位的办法

作者: lcop2011   发布时间: 2011-09-23

就一个判断ok了:
Delphi(Pascal) code
procedure TForm1.RichEdit1KeyPress(Sender: TObject; var Key: Char);
var i,k :integer;
    s,ls:string;
begin
  if key=#13 then
  begin
    key:=#0;
    if richedit1.Lines.Count<1 then exit;
    i:=richedit1.Lines.Count-1;
    s:=richedit1.Lines.Strings[i];
    k:=pos('=',s);
    ls:=copy(s,1,k);
    richedit1.Lines.Add(ls);
    edit1.Text:=inttostr(richedit1.Lines.Count);
    richedit1.SelStart:=length(richedit1.Text);
    richedit1.SelLength:=0;
    richedit1.SetFocus;
  end;
end;

作者: gzzai   发布时间: 2011-09-23

给你解析下:
Delphi(Pascal) code
procedure TForm1.RichEdit1KeyPress(Sender: TObject; var Key: Char);
var i,k :integer;
    s,ls:string;
begin
  if key=#13 then//如果回车键被按下了
  begin
    key:=#0;//把回车的动作消掉
    if richedit1.Lines.Count<1 then exit;//如果richedit1没内容,不往下执行
    i:=richedit1.Lines.Count-1;//取最后一行的索引值
    s:=richedit1.Lines.Strings[i];//取最后一行的字符
    k:=pos('=',s);//求等号在字符串的位置
    ls:=copy(s,1,k);//从字符串第一个开始、复制到(包括)等号
    richedit1.Lines.Add(ls);//将刚复制的字符串添加进richedit1内
    edit1.Text:=inttostr(richedit1.Lines.Count);//在edit1中显示richedit1的行数
    richedit1.SelStart:=length(richedit1.Text);//选择的起始位
    richedit1.SelLength:=0;//选择字符的个数
    richedit1.SetFocus;//让richedit1获得焦点
  end;
end;

作者: gzzai   发布时间: 2011-09-23