Can't figure out how to assign textbox data to an array

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1103 Views - Last Post: 14 February 2013 - 03:54 PM Rate Topic: -----

#1 donna4909  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 13-February 13

Can't figure out how to assign textbox data to an array

Posted 13 February 2013 - 06:17 PM

I have just started my second semester of VB, and arrays are driving me crazy. I am working on a soccer application that asks the user for the number of players and their names, then it creates a little scoreboard of textboxes so the user can record each point scored. It will then total the scores.

My main problem is that I don't know how to assign the textboxes to the array or what to do if the boxes are left blank. My code is below:

'Transfer textbox scores to individual player arrays.

        If Integer.TryParse(txtPlayer1Score1.Text, intPlayer1Scores(0)) And intPlayer1Scores(0) >= 0 Then

            'Then what? I don't know what to do here. Is the value sent to the array through the code above, or am I missing something else?

        End If


Is This A Good Question/Topic? 0
  • +

Replies To: Can't figure out how to assign textbox data to an array

#2 tycos  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 12-February 13

Re: Can't figure out how to assign textbox data to an array

Posted 13 February 2013 - 06:29 PM

So you ask the user to enter the number of players,
for that number dynamically create text boxes for name and score?

You want to create an array of these controls so you can loop through then once they fill them out?
Was This Post Helpful? 0
  • +
  • -

#3 tycos  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 12-February 13

Re: Can't figure out how to assign textbox data to an array

Posted 13 February 2013 - 06:48 PM

Here is a simple example that might help you:

User enters a number in a textbox and presses a button. If what they enter is between 0 and 20 then create that number of textboxes and store each one in an array.

On clicking another button you can see that I am looping the array of textboxes and putting the values of each to a message box.

This line is just to ensure they appear below each other:
textbox.Location = New Point(10, 240 + (i * 25))



   Dim player_names As New ArrayList

  Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        If IsNumeric(txt_player_number.Text) AndAlso CInt(txt_player_number.Text) > 0 AndAlso CInt(txt_player_number.Text) < 21 Then
            For i = 0 To CInt(txt_player_number.Text)
                Dim textbox As New TextBox
                textbox.Name = "player_" + i.ToString
                textbox.Text = "player_" + i.ToString
                textbox.Location = New Point(10, 240 + (i * 25))
                player_names.Add(textbox)
                Me.Controls.Add(textbox)
            Next
        Else
            MsgBox("Please Enter a number between 0 and 20")
        End If

    End Sub

    Private Sub cmb_loop_array_Click(sender As System.Object, e As System.EventArgs) Handles cmb_loop_array.Click
        For Each TextBox In player_names
            MsgBox("Name: " & TextBox.name & " value: " & TextBox.text)
        Next
    End Sub


Was This Post Helpful? 0
  • +
  • -

#4 tycos  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 12-February 13

Re: Can't figure out how to assign textbox data to an array

Posted 13 February 2013 - 06:57 PM

Array List
Maybe you need to play about with arrays, and read up on them. I used an array list in the example

        ' declare a new Arrarylist
        Dim player_names As New ArrayList

        ' Create a new object
        Dim tbox As New TextBox

        'set properties on the textbox
        tbox.Name = "Player_1"

        ' add the object to the array list
        player_names.Add(tbox)

        'get number of items in arraylist
        Dim item_count As Integer
        item_count = player_names.Count

        ' Loop through arraylist and for each Textbox do something
        For Each TextBox In player_names
            MsgBox("Name: " & TextBox.name & " value: " & TextBox.text)
        Next


Was This Post Helpful? 0
  • +
  • -

#5 donna4909  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 13-February 13

Re: Can't figure out how to assign textbox data to an array

Posted 13 February 2013 - 07:18 PM

I have watched every youtube & Microsoft video that I could, and searched the internet for days, and these arrays still do not make sense to me.

Basically, I'm using an input box to ask the user to enter the number of players. That number that they enter becomes the upper subscript for the names array. A sub gets the list of names, then the names array is transferred to a set of labels. Beside the name labels, there are 5 score textboxes. That is what I don't know how to transfer to the arrays.

 Dim intCount As Integer = 0

        'Ask how many players.
        intNumOfPlayers = CInt(InputBox("How many players do you have?"))

        'Resize the array based on user input.
        ReDim strNamesArray(0 To intNumOfPlayers - 1)

        'Get player's names.
        GetNames()

        'Store names from array into labels.
        lblPlayer1.Text = strNamesArray(0)
        lblPlayer2.Text = strNamesArray(1)
        lblPlayer3.Text = strNamesArray(2)
        lblPlayer4.Text = strNamesArray(3)
        lblPlayer5.Text = strNamesArray(4)
        lblPlayer6.Text = strNamesArray(5)
        lblPlayer7.Text = strNamesArray(6)
        lblPlayer8.Text = strNamesArray(7)
        lblPlayer9.Text = strNamesArray(8)
        lblPlayer10.Text = strNamesArray(9)

        'Transfer textbox scores to individual player arrays.

