问题是这样,由于程序的需要我在StringGrid的格子里放东西,有几列不是写进去的,是用ComboBox选择上去的,我是让StringGrid的Options的goEditing为True,然后控制放有ComboBox的列不可以写,但这样会有个小问题,就是我程序运行的时候,点击我隐藏着ComboBox的那个列的第二行时(第一行是表头)不会显示ComboBox,只有先到第二行,然后再回到第一行才行。如果我把StringGrid的Options的goEditing改为False时,就不会了
所以我想这样,想把StringGrid的Options的goEditing改为False,然后控制需要写的列为可以写,不知道怎么弄?我的代码如下:
procedure Txinzengfm.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
var
r: TRect;
begin
if StringGrid1.Col=3 then
begin
ComboBox3.Visible:=True;
r:=StringGrid1.CellRect(3,ARow);
StringGrid1.DefaultRowHeight:=ComboBox3.Height;
ComboBox3.Left:=StringGrid1.Left + R.Left + 1;
ComboBox3.Top := StringGrid1.Top + R.Top + 1;
ComboBox3.Width := R.Right - R.Left; //宽度跟着StringGrid1的相应列的宽度
end
else
ComboBox3.Visible:=False;
if StringGrid1.Col=4 then
begin
ComboBox4.Visible:=True;
r:=StringGrid1.CellRect(4,ARow);
StringGrid1.DefaultRowHeight:=ComboBox3.Height;
ComboBox4.Left:=StringGrid1.Left + R.Left + 1;
ComboBox4.Top := StringGrid1.Top + R.Top + 1;
ComboBox4.Width := R.Right - R.Left;
end
else
ComboBox4.Visible:=False;
end;procedure Txinzengfm.ComboBox3Change(Sender: TObject);
begin
StringGrid1.Cells[3,StringGrid1.Row]:=ComboBox3.Items[ComboBox3.ItemIndex];
end;procedure Txinzengfm.ComboBox4Change(Sender: TObject);
begin
StringGrid1.Cells[4,StringGrid1.Row]:=ComboBox4.Items[ComboBox4.ItemIndex];
end;procedure Txinzengfm.StringGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if StringGrid1.Col=3 then //让放有ComboBox的列为只读
begin
Key:=#0;
end;
if StringGrid1.Col=4 then //让放有ComboBox的列为只读
begin
Key:=#0;
end;
if stringgrid1.col = 6 then //控制某列只能是数字
begin
if not (key in ['0','1','2','3','4','5','6','7','8','9','.',#13,#8]) then
key := #0;
end;
所以我想这样,想把StringGrid的Options的goEditing改为False,然后控制需要写的列为可以写,不知道怎么弄?我的代码如下:
procedure Txinzengfm.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
var
r: TRect;
begin
if StringGrid1.Col=3 then
begin
ComboBox3.Visible:=True;
r:=StringGrid1.CellRect(3,ARow);
StringGrid1.DefaultRowHeight:=ComboBox3.Height;
ComboBox3.Left:=StringGrid1.Left + R.Left + 1;
ComboBox3.Top := StringGrid1.Top + R.Top + 1;
ComboBox3.Width := R.Right - R.Left; //宽度跟着StringGrid1的相应列的宽度
end
else
ComboBox3.Visible:=False;
if StringGrid1.Col=4 then
begin
ComboBox4.Visible:=True;
r:=StringGrid1.CellRect(4,ARow);
StringGrid1.DefaultRowHeight:=ComboBox3.Height;
ComboBox4.Left:=StringGrid1.Left + R.Left + 1;
ComboBox4.Top := StringGrid1.Top + R.Top + 1;
ComboBox4.Width := R.Right - R.Left;
end
else
ComboBox4.Visible:=False;
end;procedure Txinzengfm.ComboBox3Change(Sender: TObject);
begin
StringGrid1.Cells[3,StringGrid1.Row]:=ComboBox3.Items[ComboBox3.ItemIndex];
end;procedure Txinzengfm.ComboBox4Change(Sender: TObject);
begin
StringGrid1.Cells[4,StringGrid1.Row]:=ComboBox4.Items[ComboBox4.ItemIndex];
end;procedure Txinzengfm.StringGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if StringGrid1.Col=3 then //让放有ComboBox的列为只读
begin
Key:=#0;
end;
if StringGrid1.Col=4 then //让放有ComboBox的列为只读
begin
Key:=#0;
end;
if stringgrid1.col = 6 then //控制某列只能是数字
begin
if not (key in ['0','1','2','3','4','5','6','7','8','9','.',#13,#8]) then
key := #0;
end;
而非让StringGrid为不可以写,才行?我要怎么样让整个StringGrid为不可以写,然后用代码实现某几列可以写?
begin
if stringgrid1.Col =3 then
stringgrid1.Options :=stringgrid1.Options +[goediting] else
stringgrid1.Options :=stringgrid1.Options -[goediting];
end;