如何在输入更改时使用文本框动态更新标签 [英] How to Dynamically Update Label with TextBox As Input Changes

查看:84
本文介绍了如何在输入更改时使用文本框动态更新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在VB.Net中构建的拼写应用程序,其中有一个文本框接收简单的输入(拼写单词),以及一个将显示输出的标签.我要做的是,当我在文本框中输入内容时,可以在标签中看到它-在我输入文本框中的内容.

我将承认我不知道自己在做什么,因为我以前从未尝试过这样做,所以我不知道从设置我需要做的事情开始.我知道我需要一些变量来保存String输入,并且可能需要某种类型的循环,但是除此之外,我迷路了.唯一的其他示例是C#,对我没有任何帮助.

任何人都可以给我一个简单的模型来工作,这样我就可以将这种方法存储到内存中了吗?现在,我所拥有的只是我的TextChanged事件处理程序中的代码存根:

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

解决方案

我知道我需要一些变量来保存我的String输入,并且会 可能需要某种类型的循环

我认为您不需要循环或变量来保存值.您几乎拥有它:

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

在您提供的代码中,您引用了txtSpell文本更改事件处理程序中名为txtWordInput的对象.如果要在txtWordInput输入中输入文本,则需要在txtWordInput textChanged事件处理程序中进行处理:

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


跟进:

  • TextChanged 事件是正确的事件.
  • 在代码中,您将lblShowInput.Text分配给txtWordInput.Text,但是在txtSpell TextChanged事件处理程序中.
  • 由于文本正在发生更改,因此您想在TextChanged事件处理程序中使用任何用于更新标签的TextBox.

举个更好的例子,我创建了一个简单的Winforms VB应用程序,它只有一个名为 InputTextBox 的文本框和一个名为 Output Label label. >

表格:

代码:

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

说明:

  • InputTextBox_TextChanged是Visual Studio为我们的事件处理程序生成的方法名称
  • Handles InputTextBox.TextChanged将方法与它正在处理的实际事件联系起来.
  • 更改InputTextBox文本属性(通常通过用户输入)时,将执行InputTextBox_TextChanged Sub中的所有内容.在这种情况下,我要将OutputLabelText分配给InputTextBox
  • Text

输出:

资源:

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

解决方案

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:

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:

Resources:

这篇关于如何在输入更改时使用文本框动态更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