我在DB中有一个表格,其中包含有关某些商品的信息。商品可以填写,因此我们可以添加文字。因此,我想动态生成与表中信息相关的复选框列表,甚至一些复选框必须与TEdit组件一起使用才能为此项添加文本。那我该怎么办呢?我应该使用什么组件?我发现TTreeView已经足够了,但它不允许在复选框附近“绘制”TEdit。我正在使用Delphi 2010.在此先感谢!希望对你有所帮助!
答案 0 :(得分:6)
如果我正确地阅读了您的问题,您希望根据表格的内容在表单上创建一些控件。在下面的示例中,我假设您要根据TDBGrid中当前记录的内容执行此操作,因此您必须根据需要进行调整。
该示例假定具有TDBGrid和TPanel(Panel1)的表单将保存在运行时创建的控件。
TDBGrid将连接到TDataSource组件,并将连接到表/查询的某个TDataSet后代及其信息。 TDataSource有一个OnDataChanged事件。当字段中的数据更改或数据集中的当前记录更改时,将触发此事件。因此,您可以使用它在当前记录更改时更改控件。
procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
var
i: Integer;
Chk: TCheckBox;
Edit: TEdit;
begin
// When the Field is assigned, the call is the result of a change in the field.
// When the Field is unassigned, the call is the result of changing the current record.
if Assigned(Field) then
Exit;
// Remove controls on panel
for i := Panel1.ControlCount - 1 downto 0 do
Panel1.Controls[i].Free;
// Add controls on panel for current record
if True then // Replace this with condition based on contents of current record (if any!)
begin
Chk := TCheckBox.Create(Self); // Set Owner, so it is freed when form is closed.
Chk.Parent := Panel1; // Set Parent, so the control is shown.
Chk.Left := FLeftIndent; // Create FLeftIndent as a member field of the form, set value in OnCreate.
Chk.Top := FNextTop; // Create FNextTop as a member field of the form.
Inc(FNextTop, FSpacing); // Create FSpacing as a member field of the form, set value in OnCreate.
if True then // Replace this with condition that dictates creation of Edit
begin
Edit := TEdit.Create(Self);
Edit.Parent := Panel1;
Edit.Left := Chk.Left + Chk.Width + FSpacing;
Edit.Top := Chk.Top; // Add offset as needed for proper alignment of CheckBox and Edit.
end;
end;
end;
请注意,如果您在表单上没有任何其他复选框或编辑内容,则必须自己包含正确的vcl单元。最简单的方法是将它们放在表单上,保存表单,然后再次删除控件。
答案 1 :(得分:1)
尝试this关于在runtimehpe上创建复选框的示例,它可以帮助您动态修改位置。您可以创建一个新组件,其中包含带有Edit的复选框,或者在您需要的地方动态创建TEdit。
考虑创建一个TCheckbox数组和一个TEdit数组,并使用IsEditNeeded布尔函数设置每个编辑的可见属性,如果需要编辑字段,您可以在其中编码条件。
答案 2 :(得分:0)
我很想使用像ExGridView这样的网格视图,并让它绘制我的复选框,并为我执行每行的“编辑”控件。但是,如果你真的想要一个编辑框而不是网格,你也可以尝试一种控制网格方法(1个复选框+ 1个编辑控件,在控制网格中)。