Search code examples
vb.netcomboboxtoolstrip

vb.net - Add Databinding to a ToolstripComboBox


i just managed to get my ToolstripComboBox to display a Datasource (Dictionary).

But now i want to add a DataBinding to the SelectedValue Property, but i doesn't work.

For a normal ComboBox it works :/..

My Code: tscbb_Test.ComboBox.DataBindings.Add("SelectedValue", My.Settings, "Setting from mySettings")

Can somebody help?


Solution

  • I just tried this and it worked perfectly for me:

    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
    

    I could run the project, select a different item in each ComboBox, close the project, run it again and the items I previously selected were selected again, indicating that the settings must have been saved.

    I just tried this code with two settings of type Keys and it worked as expected:

    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