none
Save and Retieve ComboBox Value - Windows Forms RRS feed

  • Question

  • I am writing to determine if it is possible to save, and retrieve, a selected value of a Combo Box inside a Windows Forms application with Visual Basic .Net - similar to how Session variables are utilized to save and retrieve values with a Web application between sessions;

    The goal is to populate the default value of a Combo Box - based on the User's last selected value;

    Thanks in advance for any insight;

    Best regards - Rob


    robert hellestrae

    Monday, October 22, 2012 3:37 PM

Answers

  • You can use ComboBox.SelectedValue to set and get value in ComboBox. Or you can also use ComboBox.SelectedIndex or ComboBox.SelectedItem depending on your requirement.

    'Set value in ComboBox
    ComboBox1.SelectedValue = "SomeValue"
    'ComboBox1.SelectedIndex = 0 
    
    'Get Value from ComboBox
    Dim str as String = ComboBox1.SelectedValue
    


    Gaurav Khanna | Microsoft VB.NET MVP


    • Edited by Khanna Gaurav Monday, October 22, 2012 6:04 PM
    • Marked as answer by rrobhelle Monday, October 22, 2012 7:02 PM
    Monday, October 22, 2012 6:03 PM
  • To use the SelectedValue, you cannot just add some value to comboBox, but this property is used of some kind of a background layer of selectedItem.

    So to demonstrate how to use SelectedValue, I will create a custom class, fill the List<T> with the class properties, and used one property for SelectedItem, and another fot SelectedValue (but you have to explicitly define which is DisplayedMember, and ValueMember):

    Dim comboBox1 As New ComboBox()
    Dim persons As New List(Of Person)()
    persons.Add(New Person() With { _
    	.Id = 1, _
    	.Name = "Name 1" _
    })
    persons.Add(New Person() With { _
    	.Id = 2, _
    	.Name = "Name 2" _
    })
    comboBox1.DataSource = persons
    comboBox1.DisplayMember = "Name"' property name
    comboBox1.ValueMember = "Id"' property name
    'to retrieve a selectedValue from comboBox subscribe to SelectedIndexChnanged event and do inside:
    Dim p As Person = TryCast(comboBox1.SelectedItem, Person)
    Dim id As Integer = p.Id
    Dim name As String = p.Name



    Mitja

    • Edited by Mitja Bonca Monday, October 22, 2012 6:16 PM
    • Marked as answer by rrobhelle Monday, October 22, 2012 7:02 PM
    Monday, October 22, 2012 6:15 PM
  • If you want to show last selectedValue, you will have to save it some where, or into app.config file, or you can create a temp file somewhere on the disc (best to create it inside the application folder), and write data into it (each time when selected new value, or just before closing the form), and when re-open it, read data back from the file, and set it as selectedValue.

    Mitja

    • Marked as answer by rrobhelle Monday, October 22, 2012 7:02 PM
    Monday, October 22, 2012 6:27 PM
  • You can use Settings file as suggested in following links

    http://www.dondraper.com/2011/01/easily-save-and-retrieve-application-and-user-settings-in-vb-net-or-c-apps/
    http://www.daveoncsharp.com/2009/07/using-the-settings-file-in-csharp/

    If you have multiple field/properties then you can use XML Serialization or Binary Serialization, and save whole class in file

    http://gauravsofts.blogspot.in/2008/07/xml-serialization-in-vbnet.html


    Gaurav Khanna | Microsoft VB.NET MVP

    • Marked as answer by rrobhelle Monday, October 22, 2012 8:15 PM
    Monday, October 22, 2012 7:03 PM

All replies

  • You can use ComboBox.SelectedValue to set and get value in ComboBox. Or you can also use ComboBox.SelectedIndex or ComboBox.SelectedItem depending on your requirement.

    'Set value in ComboBox
    ComboBox1.SelectedValue = "SomeValue"
    'ComboBox1.SelectedIndex = 0 
    
    'Get Value from ComboBox
    Dim str as String = ComboBox1.SelectedValue
    


    Gaurav Khanna | Microsoft VB.NET MVP


    • Edited by Khanna Gaurav Monday, October 22, 2012 6:04 PM
    • Marked as answer by rrobhelle Monday, October 22, 2012 7:02 PM
    Monday, October 22, 2012 6:03 PM
  • To use the SelectedValue, you cannot just add some value to comboBox, but this property is used of some kind of a background layer of selectedItem.

    So to demonstrate how to use SelectedValue, I will create a custom class, fill the List<T> with the class properties, and used one property for SelectedItem, and another fot SelectedValue (but you have to explicitly define which is DisplayedMember, and ValueMember):

    Dim comboBox1 As New ComboBox()
    Dim persons As New List(Of Person)()
    persons.Add(New Person() With { _
    	.Id = 1, _
    	.Name = "Name 1" _
    })
    persons.Add(New Person() With { _
    	.Id = 2, _
    	.Name = "Name 2" _
    })
    comboBox1.DataSource = persons
    comboBox1.DisplayMember = "Name"' property name
    comboBox1.ValueMember = "Id"' property name
    'to retrieve a selectedValue from comboBox subscribe to SelectedIndexChnanged event and do inside:
    Dim p As Person = TryCast(comboBox1.SelectedItem, Person)
    Dim id As Integer = p.Id
    Dim name As String = p.Name



    Mitja

    • Edited by Mitja Bonca Monday, October 22, 2012 6:16 PM
    • Marked as answer by rrobhelle Monday, October 22, 2012 7:02 PM
    Monday, October 22, 2012 6:15 PM
  • Thanks for the feedback;

    In addition to setting, getting the value in the ComboBox - if the user closes the Windows Form application, and brings up the Windows Form a second time, the objective is to display the last selected ComboBox value as the initial default (or first) value in the ComboBox;

    Basically, retain values across instances of the Windows Form application;


    robert hellestrae

    Monday, October 22, 2012 6:16 PM
  • If you want to show last selectedValue, you will have to save it some where, or into app.config file, or you can create a temp file somewhere on the disc (best to create it inside the application folder), and write data into it (each time when selected new value, or just before closing the form), and when re-open it, read data back from the file, and set it as selectedValue.

    Mitja

    • Marked as answer by rrobhelle Monday, October 22, 2012 7:02 PM
    Monday, October 22, 2012 6:27 PM
  • You can use Settings file as suggested in following links

    http://www.dondraper.com/2011/01/easily-save-and-retrieve-application-and-user-settings-in-vb-net-or-c-apps/
    http://www.daveoncsharp.com/2009/07/using-the-settings-file-in-csharp/

    If you have multiple field/properties then you can use XML Serialization or Binary Serialization, and save whole class in file

    http://gauravsofts.blogspot.in/2008/07/xml-serialization-in-vbnet.html


    Gaurav Khanna | Microsoft VB.NET MVP

    • Marked as answer by rrobhelle Monday, October 22, 2012 8:15 PM
    Monday, October 22, 2012 7:03 PM