请问怎样为TstringGrid加上SelLength SelStart 属性(100分)

  • 主题发起人 主题发起人 haiyuinfo
  • 开始时间 开始时间
H

haiyuinfo

Unregistered / Unconfirmed
GUEST, unregistred user!
[?] 我想对每个 TstringGrid.cells[i,j] 进行输入控制,怎样得到当前cell的
SelLength SelStart
 
TComponent
TControl
TWinControl
TCustomControl
...TCustomDrawGrid
.....TDrawGrid
.......TStringGrid
TCustomEdit
SelLength, SelStart 是 TCustomEdit 的属性,所以,楼主的想法恐怕比较难实现了。
 
怎样把TstringGrid的每个cell处理成一个TCustomEdit 使其成为一个对象,而不是string
 
这样好象要重写控件
 
有没有人愿意帮我重写
procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
 
谁要是能解决在指定的cell中只能录入浮点型数据,并只允许有2位小数,那么100分就归你
我就是要用tstringgrid ,不用cxgrid等代替品
 
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
tmpSgd: TStringGrid;
begin
tmpSgd := Sender as TStringGrid;
if ARow = 0 then
Exit;
case ARow mod 4 of
1, 2: tmpSgd.Canvas.Brush.Color := clSkyBlue;
0, 3: tmpSgd.Canvas.Brush.Color := clMoneyGreen;
end;
if (ACol < 1) and (ARow mod 2 = 1) then
tmpSgd.Canvas.Font.Style := [fsBold]
else
tmpSgd.Canvas.Font.Style := [];
Rect.Left := Rect.Left + 1;
Rect.Right := Rect.Right - 1;
Rect.Top := Rect.Top + 1;
Rect.Bottom := Rect.Bottom - 1;
tmpSgd.Canvas.FillRect(Rect);
tmpSgd.Canvas.TextOut(Rect.Left + 2, Rect.Top, tmpSgd.Cells[ACol, ARow]);
end;
 
在cell中只能录入浮点型数据,并只允许有2位小数
procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol,
ARow: Integer; const Value: String);
begin
try
StrToFloat(Value);
if Value[1] = '0' then
StringGrid1.Cells[ACol, ARow] := Copy(Value, 2, Length(Value)-1);
if (pos('.', Value) > 0) and (length(value)-pos('.', Value)>2) then
StringGrid1.Cells[ACol, ARow] := Copy(Value, 1, pos('.', Value)+2);
except
StringGrid1.Cells[ACol, ARow] := '';
end;
end;
 
to japhe 我不想用后判断的这种,我起初也是这样做的
唯一要解决的是怎样将Tstringgrid的每个cell当成TCustomEdit来做
TComponent
TControl
TWinControl
TCustomControl
...TCustomDrawGrid
.....TDrawGrid
.......TStringGrid
TCustomEdit
SelLength, SelStart 能否 在tstringgrid中实现?
这样我就可以通过下列方法控制用户录入
procedure FormatKeyPress(Sender:TObject;var Key:Char;Digits:integer);
var
myEdit:tsuiEdit;
begin
if (Sender is TSuiEdit) then
begin
myEdit:= Sender as Tsuiedit;
if (Key=#27) or (Key=#8) or (myEdit.Tag=0) then exit;
if myEdit.Tag=1 then
if not (Key in ['0'..'9','+','-'] ) then Key:=#0;
if myEdit.Tag=2 then
if not (Key in ['0'..'9','+','-','.'] ) then Key:=#0;

//控制+-
if (Key ='-') or (Key='+' ) then
begin
if ((Pos('-',myEdit.Text) > 0) or (Pos('+',myEdit.Text) > 0 )) and
(myEdit.SelLength=0 ) then Key:=#0;
if myEdit.SelStart > 0 then Key:=#0;
end;
//控制.
if (Key = '.') and (myEdit.Tag=2 ) then
begin
if (Pos('.',myEdit.Text)>0) then
begin
if (myEdit.SelStart+myEdit.SelLength<Pos('.',myEdit.Text))
or (myEdit.SelStart>=Pos('.',myEdit.Text)) then Key:=#0;
if myEdit.SelLength=0 then Key:=#0;
end;
if myEdit.SelStart=0 then Key:=#0;
if (Digits>0) and (myEdit.SelStart>0) and (myEdit.Tag=2 ) then
if length(myEdit.Text)-myEdit.SelStart-myEdit.SelLength>Digits then Key:=#0;
end;
if (Digits>0) and (myEdit.SelStart>0) and (myEdit.SelLength=0) and (myEdit.Tag=2 ) then
if (pos('.',myEdit.Text )>0) and (myEdit.SelStart>=pos('.',myEdit.Text)) then
if length(myEdit.Text)-pos('.',myEdit.Text )>=Digits then Key:=#0;
end;
end;
 
这个我就无能为里力了,sorry!
 
算了,还是不加了
 
后退
顶部