2014-04-02 70 views
1

尊敬的各位,並提前致謝。將兩個列表合併爲1個下拉框

問題:

而不必爲名單A和名單B.我想這兩個列表爲1個清單合併(他們不涉及),並顯示它們給用戶兩個下拉框。然後將其分成兩部分,以便我可以存儲相關信息。

列表1

Machines 
1. Machine x 
2. Machine y 

表2測試類型

1. Test Type ab 
2. Test Type ac 
3. Text Type ad. 

使機器1可以做測試AB型和交流。機器2可以測試交流和廣告類型。它將被存儲在3個不同的表(不是真的,但只是認爲它會)。 2個表將包含列表,第三個將包含兩個列表之間的關係。即哪些項目從列表1對了從列表2等

向該用戶項目將被顯示如下

Machine X - ab 
Machine x - ac 
Machine y - ac 
Machine y - ad 

然後,用戶將選擇1從列表中,然後我將解碼選擇兩個項目。

我到目前爲止的想法是根據需要使用位(和/或)。

將有三個功能

public function joinAB(a as long, b as long) as long 

end function 

Public function getA(ab as long) as long 

end function 

public function getB(ab as long)as long 
end function 

所以我只想澄清這是不加入文聯,但加盟/在這兩個列表中的個別項目的分裂的ID。

其他人有任何其他想法。這將在我繼承的遺留系統(VB6)中完成。我的VB6編碼技能高於平均水平。

感謝您提供任何幫助/代碼片段或一般建議。

如果您需要更多信息,請讓我知道。

+0

我編輯了自己的冠軍。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

1

假設a和b是數值變量爲您3個職能建議我會使用的.ItemData()屬性在所述組合列表就像Mark和使用分裂和物品保持,以獲得獨立的部分:

Public Function joinAB(a As Long, b As Long) As Long 
    joinAB = a * 100 + b 
End Function 

Public Function getA(ab As Long) As Long 
    getA = ab \ 100 
End Function 

Public Function getB(ab As Long) As Long 
    getB = ab Mod 100 
End Function 

這假定是b永遠不會超過100更高,並且也不a或b將是負的

如果a和b是字符串變量的話,我會表現出加盟字符串作爲他的ComboBox文本int和分裂所選擇的文本,以獲得seprate部分

Public Function joinAB(a As String, b As String) As String 
    joinAB = a & " - " & b 
End Function 

Public Function getA(ab As String) As String 
    getA = Left$(ab, InStr(ab, " - ") - 1) 
End Function 

Public Function getB(ab As String) As String 
    getB = Mid$(ab, InStr(ab, " - ") + 3) 
End Function 
1

化妝用途,其中的關係映射,如果關係有一個唯一的ID,那麼你可以利用該加入第三表/分割列表....

或爲我們提供表結構和數據...

1

這可能有幾個解決方案。

最簡單的方法是在連接表上創建一個唯一的32位整數字段。這可以嵌入到VB6 ListBox的ItemData屬性中。

此一對夫婦的輔助函數有:

Private Sub AddListBoxItem(ByRef lst as ListBox, ByVal in_nKey As Long, ByRef in_sDisplay As String) 
    With lst 
     .AddItem in_sDisplay 
     .ItemData(.NewIndex) = in_nKey 
    End With 
End Sub 

Private Function GetSelectedListBoxKey(ByRef in_lst As ListBox) As Long 
    With in_lst 
     GetSelectedListBoxKey = .ItemData(.ListIndex) 
    End With 
End Function 

至於實現你的功能,我想簡單地用兩個集合。

m_col_A_B_to_AB將被鍵入A &「_」& B返回AB。 m_col_AB_to_A_B將被鎖定以AB返回A和B.

助手功能將是:

Private Sub AddRow(ByVal in_nA As Long, ByVal in_nB As Long, ByVal in_nAB As Long) 
    Dim an(0 To 1) As Long 
    an(0) = in_nA 
    an(1) = in_nB 
    m_col_A_B_to_AB.Add an(), CStr(in_nAB) 
    m_col_AB_to_A_B.Add in_nAB, CStr(in_nA) & "_" & CStr(in_nB) 
End Sub 

Private Sub Get_A_B(ByVal in_nAB As Long, ByRef out_nA As Long, ByRef out_nB As Long) 

    Dim vTmp As Variant 
    vTmp = m_col_A_B_to_AB.Item(CStr(in_nAB)) 

    out_nA = vTmp(0) 
    out_nB = vTmp(1) 

End Sub 

Private Function GetA(ByVal in_nAB As Long) As Long 
    Get_A_B in_nAB, GetA, 0& 
End Function 

Private Function GetB(ByVal in_nAB As Long) As Long 
    Get_A_B in_nAB, 0&, GetB 
End Function 

Private Function JoinAB(ByVal in_nA As Long, ByVal in_nB As Long) As Long 

    JoinAB = m_col_AB_to_A_B.Item(CStr(in_nA) & "_" & CStr(in_nB)) 

End Function