vb.net - 将数据绑定添加到ToolstripComboBox

时间:2014-07-17 03:13:02

标签: vb.net combobox toolstrip

我设法让我的ToolstripComboBox显示数据源(字典)。

但是现在我想将一个DataBinding添加到SelectedValue属性中,但我没有工作。

对于普通的ComboBox,它可以工作:/ ..

我的代码: tscbb_Test.ComboBox.DataBindings.Add(" SelectedValue",My.Settings," mySettings&#34中的设置;)

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我刚尝试了这个,它对我来说很有效:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim table As New DataTable

    With table.Columns
        .Add("ID", GetType(Integer))
        .Add("Name", GetType(String))
    End With

    With table.Rows
        .Add(1, "Peter")
        .Add(2, "Paul")
        .Add(3, "Mary")
        .Add(4, "John")
    End With

    Me.BindingSource1.DataSource = table
    Me.BindingSource2.DataSource = table

    With Me.ToolStripComboBox1.ComboBox
        .DisplayMember = "Name"
        .ValueMember = "ID"
        .DataSource = Me.BindingSource1
        .DataBindings.Add("SelectedValue", My.Settings, "ToolStripSelectedValue")
    End With

    With Me.ComboBox1
        .DisplayMember = "Name"
        .ValueMember = "ID"
        .DataSource = Me.BindingSource2
        .DataBindings.Add("SelectedValue", My.Settings, "FormSelectedValue")
    End With
End Sub

我可以运行项目,在每个ComboBox中选择一个不同的项目,关闭项目,再次运行它,然后再次选择我之前选择的项目,表示设置必须已保存。

我刚尝试了两个Keys类型的设置,并按预期工作:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim keys = [Enum].GetValues(GetType(Keys))

    Me.BindingSource1.DataSource = keys
    Me.BindingSource2.DataSource = keys

    With Me.ToolStripComboBox1.ComboBox
        .DataSource = Me.BindingSource1
        .DataBindings.Add("SelectedItem", My.Settings, "ToolStripSelection")
    End With

    With Me.ComboBox1
        .DataSource = Me.BindingSource2
        .DataBindings.Add("SelectedItem", My.Settings, "FormSelection")
    End With
End Sub