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

如何让dbgrid的栏位负号只能出现在在第一位

尚未結案
luowy651
高階會員


發表:257
回覆:313
積分:114
註冊:2003-04-09

發送簡訊給我
#1 引用回覆 回覆 發表時間:2012-06-08 11:30:34 IP:122.234.xxx.xxx 訂閱
下面的code来自leveon大大,可以让负号只能出现一次,但怎样能让负号如果出现的话,只能出现在第一位?

//建一個繼承物件 目的為取 正在編輯的值

TdbGrid_temp = class(TDBGrid)
private
NowEditText:string;
protected
procedure SetEditText(ACol, ARow: Longint; const Value: string); override;
end;
procedure TdbGrid_temp.SetEditText(ACol, ARow: Longint; const Value: string);
begin
inherited;
NowEditText := Value;
end;

//keypress 改這樣

procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
var
tempGrid:TdbGrid_temp;
tmpEditStr:string;
begin
tempGrid := TdbGrid_temp(Sender);
tmpEditStr := tempGrid.NowEditText;
if trim(tmpEditStr)='' then tmpEditStr:= tempGrid.SelectedField.AsString;
if tempGrid.Fields[tempGrid.SelectedIndex].FieldName ='sl' then begin
if (Key='-') and (Pos('-', tmpEditStr)>0) then begin
Key:=#0;
end;
end;
end;

//接下來 在Form上放一個按鈕
procedure TForm1.Button1Click(Sender: TObject);
var
aa:TdbGrid_temp;
begin
aa:= TdbGrid_temp.Create(Self);
aa.Parent := self;
aa.DataSource := DBGrid1.DataSource;
on keypress
end;
編輯記錄
luowy651 重新編輯於 2012-06-07 22:00:31, 註解 無‧
luowy651
高階會員


發表:257
回覆:313
積分:114
註冊:2003-04-09

發送簡訊給我
#2 引用回覆 回覆 發表時間:2012-06-08 13:27:41 IP:122.234.xxx.xxx 訂閱
如果不是在dbgrid上,而是在edit上,倒是可以用以下code实现:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
Edit1.MaxLength := 8;
if (Pos('.', Edit1.Text) >= 1) and (Key = '.') then //小数点只能出现一次
Key := #0;
if (pos('-', Edit1.Text) >= 1) and (Key = '-') then //负号只能出现一次
Key := #0;
if (Pos('-', Edit1.Text) < 1) and (Key = '-') then //用于不让负号出现在后面,只能出现在左边第一位
begin
Key := #0;
Edit1.Text := '-' Edit1.Text; //用于把负号自动放到第一位上
end;

end;
系統時間:2024-04-19 13:27:34
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!