开发者

Delphi - How To Auto Highlight 1st entryIn A DBLookupListBox On Form Creation

How do I auto highlight the 1st entry in a DBLookupListBox without the end user highlighting it.

procedure开发者_开发知识库 TForm2.FormCreate(Sender: TObject);
begin
  Form2.ActiveControl := DBLookupListBox1;
end;

But this doesn't work, I've also tried DBLookupListBox1.setfocus on form create, but this gives an error, because the DBLookupListBox is not created yet.

Thanks

-Brad


I haven't tested this, but I would assume that you should use SetFocus in the form's OnShow event to activate the control.

procedure TForm2.FormShow(Sender: TObject);
begin
  DBLookupListBox1.SetFocus;
end;


Setting a default value is a bit more complicated because the DBLookupListBox is DB-aware. One approach is to set the default values in the DataSets OnNewRecord event or AfterInsert event:

procedure TMyDataModule.cdsMyClientDataSetNewRecord(DataSet: TDataSet);
begin
  cdsMyClientDataSetMYPERSISTENTFIELDNAME.Value := 0;
end;


If you still would like to do this from the Form:

procedure TForm2.FormShow(Sender: TObject);
const
  DEFAULT = 0;
var
  S: String;
begin
  S := DBLookupListBox1.DataField;

  if DBLookupListBox1.DataSource.DataSet.FieldByName(S).IsNull then
  begin 
    DBLookupListBox1.DataSource.DataSet.Edit;
    DBLookupListBox1.DataSource.DataSet.FieldByName(S).Value := DEFAULT;
    DBLookupListBox1.DataSource.DataSet.Post;
  end;
end;


IMHO:
Setting default values should be considered as business logic and therefore belongs in the DataModule.

Setting the appropriate focus is GUI-logic and should be done in the form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