我有一个包含两个TEdit组件的表单。在另一种形式上,我希望将来自两个TEdits的数据添加到TStringGrid。我该怎么做?
这是我到目前为止所拥有的:
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;
答案 0 :(得分:1)
在Delphi和FreePascal / Lazarus中,您可以在增加RowCount
后使用TStringGrid.Cells
属性,例如:
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;