'Again this is the part causing the trouble.

        ReDim intPlayer1Scores(intCount)

        For intCount = 0 To intPlayer1Scores.Length - 1
            intPlayer1Scores(0) = CInt(txtPlayer1Score1.Text)
            intPlayer1Scores(1) = CInt(txtPlayer1Score2.Text)
            intPlayer1Scores(2) = CInt(txtPlayer1Score3.Text)
            intPlayer1Scores(3) = CInt(txtPlayer1Score4.Text)
            intPlayer1Scores(4) = CInt(txtPlayer1Score5.Text)
        Next

    End Sub


I've tried many many different ways to get these textboxes to turn into array elements, but honestly, I have no clue how to do it.
Was This Post Helpful? 0
  • +
  • -

#6 tycos  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 12-February 13

Re: Can't figure out how to assign textbox data to an array

Posted 13 February 2013 - 07:35 PM

Your going about it wrong, you are creating 10 text boxes before you know how many players there are, consider the solution in a flow like this:

Ask user how many players?
User enters 15
Create 15 TextBoxes Dynamically
User Fills in each TextBox
User Presses Button when finished
Take Each textbox value and store in an array

------------------------------------------------

Your For loop does the same thing every time.

please show me your get_names sub
Was This Post Helpful? 0
  • +
  • -

#7 donna4909  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 13-February 13

Re: Can't figure out how to assign textbox data to an array

Posted 13 February 2013 - 07:44 PM

