Question Textbox.text to Label

anybloodyid

Member
Joined
Jan 25, 2018
Messages
16
Programming Experience
Beginner
Hi and sorry if this is posted in wrong place only just joined.
I want to send input from a textbox to 4 labels, so enter text hit return input goes to label1, enter more text input goes to label2 etc
The code below is how I worked it out but I feel sure there must be a better way, is there?
VB.NET:
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
    Public Class GlobalVariables
        Public Shared num As Integer = 1
    End Class

    Private Sub Txt_input_KeyDown(sender As Object, e As KeyEventArgs) Handles Txt_input.KeyDown

        If e.KeyCode = Keys.Enter Then
            Select Case GlobalVariables.num
                Case 1
                    Lbl_player1.Text = Txt_input.Text
                    Txt_input.Text = ""
                    GlobalVariables.num = 2
                Case 2
                    Lbl_player2.Text = Txt_input.Text
                    Txt_input.Text = ""
                    GlobalVariables.num = 3
                Case 3
                    Lbl_player3.Text = Txt_input.Text
                    Txt_input.Text = ""
                    GlobalVariables.num = 4
                Case 4
                    Lbl_player4.Text = Txt_input.Text
                    Txt_input.Text = ""
                    GlobalVariables.num = 0
            End Select

        End If

    End Sub
End Class
 
You could use an array:
If e.KeyCode = Keys.Enter Then
    Static counter = 0
    Dim labels = {Lbl_player1, Lbl_player2, Lbl_player3, Lbl_player4}
    labels(counter).Text = Txt_input.Text
    Txt_input.Text = String.Empty
    counter = If(counter = labels.Count - 1, 0, counter + 1)
End If
 
Hi John
Thanks for the reply a couple of further questions please
1)Does using static keep the variable local which is better than using global?

2) Could you explain this line
If(counter = labels.Count - 1, 0, counter + 1)
I understand what it does adds 1 to counter but what are the - 1, 0 doing?
 
It is certainly simpler to use for the example. Using a global shared field should be avoided if possible.

If Operator (Visual Basic) | Microsoft Docs
When If is called by using three arguments, the first argument must evaluate to a value that can be cast as a Boolean. That Boolean value will determine which of the other two arguments is evaluated and returned.
 
I have a follow up question with the code given to me last time.
Each Label gets a name from the textbox but how do I then get the textbox to start inputting scores in to the next 4 labels Lbl_roundscore1
Do I need a another if statement inside the one I have eg if number = 4 start placing text from textbox to Lbl_roundscore and forget Lbl_player
 
You could just add them to the array, in the input order.
 
You could just add them to the array, in the input order.
Hi John,
Thanks again for taking the time to look and answer.

Would that idea not just keep filling in each of the player name labels and then the score labels round and round?

What I was after was to fill in player name labels and then move to score labels ie
player label1
player label2
player label3
player label4
Stop filling in player name labels and go round player score labels 1,2,3,4,1,2,3,4,1,2, etc
 
No, you would be adding input in the order of the array. Stop input when you are done.
 
I understand what you are saying but after entering player names I move on to enter player scores for each round, there could be 10 or more rounds to each game.
After each round another label shows total score.

Player1.name Player1.roundscore Player1.totalscore
Player2.name Player2.roundscore Player2.totalscore
Player3.name Player3.roundscore Player3.totalscore
Player4.name Player4.roundscore Player4.totalscore
 
Sounds like a DataGridView would be more suitable for this.
 
Hi and sorry if this is posted in wrong place only just joined.
I want to send input from a textbox to 4 labels, so enter text hit return input goes to label1, enter more text input goes to label2 etc
The code below is how I worked it out but I feel sure there must be a better way, is there?
VB.NET:
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
    Public Class GlobalVariables
        Public Shared num As Integer = 1
    End Class

    Private Sub Txt_input_KeyDown(sender As Object, e As KeyEventArgs) Handles Txt_input.KeyDown

        If e.KeyCode = Keys.Enter Then
            Select Case GlobalVariables.num
                Case 1
                    Lbl_player1.Text = Txt_input.Text
                    Txt_input.Text = ""
                    GlobalVariables.num = 2
                Case 2
                    Lbl_player2.Text = Txt_input.Text
                    Txt_input.Text = ""
                    GlobalVariables.num = 3
                Case 3
                    Lbl_player3.Text = Txt_input.Text
                    Txt_input.Text = ""
                    GlobalVariables.num = 4
                Case 4
                    Lbl_player4.Text = Txt_input.Text
                    Txt_input.Text = ""
                    GlobalVariables.num = 0
            End Select

        End If

    End Sub
End Class
Just a remark WRT global variables:
VB.NET:
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
    Public Class GlobalVariables
        Public Shared num As Integer = 1
    End Class

    Private Sub Txt_input_KeyDown(.... etc.
Any variables declared after the ' Public Class ... ' and before the first subroutine will automatically make them global.
VB.NET:
Public Class Form1

    Dim num As Int32 = 1

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

    End Sub

    Private Sub Txt_input_KeyDown(.... etc.
I've used 'Dim' but 'Private' or 'Public' may also be used dependent upon their use.

Also: When you have a subroutine automatically generated, (as in your 'Private Sub Form1_Load' for example), it will be in the form of :
VB.NET:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If you're not using the automatically supplied variables removing them will not only make the app. run infinitesimally faster, (the processor doesn't have to initiate them or save bytes for them), it makes the code a lot easier to read.
VB.NET:
Private Sub Form1_Load() Handles MyBase.Load



Poppa.
 
Back
Top