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
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))
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.
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:
Dim items(myListBox.Items.Count - 1) As String
myListBox.Items.CopyTo(items, 0)
or, assuming .NET 3.5 or higher, this:
vb.net Code:
Dim items = myListBox.Items.Cast(Of String)().ToArray()
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
Re: Loading indivudal listbox items into an array (Please Help :()
Quote:
Originally Posted by
Theman123
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.