2015-01-31 32 views
0

我試圖改變DBLookupComboBox的可見文本(值),使用:如何更改Delphi中的DBLookupComboBox值?

DBLookupComboBox1.KeyValue:=S; 

DBLookupComboBox2.KeyValue:=X; 

如果鍵值和S都是字符串,然後一切工作正常,我可以設置值。

但是,當KeyValue是一個整數/ Int64(DB中的UID),並且X是一個Int64變量時 - 它不起作用,也沒有任何變化。

所以舉個例子,我需要在DBLookupComboBox1中設置「Amsterdam」,在DBLookupComboBox2中設置1500。 「阿姆斯特丹」來自用戶的「城市」字段,1500作爲UID。

我在做什麼錯誤?

感謝

回答

0

設置鍵值調用它在Delphi 7出現TDBLookupControl的SetKeyValue:

procedure TDBLookupControl.SetKeyValue(const Value: Variant); 
begin 
    if not VarEquals(FKeyValue, Value) then 
    begin 
    FKeyValue := Value; 
    KeyValueChanged; 
    end; 
end; 

procedure TDBLookupComboBox.KeyValueChanged; 
begin 
    if FLookupMode then 
    begin 
    FText := FDataField.DisplayText; 
    FAlignment := FDataField.Alignment; 
    end else 
    if ListActive and LocateKey then 
    begin 
    FText := FListField.DisplayText; 
    FAlignment := FListField.Alignment; 
    end else 
    begin 
    FText := ''; 
    FAlignment := taLeftJustify; 
    end; 
    Invalidate; 
end; 

正如你所看到的,你的變量x被用作LocateKey的一部分。

function TDBLookupControl.LocateKey: Boolean; 
var 
    KeySave: Variant; 
begin 
    Result := False; 
    try 
    KeySave := FKeyValue; 
    if not VarIsNull(FKeyValue) and FListLink.DataSet.Active and 
     FListLink.DataSet.Locate(FKeyFieldName, FKeyValue, []) then // << ---here 
    begin 
     Result := True; 
     FKeyValue := KeySave; 
    end; 
    except 
    end; 
end; 

進入這些程序和功能應該可以幫助您調試您的問題。所有位於DbCtrls單位..

+0

ty約翰。沒有轉換。在字符串組合(比方說DBLookupComboBox1)我使用名稱,並在整數組合(讓我們說DBLookupComboBox2),我只使用身份證號碼)。 所以在DBLookupComboBox1我希望它是例如:阿姆斯特丹 和DBLookupComboBox2我希望它是例如:1500. 我將編輯我的問題 – 2015-01-31 19:58:46

+0

我不應該說字符串轉換,編輯答案並刪除該引用。設置一個斷點並比較兩個值(將X作爲變體,將底層查找數據庫字段值作爲變體進行比較)。作爲另一個測試,將它們作爲字符串進行比較 – 2015-01-31 20:05:07

+0

的事情是,當我調試,我可以看到我需要的號碼進入Keyvalue,但它只是不顯示它在屏幕上... – 2015-01-31 20:16:11

相關問題