1

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?

mademyday
  • 83
  • 2
  • 9
  • 1
    Saying "it doesn't work" is never adequate. Always explain what happens and, if it's not obvious, how that differs from your expectation. If there's an error message, provide it. – jmcilhinney Jul 17 '14 at 03:34
  • Sorry mate.. Um just nothing happens, exact this code works for a normal combobox, but not for a toolstrip combobox. If i change the value, its again the default value at start, i admit i forgot to test whether it even saves the new value to the settings. But for a normal combobox its working too, that kinda suxx :/.. – mademyday Jul 17 '14 at 03:38

1 Answers1

1

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
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Sorry Bro, but it doesnt work for me, i just made a new form, added 2 bindingsources from the toolbox, and the toolstripcombobox, it doesn't show any value and so the binding couldn't be tested. At least i could make the values visible with turning Combobox.BindingContext = Me.BindingContext. Don't know what im doin wrong. – mademyday Jul 17 '14 at 14:42
  • Lol, i just made a new project, then it worked, but i dont know why its not working in my main project Oo!? Well ill look, if you have more suggestions, it would be nice to say them ;)! Thank you anyways so far! PS: Maybe it' because im binding to an Enum? ToolstripComboBox.ComboBox.DataSource = System.Enum.GetValues(GetType(EnumName)) – mademyday Jul 17 '14 at 14:50
  • My Code now (does not work :() (Don't know how to make Vb-Code Tags :/) Me.BindingSource1.DataSource = System.Enum.GetValues(GetType(Keys)) tscb_Keys.ComboBox.BindingContext = Me.BindingContext tscb_Keys.ComboBox.DataSource = Me.BindingSource1 tscb_Keys.ComboBox.DataBindings.Add("SelectedValue", My.Settings, "SettingInMySettingsAs Integer?") – mademyday Jul 17 '14 at 15:05
  • PS Again: It seems like, that the Binding thingy doesnt save the value to the mysettings, is it because its not on top, means i have more toolstripitems and the combobox as a sub item? – mademyday Jul 17 '14 at 15:18
  • Your binding doesn't really make sense for a number of reasons. Firstly, do you really have a setting of type Nullable(Of Integer)? – jmcilhinney Jul 17 '14 at 23:40
  • I want to have the Keys Enumeration as Datasource and the possibilty that a User can Select one key + that its stored in the mysettings. – mademyday Jul 18 '14 at 01:04
  • I asked you a specific question and you didn't answer it. To the best of my knowledge, settings are not nullable. If you want to store a `Keys` value then create a setting of type `Keys`. Assign a list of `Keys` values to the `DataSource` of your `ComboBox` and then bind your setting to the `SelectedItem`, NOT the `SelectedValue`. `Keys.None` is what indicates no value. – jmcilhinney Jul 18 '14 at 01:47
  • Okay ill try, thank you! I couldnt answer, because i didnt know what u need to know, as far as i know, i dont have that nullable type of integer. – mademyday Jul 18 '14 at 01:49
  • If some asks you a question and you don't know the answer then say that you don't know the answer. If you say nothing then, as far as we can tell, you've just ignored the question. We only know what you tell us so you have to tell us everything that's relevant. – jmcilhinney Jul 18 '14 at 02:42
  • I've added a second code example that works with `Keys` values. – jmcilhinney Jul 18 '14 at 02:47
  • Hey man, i have one thing left, for me the thing is working for 99% but sometimes it doesnt save the settings after i changed the value in the combobox and instantly exit the form. I have to focus another control before leaving, so that it saves correctly. At the moment i used the workaround, that i focus another control, when the selectedindex changed event is fired. – mademyday Jul 19 '14 at 04:10
  • The `Add` method is overloaded and allows you to specify when the data source is updated. I believe that the default is `OnValidation`, which is why it occurs when focus changes. You can specify `OnPropertyChanged`, so it occurs as soon as the selection changes. – jmcilhinney Jul 19 '14 at 04:19