Solved

Save and display listbox / comboBox items in Data Structure

Posted on 2008-06-15
3
1,219 Views
Last Modified: 2013-12-20
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?
<VBFixedString(30)> 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
Public Structure MyData

        <VBFixedString(30)> Public Text1 As String

        <VBFixedString(30)> Public Text2 As String

        <VBFixedString(30)> 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

Open in new window

0
Comment
Question by:MonteCristo33
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
elimesika earned 125 total points
ID: 21789607
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
0
 

Author Comment

by:MonteCristo33
ID: 21795423
Dear Elimesika,

Thanks for your help. It made it all cristal clear, and above all, made it work.

MC
0
 

Author Closing Comment

by:MonteCristo33
ID: 31467385
Great stuff! clear and practical answer. Many thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now