Ed Mata Ed Mata - 5 months ago 18
Vb.net Question

I want to display a list of names(3) from a textbox(user input) into a label

This is the code I have so far but is not working properly. The textbox for the names user should input and the button show to display the names in a label in the order entered.

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim intcount, w1, w2, w3 As Integer
Dim intMax As Integer = 2

For intcount = 0 To intMax
strSurnames(intcount) = TextBox1.Text

Next
TextBox1.Clear()

End Sub

Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
Dim intMax As Integer = 2
For intcount = 0 To intMax
lblShow.Text &= strSurnames(intcount)
Next
End Sub

Answer

I am guessing you are going to use only 1 textbox and click the "add button" multiple times to store the name. If this is true, you will first need to create

Dim arrayStr As New List(Of String)

Every single time you click on the add button, it will add into this array.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        arrayStr.Add(TextBox1.Text.Trim())
        TextBox1.Clear()
End Sub

And to show the full name in just one label when you click on the "Show button", can do it like this

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Label1.Text = "" //The label text is cleared.
        For i As Integer = 0 To arrayStr.Count() - 1
            Label1.Text += arrayStr(i) + ", "
        Next
    End Sub

UPDATE - BASED ON YOUR COMMENT QUESTION This is the updated solution based on your question. I am only going to show you with the "Sur Names" only. You can implement the "Weight" the same way.

First, create your array and declare a count as integer for the size of your array.

Dim surNameStr(20) As String
Dim count As Integer = 0

In the "Add Button", you increase the count number by 1 every time you add a new sur name. Once it reached your "maximum" number, you disable your button by BtnAdd.Enabled = False.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If count < 20 Then
            surNameStr(count) = TextBox1.Text.Trim()
            count = count + 1
        Else
            Button1.Enabled = False
            Button2.Enabled = True
        End If
End Sub

Then, in "Show Button", this is how you can show all the surnames stored.

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Label1.Text = ""
        For i As Integer = 0 To 19
            Label1.Text += surNameStr(i) + vbNewLine
        Next
End Sub
Comments