10 Replies - 4345 Views - Last Post: 04 June 2014 - 09:57 AM Rate Topic: -----

#1 humanshop   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 31-May 14

Adding textbox text as a new element to an array

Posted 31 May 2014 - 12:03 PM

Hello All,

I am trying to figure out how to add textbox text as a new element to a string array for each click of a submit button.
Everything I have read is only showing how to manually add elements to an array (ex: array(0) = ..., array(1) = ..., etc). As the code reads right now, I realized that my button click is simply redefining the entire array as what is in the textbox.


After creating this array I want to output each string in the array into another textbox via a different button, although I believe this part of the code is functioning properly.


Public Class Form1
    Dim words(99) As String


    Private Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles SubmitButton.Click
        words(99) = {userInputTextBox.Text}

    End Sub

    Private Sub showInputButton_Click(sender As Object, e As EventArgs) Handles showInputButton.Click
        For Each word As String In words
            outputTextBox.AppendText(word & vbNewLine)
        Next
    End Sub

End Class


Is This A Good Question/Topic? 0
  • +

Replies To: Adding textbox text as a new element to an array

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: Adding textbox text as a new element to an array

Posted 31 May 2014 - 12:14 PM

Using the code you have, you would need to create a variable to hold the next index. Starting at 0, When the user clicks the submit button you would use your index variable for the index of the array and set it = to your textinput then before exiting the sub, increment the index variable by 1 to await the next input.

I would suggest that you check to make sure that there is a value in the textbox prior to adding so as not to insert blank strings into the array.

You also have your array limited to 100 inputs which may be fine but you can resize an array and preserve its contents using the ReDim Preserve statement.
Was This Post Helpful? 0
  • +
  • -

#3 humanshop   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 31-May 14

Re: Adding textbox text as a new element to an array

Posted 04 June 2014 - 07:45 AM

View PostCharlieMay, on 31 May 2014 - 12:14 PM, said:

When the user clicks the submit button you would use your index variable for the index of the array and set it = to your textinput


I'm still unsure how to do this. How do you reference the index of the array? Thanks

 Public Class Form1
    Dim words(99) As String
    Dim countervar As Integer = 0


    Private Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles SubmitButton.Click
        words. = {userInputTextBox.Text}
        countervar = countervar + 1

    End Sub

    Private Sub showInputButton_Click(sender As Object, e As EventArgs) Handles showInputButton.Click
        For Each word As String In words
            outputTextBox.AppendText(word & ", ")
        Next
    End Sub

End Class 

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14097
  • View blog
  • Posts: 56,497
  • Joined: 12-June 08

Re: Adding textbox text as a new element to an array

Posted 04 June 2014 - 07:51 AM

How familiar with arrays are you?

Arrays in Visual Basic
Was This Post Helpful? 0
  • +
  • -

#5 humanshop   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 31-May 14

Re: Adding textbox text as a new element to an array

Posted 04 June 2014 - 08:33 AM

I understand to reference an index of an array you use arrayName(x), x being the place of element to reference. But I'm trying to figure out how to add the textbox text as a new element to the array with each click.

I am I getting close? Not getting desired result. Help is appreciated.

Public Class Form1
    Dim words(99) As String
    Dim countervar As Integer = 0


    Private Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles SubmitButton.Click
        For counterVar As Integer = 0 To words.Length - 1 Step 1
            words(counterVar) = userInputTextBox.Text
            counterVar = counterVar + 1
        Next


    End Sub

    Private Sub showInputButton_Click(sender As Object, e As EventArgs) Handles showInputButton.Click
        For Each word As String In words
            outputTextBox.AppendText(word & ", ")
        Next
    End Sub

End Class 

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14097
  • View blog
  • Posts: 56,497
  • Joined: 12-June 08

Re: Adding textbox text as a new element to an array

Posted 04 June 2014 - 08:35 AM

Quote

I am I getting close? Not getting desired result. Help is appreciated.

Explain this. What is it doing vs what are you expecting?
Was This Post Helpful? 0
  • +
  • -

#7 humanshop   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 31-May 14

Re: Adding textbox text as a new element to an array

Posted 04 June 2014 - 08:51 AM

What I want it to do:
Store the userInputBox text as a new element to the words array with each click of the submit button.
Then when showInputButton is clicked, output every element of that array into outputTextbox

What it is doing:
When the userInputBox text is changed and SubmitButton is clicked, all values of the array are being replaced with the lastest string that was typed into userInputBox. Instead of adding another element to the string array.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14097
  • View blog
  • Posts: 56,497
  • Joined: 12-June 08

Re: Adding textbox text as a new element to an array

Posted 04 June 2014 - 08:59 AM

06	    Private Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles SubmitButton.Click
07	        For counterVar As Integer = 0 To words.Length - 1 Step 1
08	            words(counterVar) = userInputTextBox.Text
09	            counterVar = counterVar + 1
10	        Next
11	 
12	 
13	    End Sub


Gotcha. Look at what you are doing.. read it out loud.

Each time 'submit' is clicked I want to start up a loop.
That loop will run from 0 to the length of the array 'words'.
Each iteration I will assign the textbox userInputTextBox's value to the index in the array from the loop control variable.


Sounds like you are getting exactly what you programmed.

Now if you want userInputTextBox's value to go into the next slot I would advocate *NOT* having a loop that goes from 0 to the length of the array. So remove the for loop. The rest of the code (inside the loop) is what you need. You are tracking your counter as a global variable.. and incrementing it with each button click.

The problem is that there are only so many spaces in the array to fill and you are not checking to make sure you are not going to attempt to insert a value into index 7 when the max length is 6. A basic if statement would help.. or switching to an array list that expands when you ".add" an item.
Was This Post Helpful? 0
  • +
  • -

#9 humanshop   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 31-May 14

Re: Adding textbox text as a new element to an array

Posted 04 June 2014 - 09:14 AM

Thank you! Much appreciated. It seems so simple now, I was just over-thinking it I guess. I just started learning vb and it is my first programming language.
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14097
  • View blog
  • Posts: 56,497
  • Joined: 12-June 08

Re: Adding textbox text as a new element to an array

Posted 04 June 2014 - 09:15 AM

Again.. make sure to check to see if "counterVar" is greater than the length of the array.. or, else, use an expanding collection like an array list.
Was This Post Helpful? 0
  • +
  • -

#11 humanshop   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 31-May 14

Re: Adding textbox text as a new element to an array

Posted 04 June 2014 - 09:57 AM

Yes I set up an If statement. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1