Question : Save and display listbox / comboBox items in Data Structure

Hi,

I'm trying to save a collection of controls in a Data Structure, but I'm having a problem with listboxes and combo boxes. What would be the correct code for grabbing a selected item, saving it to file using a Data Structure and then displaying it again when that file is open again?
Public Text3 As String
and
MD.Text3
are data items I use to save the contents of the listbox.

Any help much appreciated.

Cheers

MC
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
Public Structure MyData
         Public Text1 As String
         Public Text2 As String
         Public Text3 As String
        Public Option1 As Boolean
        Public Option2 As Boolean
    End Structure
 
    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Dim MD As New MyData
        MD.Text1 = TextBox2.Text
        MD.Text2 = TextBox3.Text
        MD.Text3 = ListBox1.SelectedItem
        MD.Option1 = Me.CheckBox1.Checked
        MD.Option2 = Me.RadioButton1.Checked
        FileOpen(1, txtFileName.Text, OpenMode.Random, OpenAccess.Write, OpenShare.Shared, Len(MD))
        FilePut(1, MD)
        FileClose(1)
    End Sub
 
    
    Private Sub cmdOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpen.Click
        Dim MD As New MyData
        FileOpen(1, txtFileName.Text, OpenMode.Binary, OpenAccess.Read, OpenShare.Shared, Len(MD))
        FileGet(1, MD)
        FileClose(1)
        TextBox2.Text = MD.Text1
        TextBox3.Text = MD.Text2
        ListBox1.Text = MD.Text3
        CheckBox1.Checked = MD.Option1
        RadioButton1.Checked = MD.Option2
    End Sub
End Class

Answer : Save and display listbox / comboBox items in Data Structure

HI

You should define a variable that holds the selected index and populate it in the SelectedIndexChanged event, then, you can refer to that index in your code.

See samples at http://www.startvbdotnet.com/controls/listbox.aspx
Random Solutions  
 
programming4us programming4us