Results 1 to 6 of 6

Thread: Loading indivudal listbox items into an array (Please Help :()

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    3

    Loading indivudal listbox items into an array (Please Help :()

    I have a huge assignment tommorow in which i have to make a trivia game on visual basic. In this game you have to be able to make your own questions (modify/delete etc). Basically I have one form where you can add questions into one listbox and answers into another listbox. Now i have another form with the actual game board. I want to load each individual listbox item from the other form into the array on the game board form. Example. the first item of form2.listbox1 into question(1)

    I got this so far

    Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim i As Integer

    For i = 0 To Form2.ListBox1.Items.Count - 1
    questions(i) = Form2.ListBox1.Items(i).ToString
    Next

    End Sub

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Loading indivudal listbox items into an array (Please Help :()

    You could send the array in the parent form to the child form

    Parent form
    Code:
    Private Questions(10) As String
    . . .
     Private Sub Button1_Click( _
         ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles Button1.Click
    
    . . .
    
    Dim f As New frmChildForm(Questions)
    
    Try
        f.ShowDialog()
    Finally
        f.Dispose()
    End Try
    Child form
    Code:
    Public Class frmChildForm
        Private Questions() As String
        Public Sub New(ByVal sender() As String)
            InitializeComponent()
            Questions = sender
        End Sub
        Private Sub Form2_Load( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
            ListBox1.DataSource = Questions
        End Sub
    End Class
    Or MainForm
    Code:
    Public Class frmMainForm
        Private mQuestions(10) As String
        Public ReadOnly Property Questions() As String()
            Get
                Return mQuestions
            End Get
        End Property
    
    . . .
        Dim f As New frmChildForm
    
        Try
            f.ShowDialog()
        Finally
            f.Dispose()
        End Try
    Child form
    Code:
    Public Class frmChildForm
        Private Sub Form2_Load( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
            ListBox1.DataSource = frmMainForm.Questions
        End Sub
    End Class
    To add a single item
    Code:
    ListBox1.Items.Add(frmMainForm.Questions(0))

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    3

    Re: Loading indivudal listbox items into an array (Please Help :()

    Thatnk you Kevin,

    Is there a more simpler way? I am a complete beginner in VB and do not understand how implement the
    code. I keep getting errors. Also I want to put each individual element from the listboxes into an array.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Loading indivudal listbox items into an array (Please Help :()

    You implement the code exactly as Kevin has shown. If you get errors, show us what you've done and tell us what the errors are. We can then help you fix them.

    To put the ListBox items into an array, you could do this:
    vb.net Code:
    1. Dim items(myListBox.Items.Count - 1) As String
    2.  
    3. myListBox.Items.CopyTo(items, 0)
    or, assuming .NET 3.5 or higher, this:
    vb.net Code:
    1. Dim items = myListBox.Items.Cast(Of String)().ToArray()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    3

    Re: Loading indivudal listbox items into an array (Please Help :()

    well for the parent form (Im guessing that means where the listboxes are) i put
    Dim f As New Form(questions)

    Try
    f.ShowDialog()
    Finally
    f.Dispose()
    End Try


    under a button click and i got "to many arguments for Public Sub New()"
    and when i put the other code in the child form I got
    "Reference to a non-shared member requires an object reference" due to my other buttons on other forms which have the code to show the parent form (eg. form2.show())

    thank you

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Loading indivudal listbox items into an array (Please Help :()

    Quote Originally Posted by Theman123 View Post
    i got "to many arguments for Public Sub New()"
    Then you haven't declared the constructor in the child form as Kevin demonstrated. Look at the second code snippet in post #2. There's a constructor, i.e. Sub New, declaration in there with a parameter.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width