DesignerMind
DesignerMind

Reputation: 410

How to Dynamically Update Label with TextBox As Input Changes

I have a spelling application that I am building in VB.Net, where I have a textbox receiving simple input (spelling words), and a label which will show the output. What I want to accomplish is when I enter something in the textbox, I can see it in my label - as I am typing into the textbox.

I will admit that I don't know what I'm doing, as I've never tried this before, so I don't know to begin in terms of setting up what I need to do. I know that I'll need some variable to hold my String input, and will probably need some type of loop, but beyond that, I am lost. The only other example is in C#, and doesn't help me any.

Can anyone give me a simple model to work off of, so I can put the approach into memory? For now, all I have is code stub from my TextChanged event handler:

Private Sub txtSpell_TextChanged(sender As Object, e As EventArgs) Handles txtSpell.TextChanged
    'Set variables to hold values.
    Dim someText As String

    'Connect the label and textbox.
    lblShowInput.Text = txtWordInput.Text

    'Process loop to populate the label from textbox input.

    for '(This is where I am lost on the approach)

End Sub

Upvotes: 0

Views: 18020

Answers (1)

I know that I'll need some variable to hold my String input, and will probably need some type of loop

I don't think you'll need a loop, or the variable to hold the value. You almost have it:

Private Sub txtSpell_TextChanged(sender As Object, e As EventArgs) Handles txtSpell.TextChanged
    'Connect the label and textbox.
    lblShowInput.Text = txtSpell.Text
End Sub

In the code you provided, you are referencing an object named txtWordInput inside your txtSpell text changed event handler. If you are entering the text in the txtWordInput input, you'll want to handle this in the txtWordInput textChanged event handler:

Private Sub txtWordInput_TextChanged(sender As Object, e As EventArgs) Handles txtWordInput.TextChanged
        'Connect the label and textbox.
        lblShowInput.Text = txtWordInput.Text
End Sub

Follow-up:

  • The TextChanged event is the correct event for this.
  • In your code, you are assigning lblShowInput.Text to txtWordInput.Text, but in the txtSpell TextChanged event handler.
  • You want to be in the TextChanged event handler for whatever TextBox you would like to use to update the label, as the text is changing.

To give a better example, I have created a simple Winforms VB application that has only a textbox named InputTextBox and a label named Output Label.

The Form:

Simple VB Form with textbox and label

The Code:

Public Class Form1

    Private Sub InputTextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles InputTextBox.TextChanged
        OutputLabel.Text = InputTextBox.Text

    End Sub
End Class

Explanation:

  • InputTextBox_TextChanged is the method name generated by Visual Studio for our event handler
  • Handles InputTextBox.TextChanged ties the method to an actual event it is handling.
  • When the InputTextBox text property is changed (typically by user input), whatever we have in our InputTextBox_TextChanged Sub will execute. In this case, I am assigning the Text of OutputLabel to the Text of the InputTextBox

Output:

enter image description here

Resources:

Upvotes: 1

Related Questions