Hi i am trying to do this project for class and i have been stuck on it for a couple of days

Here is the assignment

**Array Assignment
Write a program that allows the user to enter up to 20 Pro team names and then displays them. The user doesn’t have to enter 20 but they may not enter more than 20.

The team names must be stored in an array.

A For…..Next loop must be used to display the team names in a listbox.

If the user attempts to enter more than 20 team names an error should be displayed “No space to record additional team name”

The .clear and .focus features should be used to clear and reset the textbox after the record button is clicked.

The array and the counter used as the index for the array should be declared globally.**

cacf28fb176000e428eef7935ee6a73a.gif

Here is my code

Public Class Form1
    Dim named(19) As String
    Dim cnt As Integer = 0





    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        named(cnt) = TextBox1.Text
        cnt = cnt + 1
        TextBox1.Text = Nothing

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim strcnt As Integer = 0
        Label2.Text = cnt
        If cnt <= 20 Then
            For strcnt = 0 To named.Length - 1
                ListBox1.Items.Add(named(strcnt))
            Next
        Else
            ListBox1.Items.Add("No space to record additional team name")
        End If

    End Sub

So in button 1 (the Record team name button) i set the array named(cnt) = textbox1.text.. CNT is the counter. Then add 1 to cnt for the next time the button is clicked. It will set named(1) = to the value in textbox1. Then clear the textbox1.text

In button 2 (display button) i create a new variable called STRCNT, which is a other counter for the for next loop! Ok i then set label2 = to cnt.. (This will display the number of teams). I then use a for next loop to check if cnt is less then 20 or greater then 20. I then use a for next loop to display all values in named in the listbox (IF it is less then 20!!) If it is greater i display the text in the listbox "No space to record additional team name"...

ALSO i havent done the reset button yet, plan to do that at the end.

So when i run the program it works fine when i enter names, but when i click the display button it highlights the code "ListBox1.Items.Add(named(strcnt))"

It says "Value cannot be null.Parameter name: item"

Can anyone please help!!!

On line 25 you are assuming that named is completely filled. named.Length will be the full length of the string array not the length of the array that is partly filled up.The rest will be null. You can't add null to a ListBox.

commented: Correct +5

When cnt=20, clicking Button1 you can face an exception, because the upper bound of the array is 19. So you can disable the button1 or use a checking a condition for cnt=20 to prevent extra input.

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        if cnt<=19 Then
            named(cnt) = TextBox1.Text
            cnt = cnt + 1
        End If
        TextBox1.Text = Nothing
    End Sub

And for display ckeck a condition if it null or not.

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Label2.Text = cnt

            For strcnt As Integer = 0 To named.Length - 1
                if Not String.IsNullOr WhiteSpace(named(strcnt)) Then
                    ListBox1.Items.Add(named(strcnt))
                End If
            Next

End Sub

Hope it can help you.

Shark_1 thanks for the replys. bUT line 6 is underlined when i copy it?

Sorry! Unfortunately, I wrote an extra space in between String.IsNullOr WhiteSpace(named(strcnt)).
Make a correction there, please.
It should be

if Not String.IsNullOrWhiteSpace(named(strcnt)) Then

Thnx.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.