vb.net组合框返回另一列

时间:2016-11-02 23:00:24

标签: vb.net

我有两个组合框。第二个依赖于第一个。然后,我将选定的值作为对象传递给第二个表单。我的代码在这里工作得很好。但是我还需要根据第二个组合框(即cmbPlaasnaam)的选择将第三个对象传递给第二个形式(即plaasnopass)。但是,该值位于另一列中。我的代码运行没有错误,但第三个值没有传递给第二个表单。

Public Class ParskaartjieKiesFrm

Dim obj As New Parskaartjies
Private Sub ParskaartjieKiesFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'SkeduleringDatabasisDataSet.wingrd13' table. You can move, or remove it, as needed.
    Me.Wingrd13TableAdapter.Fill(Me.SkeduleringDatabasisDataSet.wingrd13)

    With CmbAliasnaam
        .BeginUpdate()
        .AutoCompleteMode = AutoCompleteMode.SuggestAppend
        .AutoCompleteSource = AutoCompleteSource.ListItems

        Dim qry = From zc As SkeduleringDatabasisDataSet.wingrd13Row In SkeduleringDatabasisDataSet.wingrd13 _
                  Select zc.Aliasnaam Distinct Order By Aliasnaam
        For Each ALIASNAAM As String In qry
            .Items.Add(ALIASNAAM)
        Next
        .EndUpdate()

    End With

    With CmbPlaasnaam
        .AutoCompleteMode = AutoCompleteMode.SuggestAppend
        .AutoCompleteSource = AutoCompleteSource.ListItems
        .Enabled = False
    End With

End Sub

Private Sub CmbAliasnaam_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbAliasnaam.SelectedIndexChanged

    obj.aliasnaampass = CmbAliasnaam.SelectedItem


    With CmbPlaasnaam
        .SelectedIndex = -1
        .Items.Clear()
        .Enabled = False

    End With


    If CmbAliasnaam.SelectedIndex > -1 Then

        CmbPlaasnaam.BeginUpdate()

        Dim Aliasnaam As String = CmbAliasnaam.Items(CmbAliasnaam.SelectedIndex).ToString

        Dim qry = From zc As SkeduleringDatabasisDataSet.wingrd13Row In SkeduleringDatabasisDataSet.wingrd13 _
                   Where zc.Aliasnaam = ALIASNAAM Select zc.Plaasnaam Distinct _
                   Order By Plaasnaam

        For Each plaasnaam As String In qry
            CmbPlaasnaam.Items.Add(plaasnaam)

        Next
        If CmbPlaasnaam.Items.Count > 0 Then
            CmbPlaasnaam.Enabled = True
        End If
        CmbPlaasnaam.EndUpdate()

    End If

End Sub


Private Sub CmbPlaasnaam_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbPlaasnaam.SelectedIndexChanged
    obj.plaasnaampass = CmbPlaasnaam.SelectedItem
    obj.plaasnopass = CmbAliasnaam.SelectedValue

    obj.Show()
    Me.Close()
End Sub

End Class

我该怎么办?

此致

1 个答案:

答案 0 :(得分:0)

根据你的评论,我知道你有从

加载的组合框
select a from t

但这会为您提供单个值,并且您想要加载

select a, b from t

并将两个值传递给下一个表单。

使用LINQ很有帮助!!修改它。但首先声明新类型

Public Class CboItem
    Public Property Display As String
    Public Property Value As String ' <-- any datatype
    ' ....  any number of properties here
End Class

 Dim qry = 
     (From zc As SkeduleringDatabasisDataSet.wingrd13Row 
         In SkeduleringDatabasisDataSet.wingrd13 _
     Where zc.Aliasnaam = ALIASNAAM 
     Select New CboItem() With { .Display = zc.Plaasnaam, .Value = zs.[...] }).
     Distinct(function(item)...).OrderBy(function(item)...)

 CmbAliasnaam.DisplayMember = "Display"
 CmbAliasnaam.ValueMember = "Value"
 CmbAliasnaam.DataSource = qry.ToList()

现在您可以传递给您的表单选定项目或任何您从中获得的项目。例如

Dim item As CboItem = TryCast(CmbAliasnaam.SelectedItem, CboItem)
If item IsNot Nothing Then     
    frm.SetSelected(item.Display, item.Value, item.anyOtherProperty)
End If
' or
frm.SomeProperty = item

如您所见,您可以传递可以存储在组合框中的对象。它不一定是字符串。