6

What I tried to achieve is like setting ItemIndex to -1 as like in normal ComboBox, so that the selected item will be cleared.

There is no ItemIndex for DBLookupComboBox, and the Text property is read only. So how do I clear the selected item? Please help. Many thanks in advance.

PS: Closing and reopening the DBLookupComboBox->ListSource DataSet does not help.

Kw Choy
  • 129
  • 1
  • 1
  • 10
  • 1
    I solved the problem with DBLookupComboBox->KeyValue = ""; since the data type in the ListFields are of type String. Thanks to Kutsoff and John Easley for giving me a clue to the solutions. :) – Kw Choy Jan 05 '12 at 14:40

4 Answers4

5

Two ways:

1) at runtime: DbLookupComboBox.KeyValue := null;

2) at design-time: set the DbLookupComboBox.NullValueKey property to DEL (or whatever key you want to use to null the value during runtime)

John Easley
  • 1,551
  • 1
  • 13
  • 23
  • 1) Throws EVariantTypeCastError. 2) Causes an Access Violation. Thanks anyway. – Kw Choy Jan 05 '12 at 14:19
  • 1) usually happens when the lookup key and keyfields mismatch. If your keyfield is a string, then yes, you can assign ''. 2) I've never experienced this..sorry.. – John Easley Jan 05 '12 at 18:00
4

You can use

DBLookupComboBox.KeyValue := -1;
kutsoff
  • 325
  • 2
  • 7
  • Setting KeyValue does reset the selected item, but throws EVariantTypeCastError exception. I'm using C++Builder 2009, not sure if this has anythong to do with the exception. Thanks anyway. – Kw Choy Jan 05 '12 at 14:17
2

DBLookupComboBox represents the value of a field of a database. So you have to clear/change the value of the field to clear/change the value of the DBLookupComboBox by code.

Andreas
  • 1,334
  • 1
  • 10
  • 21
0

if first time you set dblokupcombobox KeyField in property is 'city' //example

if you want to clear selected item you can use this

procedure TFtreset.BitBtn6Click(Sender: TObject);
begin
  dblookupcombobox2.KeyValue:='city';
end;
Walery Strauch
  • 6,792
  • 8
  • 50
  • 57