Lazarus 是否将变量作为单元格数据添加到StringGrid?
我有一个包含两个TEdit组件的表单。在另一个表单中,我希望使用两个TEDIT中的数据将一行添加到TStringGrid中。我该怎么做 这就是我到目前为止所做的:Lazarus 是否将变量作为单元格数据添加到StringGrid?,lazarus,delphi,Lazarus,Delphi,我有一个包含两个TEdit组件的表单。在另一个表单中,我希望使用两个TEDIT中的数据将一行添加到TStringGrid中。我该怎么做 这就是我到目前为止所做的: procedure TSecondForm.StartButtonClick(Sender: TObject); begin string1 := Edit1.Text; string2 := Edit2.Text; MainForm.StringGrid1.RowCount := MainForm.StringGrid
procedure TSecondForm.StartButtonClick(Sender: TObject);
begin
string1 := Edit1.Text;
string2 := Edit2.Text;
MainForm.StringGrid1.RowCount := MainForm.StringGrid1.RowCount + 1; // this adds the rows, but I don't know how to make it so that the two variables are inputed into two seperate cells
end;
在Delphi和FreePascal/Lazarus中,可以在增加
行数后使用该属性,例如:
procedure TSecondForm.StartButtonClick(Sender: TObject);
var
string1, string2: string;
row: integer;
begin
string1 := Edit1.Text;
string2 := Edit2.Text;
row := MainForm.StringGrid1.RowCount;
MainForm.StringGrid1.RowCount := row + 1;
MainForm.StringGrid1.Cells[0, row] := string1;
MainForm.StringGrid1.Cells[1, row] := string2;
end;
仅在FreePascal/Lazarus中,您可以交替使用TStringGrid.InsertRowWithValues()
方法:
procedure TSecondForm.StartButtonClick(Sender: TObject);
var
string1, string2: string;
begin
string1 := Edit1.Text;
string2 := Edit2.Text;
MainForm.StringGrid1.InsertRowWithValues(MainForm.StringGrid1.RowCount, [string1, string2]);
end;
谢谢你的回复,雷米。澄清一下,我知道Cells属性,但我只是在努力用我所有的变量实现Cells的逻辑。你最好用谷歌搜索一下TStringGrid教程——这里有很多。youtube上也有很多。太棒了,现在看起来很简单。