Ed Prince
Ed Prince

Reputation: 714

Adding a variable to a listBox in VB.net Windows Form

I am making a dvd database system in windows form and trying to display the dvd's entered by a user. Then display the Title, Director and Genre in 3 separate listBoxes.

When the user enters the information through 3 separate text boxes, the information is stored in a structure I made called TDvd. This means I can call for example dvd.Title or dvd.Director. I also use the variable index to add this information to an array I made called Dvd(100) (just a random number I used to test).

Here is the code I currently have for adding the items to the ListBox:

For i = 1 To noOfAddedDvds
    lstTitle.Items.Add(dvd(i).Title)
    lstDirector.Items.Add(dvd(i).Director)
    lstGenre.Items.Add(dvd(i).Genre)
Next

The variable NoOfDvdsAdded is just a way of keeping track of the number of dvd's the user has already entered.

I run this and enter the Title, Director and Genre, but when I try and display this information across the 3 listboxes, I get the error:

An unhandled exception of type 'System.ArgumentNullException' occurred in System.Windows.Forms.dll

Public Class Form1
Structure TDvd
    Dim Title As String
    Dim Director As String
    Dim Genre As String
End Structure
Dim dvd(100) As TDvd
Dim index As Integer = 0
Dim noOfAddedDvds As Integer

Private Sub btnAddToDatabase_Click(sender As Object, e As EventArgs) Handles btnAddToDatabase.Click
    If txtDirector.Text <> "" Or txtGenre.Text <> "" Or txtTitle.Text <> "" Then
        txtTitle.Text = dvd(index).Title
        txtDirector.Text = dvd(index).Director
        txtGenre.Text = dvd(index).Genre
        index += 1
        noOfAddedDvds += 1
    End If
    End Sub

Private Sub btnDisplayDatabase_Click(sender As Object, e As EventArgs) Handles btnDisplayDatabase.Click
    Dim i As Integer
    For i = 0 To noOfAddedDvds
        MessageBox.Show(index & ", " & i)
        lstTitle.Items.Add(dvd(i).Title)
        lstDirector.Items.Add(dvd(i).Director)
        lstGenre.Items.Add(dvd(i).Genre)
        MessageBox.Show(index & ", " & i)
    Next

End Sub

End Class

Upvotes: 0

Views: 1934

Answers (3)

Your thing cries out for being rewritten in OO form:

Friend DVDGenres
   Undefined
   Comedy
   Action
   Adventure
   Sci-Fi
End Enum 

Friend Class DVD
    Public Property Title As String
    Public Property Director As String
    Public Property Genre As DVDGenres

    Public Sub New
        Title = ""
        Director = ""
        Genre = DVDGenres.Undefined
        ' other stuff too
    End Sub

    Public Overrides Function ToString As String
       Return Title
    End Sub


End Class

Now something to store them in. Arrays went out with Rubik's Cubes, so a List:

Private myDVDs As New List(of DVD)

A list and a class can do what arrays and structures can without the headaches. Add a DVD:

Dim d As New DVD
d.Name = TextBoxName.Text
d.Director = TextBoxDir.Text
d.Genre = comboboxGenre.SelectedItem

' add to the container:
myDVDs.Add(d)

Display all the DVDs in a ListBox to pick from:

AllDVDsLB.DataSource = myDVDs
AllDVDsLB.DisplayMember = "Title"

This will set your list as the datasource for the listbox. Whatever is in the List is automatically displayed without copying data into the Items collection. Then, say from selectedindex changed event, display the selected item details to some labels:

Label1.Text = Ctype(AllDVDsLB.SelectedItem, DVD).Title
Label2.Text = Ctype(AllDVDsLB.SelectedItem, DVD).Director
Label3.Text = Ctype(AllDVDsLB.SelectedItem, DVD).Genre.ToString

Iterate to do something like what is in the Question:

For Each d As DVD in myDVDs          ' CANT run out of data
    lstTitle.Items.Add(d.Title)
    lstDirector.Items.Add(d.Director)
    lstGenre.Items.Add(d.Genre.ToString) 
Next

Or iterate and reference with an Int32:

For n As Integer = 0 To myDVDs.Count - 1
    lstTitle.Items.Add(myDVDs(n).Title)
    ' etc
Next n

HTH

Upvotes: 0

David
David

Reputation: 219057

According to the documentation, an ArgumentNullException is thrown by the Add() method if the argument passed to it is null. (Or Nothing in VB.) So one of these is Nothing at runtime:

dvd(i).Title
dvd(i).Director
dvd(i).Genre

You'll have to debug to determine which. It would seem that the error is because you're starting your iteration at 1 instead of 0, I would think it should be:

For i = 0 To noOfAddedDvds - 1

So when you get to the index of noOfAddedDvds in your collection, that element will be an uninitialized struct with Nothing strings.

You'll definitely want to fix the iteration (indexes start at 0). Additionally, you may also benefit from initializing the String properties in your struct to String.Empty internally. Depends on whether you want similar errors to manifest as an exception or as an empty record. Sometimes the latter makes the problem more obvious since at runtime you'd see that your output started on the second record.

Upvotes: 1

Martin Milan
Martin Milan

Reputation: 6390

Just a few pointers...

The Items collection on the ListBox is actually 0 indexed, by which I mean that instead of going "1,2,3", it actually goes (0,1,2).

That's what your problem is.

Hint - think about perhaps using a List instead of an array as well... (for dvd)

Upvotes: 0

Related Questions