Yes, that does make sense but I don't know how to do any of that dynamically. That's why I have a form with 50 textboxes... (Yes, it's crazy, I know).

This class is the second part of a vb beginners class. We're using a book called Starting Out with Visual Basic 2010, and it seems like it covers a ton of information, but everything is just bits and pieces. And there's no direction about how to use everything together. Last semester the teacher provided sample programs for each chapter, and that really helped, but this new teacher just gives us links to online videos that do the same thing the book does (info in bits and pieces).

Forgot to add the GetNames() sub

Function GetNames()

        'Ask for players names and send to array.

        For intCount = 0 To intNumOfPlayers - 1
            strNamesArray(intCount) = InputBox("Enter player names.")
        Next
        Return strNamesArray

    End Function

Was This Post Helpful? 0
  • +
  • -

#8 tycos  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 12-February 13

Re: Can't figure out how to assign textbox data to an array

Posted 13 February 2013 - 07:50 PM

OK I think I see where you are going with this.

Ask Number players.
2
For how ever many players prompt for name
Ryan
Bob

Put each name into array

(0,0) = 0
(0,1) = Ryan
(1,0) = 1
(1,1) = Bob

     
Dim player_number = CInt(InputBox("How many players do you have?"))
Dim player_name As String = ""
Dim Array(player_number - 1, 1) As String

Dim i As Integer = 0
For i = 0 To player_number - 1
  player_name = InputBox("Player " & i & " : Name : ")
  Array(i, 0) = i
  Array(i, 1) = player_name
  Next



 
Dim player_number = CInt(InputBox("How many players do you have?"))
Dim player_name As String = ""
Dim player_score As Integer
Dim Array(player_number - 1, 2) As String

Dim i As Integer = 0

For i = 0 To player_number - 1
  player_name = InputBox("Player " & i & " : Name : ")
  player_score = CInt(InputBox(player_name & " score: "))
  Array(i, 0) = i
  Array(i, 1) = player_name
  Array(i, 2) = player_score
Next



Result when Entering 2 Players:

(0,0) = 0
(0,1) = Ryan
(0,2) = 100
(1,0) = 1
(1,1) = Bob
(1,2) = 25
Was This Post Helpful? 0
  • +
  • -

#9 donna4909  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 13-February 13

Re: Can't figure out how to assign textbox data to an array

Posted 13 February 2013 - 07:55 PM

Yes, that is what I'm doing, but your variable confuse me. The name part of my code works (or at least I think it does, *lol*); it diplays in the labels like I want it to.

But in the groupbox with each those name labels, there are 5 textboxes to enter scores into. How do I get those scores to each player's individual array? They're declared globally in a module as:

Public intPlayer1Scores()
    Public intPlayer2Scores()
    Public intPlayer3Scores()


I apologize for my numerous typos. I've been working on this for several days with little sleep, and it's starting to show.
Was This Post Helpful? 0
  • +
  • -

#10 tycos  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 12-February 13

Re: Can't figure out how to assign textbox data to an array

Posted 13 February 2013 - 08:08 PM

** Please attach your project so I can understand fully what you are trying to do **

So when you load the form you have no idea how many players there are, you might set a max and min.
At this point there are no text boxes on the screen ok.

So ask for a number, they say 2

FIRST PASS I = 0
For I = 0 to 5 do
Create Textbox
Call it "player_0"
Add textbox to array
Add it to the form
next

SECOND PASS I = 1
For I = 0 to 5 do
Create Textbox
Call it "player_1"
Add textbox to array
Add it to the form
next

So I ended up with an array of two textboxes one called "player_0" and one called "player_1"

Is this work for a project? I do not want to write it for you as you need to learn this and understand it.

I am not showing you the best ways to do this but a way that will work. I would have a player class defined and have a list of players

This post has been edited by tycos: 13 February 2013 - 08:16 PM

Was This Post Helpful? 0
  • +
  • -

#11 donna4909  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 13-February 13

Re: Can't figure out how to assign textbox data to an array

Posted 13 February 2013 - 08:17 PM

Yes, this is for a homework problem. I wouldn't want anyone to write it for me, as I will need to know it for tests.

Everything that you wrote makes sense, but what is the code to "Add textbox to array"? All I can come up with is:
player_0.Text = ArrayName(0)

Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: Can't figure out how to assign textbox data to an array

Posted 13 February 2013 - 08:24 PM

Assignment flow is right to left.

{Where to store} = {What to store}


Suggested Tutorial Reading material

This post has been edited by AdamSpeight2008: 13 February 2013 - 08:25 PM

Was This Post Helpful? 0
  • +
  • -

#13 donna4909  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 13-February 13

Re: Can't figure out how to assign textbox data to an array

Posted 13 February 2013 - 08:27 PM

It won't let me attach a zipped file... It just says "The server returned an error during upload".
Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: Can't figure out how to assign textbox data to an array

Posted 13 February 2013 - 08:52 PM

But your keyboard seems to be working though.
Either typing you code in a reply

Begin with the code tags

Then Ctrl-C (Copy) and Ctrl-V (Paste), of your code into the code tags.
Was This Post Helpful? 0
  • +
  • -

#15 donna4909  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 13-February 13

Re: Can't figure out how to assign textbox data to an array

Posted 13 February 2013 - 09:22 PM

Here's all the code for all forms/modules:

Main Form
Public Class Form1

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

        Dim intCount As Integer = 0

        'Ask how many players.
        intNumOfPlayers = CInt(InputBox("How many players do you have?"))

        'Resize the array based on user input.
        ReDim strNamesArray(0 To intNumOfPlayers - 1)

        'Get player's names.
        GetNames()

        'Store names from array into labels.
        lblPlayer1.Text = strNamesArray(0)
        lblPlayer2.Text = strNamesArray(1)
        lblPlayer3.Text = strNamesArray(2)
        lblPlayer4.Text = strNamesArray(3)
        lblPlayer5.Text = strNamesArray(4)
        lblPlayer6.Text = strNamesArray(5)
        lblPlayer7.Text = strNamesArray(6)
        lblPlayer8.Text = strNamesArray(7)
        lblPlayer9.Text = strNamesArray(8)
        lblPlayer10.Text = strNamesArray(9)

        'Transfer textbox scores to individual player arrays.
        ReDim intPlayer1Scores(4)

        intPlayer1Scores(0) = CInt(txtPlayer1Score1.Text)
        intPlayer1Scores(1) = CInt(txtPlayer1Score2.Text)
        intPlayer1Scores(2) = CInt(txtPlayer1Score3.Text)
        intPlayer1Scores(3) = CInt(txtPlayer1Score4.Text)
        intPlayer1Scores(4) = CInt(txtPlayer1Score5.Text)


    End Sub
    Function GetNames()

        'Ask for players names and send to array.

        For intCount = 0 To intNumOfPlayers - 1
            strNamesArray(intCount) = InputBox("Enter player names.")
        Next
        Return strNamesArray

    End Function

    Public Sub btnCalculateTotals_Click(sender As System.Object, e As System.EventArgs) Handles btnCalculateTotals.Click

        DisplayTotals()


    End Sub

    Public Sub DisplayTotals()

        'Declare totals form and display.
        Dim frmTotals As New Totals
        frmTotals.ShowDialog()


    End Sub
End Class


Totals Form
Public Class Totals

    Public Sub Totals_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim intTotalP1, intTotalP2 As Integer

        TotalIndividualScores()

        lstTotals.Items.Add(strNamesArray(0) & intTotalP1.ToString)
        lstTotals.Items.Add(strNamesArray(1) & intTotalP2.ToString)

    End Sub

    Public Function TotalIndividualScores()
        Dim intTotalP1 As Integer = 0
        Dim intTotalP2 As Integer = 0

        'Player one scores.
        intTotalP1 = intPlayer1Scores(0) + intPlayer1Scores(1) + intPlayer1Scores(2) + intPlayer1Scores(3) + intPlayer1Scores(4)
        Return intTotalP1

        'Player two scores.
        intTotalP1 = intPlayer2Scores(0) + intPlayer2Scores(1) + intPlayer2Scores(2) + intPlayer2Scores(3) + intPlayer2Scores(4)
        Return intTotalP2


    End Function
End Class


Global Variables module
Module GlobalVariables

    Public intNumOfPlayers As Integer
    Public strNamesArray()

    Public intPlayer1Scores()
    Public intPlayer2Scores()
    Public intPlayer3Scores()
    Public intPlayer4Scores()
    Public intPlayer5Scores()
    Public intPlayer6Scores()
    Public intPlayer7Scores()
    Public intPlayer8Scores()
    Public intPlayer9Scores()
    Public intPlayer10Scores()

    

End Module


That's everything. I'm sure there are lots of errors, but cut me some slack. I'm not stupid (I have my CCNA, carry a 4.0 GPA at school, and am in the honors club), but I am obviously a very poor programmer.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2