線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1024
推到 Plurk!
推到 Facebook!

問一個TEdit的問題~~找了好久~~還是找不出@_@

尚未結案
jesszn
一般會員


發表:7
回覆:5
積分:2
註冊:2003-09-06

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-09-15 22:41:40 IP:61.217.xxx.xxx 未訂閱
假如我有一個Edit1這個元件,我設定他的maxlength := 4 然後執行,我在上面打了4個字後,就打不進去了, 再來我把游標移到最前方,要輸入時因為已經超過最大字數 而且好像是insert的狀態所以沒辦法再輸入了,請問有沒有 辦法可以將Edit設成overwrite的狀態呢? 請各高手幫忙一下啦~~謝謝^^
Miles
尊榮會員


發表:27
回覆:662
積分:622
註冊:2002-07-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-09-15 23:42:05 IP:61.31.xxx.xxx 未訂閱
Hi jesszn 您好: 試試這段
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
   if (Edit1.GetTextLen = Edit1.MaxLength) and
      (Edit1.SelLength < 2) then begin
      Edit1.SelLength := 1;
      Edit1.SelText := String(Key);
   end;
end;
我不是高手, 高手是正在銀幕前微笑的人.
------


我不是高手, 高手是正在銀幕前微笑的人.
hagar
版主


發表:143
回覆:4056
積分:4445
註冊:2002-04-14

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-09-17 20:08:50 IP:202.39.xxx.xxx 未訂閱
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;    type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Edit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    FInsertState: Boolean; // 記錄現在是 Insert 或 Override 狀態
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.FormCreate(Sender: TObject);
begin
  FInsertState := True;
end;    procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  iSelStart: integer;
  sTmp: string;
begin
  if Key = VK_INSERT then begin // 使用者按下 Insert 鍵
    FInsertState := not FInsertState;
    if FInsertState then
      Label1.Caption := 'Insert'
    else
      Label1.Caption := 'Override';
  end;      if Key = VK_ESCAPE then Exit;
  if Key = VK_RETURN then Exit;
  if Key = VK_PRIOR then Exit;
  if Key = VK_NEXT then Exit;
  if Key = VK_LEFT then Exit;
  if Key = VK_RIGHT then Exit;
  if Key = VK_HOME then Exit;
  if Key = VK_END then Exit;
  if Key = VK_INSERT then Exit;
  if Key = VK_DELETE then Exit;      if not FInsertState then begin // 如果是在 Override 的狀態下, 要做額外處理
    iSelStart := Edit1.SelStart;  // 先將 SelStart 值記錄下來
    sTmp := Edit1.Text;
    if (iSelStart   1 <= Length(sTmp)) then begin // SelStart 之後有要被 Override 的字元
      if IsDBCSLeadByte(Byte(sTmp[iSelStart 1])) then // 要被 Override 的是中文或全形字
        Delete(sTmp, iSelStart 1, 2) // 刪除 2 bytes
      else
        Delete(sTmp, iSelStart 1, 1); // 要被 Override 的是半形字, 刪除 1 byte          Edit1.Text := sTmp; // 將 Edit1.Text 指定為經 Override 的 sTmp
      Edit1.SelStart := iSelStart; // 將 SelStart 指定為原本的 SelStart 值, 否則要新增的字元會跑到第一個字元去
    end;
  end;
end;    end.
2.另一種方式, 可以把 TRichEdit 拿來當 TEdit 用 此時就有 Insert/Override 的功能了 不過沒 MaxLength 的功能可用, 得自行在 KeyPress 等事件攔截 且要攔截 Enter 鍵, 以免 Cursor 跑到第 2 行去了. --- 歡迎光臨 KTop 研究院
系統時間:2024-05-19 6:57:31
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!