StringGrid问题(附源码) ( 积分: 100 )

Q

qqjm

Unregistered / Unconfirmed
GUEST, unregistred user!
用StringGrid显示一数组,内容是自己画上去的,可以对数据进行编辑,编辑完成后按回车保存。
这些我都做到了,但是出了一个问题。

如果我不保存的数据(不按回车键),则输入的内容还显示在上面,如果输入的值的长度小于原来数据的长度是没有问题的,但长于原数据的话,就会显示出来。

我想问的是如何删除输入的数据(输出空格的方法就不用说了)。

代码如下:
uStringGrid.dfm
---------------------------------------------
object Form1: TForm1
Left = 192
Top = 114
Width = 870
Height = 640
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object StringGrid1: TStringGrid
Left = 16
Top = 16
Width = 793
Height = 457
ColCount = 6
DefaultRowHeight = 18
RowCount = 102
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goEditing, goThumbTracking]
ParentFont = False
TabOrder = 0
OnDrawCell = StringGrid1DrawCell
OnGetEditMask = StringGrid1GetEditMask
OnGetEditText = StringGrid1GetEditText
OnKeyPress = StringGrid1KeyPress
OnSetEditText = StringGrid1SetEditText
ColWidths = (
64
425
217
64
64
64)
end
object Button1: TButton
Left = 296
Top = 552
Width = 89
Height = 25
Caption = 'scroll to Bottom'
TabOrder = 1
OnClick = Button1Click
end
object Button2: TButton
Left = 296
Top = 504
Width = 75
Height = 25
Caption = 'Row Heigh'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
ParentFont = False
TabOrder = 2
OnClick = Button2Click
end
object Edit1: TEdit
Left = 88
Top = 504
Width = 121
Height = 21
TabOrder = 3
Text = '8'
end
object Button3: TButton
Left = 216
Top = 504
Width = 75
Height = 25
Caption = 'Font Size'
TabOrder = 4
OnClick = Button3Click
end
object Edit3: TEdit
Left = 664
Top = 512
Width = 121
Height = 21
TabOrder = 5
end
end
======================================================================================

uStringGrid.pas
------------------------
unit uStringGrid;

interface

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

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Button3: TButton;
Edit3: TEdit;
procedure StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
const Value: String);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure StringGrid1GetEditText(Sender: TObject; ACol, ARow: Integer;
var Value: String);
procedure StringGrid1GetEditMask(Sender: TObject; ACol, ARow: Integer;
var Value: String);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure StringGrid1KeyPress(Sender: TObject; var Key: Char);
private
iList:array[0..100] of array[0..4] of integer;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol,
ARow: Integer; const Value: String);
begin

if Edit3.Tag = 1 then
begin
Edit3.Text := 'Begin Save';
iList[Arow-1][ACol-1] := StrToInt(Trim(Value));
Edit3.Text := 'Save Complete';
Edit3.Tag := 0;
end;

end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if ARow> 0 then
begin
if ACol > 0 then
self.StringGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2,inttostr(iList[aRow-1][ACol-1]))
else
self.StringGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2,inttostr(ARow-1))

end else
begin
if ACol > 0 then
begin
self.StringGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2,inttostr(ACol-1) )
end;
end;

end;

procedure TForm1.StringGrid1GetEditText(Sender: TObject; ACol,
ARow: Integer; var Value: String);
begin
Edit3.Text := 'Begin Edit';
if Edit3.Tag = 1 then
begin
Edit3.Text := 'Save Cancel !'+ Edit3.Text;
Edit3.Tag := 0;
end;
try
Value := inttostr(iList[Arow-1][ACol-1]);
Except
ShowMessage('输入错误!');
end;
end;

procedure TForm1.StringGrid1GetEditMask(Sender: TObject; ACol,
ARow: Integer; var Value: String);
begin
// Value := '00000000'
end;

procedure TForm1.FormCreate(Sender: TObject);
var
i,j:integer;
begin
//初始化数组
for i:= Low(iList) to High(iList) do
begin
for j:= Low(iList) to High(iList) do
begin
iList[j]:= i*5 + j;
end;
end;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
StringGrid1.Perform(WM_VSCROLL,SB_BOTTOM,0);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
StringGrid1.DefaultRowHeight := StringGrid1.Font.Size - StringGrid1.Font.Height ;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
StringGrid1.Font.Size := Strtoint(Self.Edit1.Text);
end;

procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char);
begin
case key of
#13:
begin
Edit3.Text := 'Save';
Edit3.Tag := 1;
end;
end;

end;

end.
-----------------------------------
 
stringgrid需要手工控制各個cell的值,及其他的控制
 
怎么控制,用那些属性或函数进行控制?
可否举个例子说明?
 
试试:TStringAlignGrid
它有OnAfterEdit 事件,这里处理应该可以
 
to: redsky.l
谢了,不过我不想程序用太多第三方的控件,特别是没有开源的控件。
 
TStringAlignGrid
是开源的控件,你可以参看它的代码,给GRID加上OnAfterEdit ,也行呀
 
找到原因了,这是因为DrawCell事件在重画这个单元格的时候并没有对整个单元格进行绘制,用白色重新填充这个单元格就行了!
 

Similar threads

顶部