vb.net 如何在输入更改时使用 TextBox 动态更新标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25436548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:05:32  来源:igfitidea点击:

How to Dynamically Update Label with TextBox As Input Changes

vb.netwinformsloops

提问by DesignerMind

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.

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

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.

我承认我不知道我在做什么,因为我以前从未尝试过,所以我不知道从设置我需要做什么开始。我知道我需要一些变量来保存我的字符串输入,并且可能需要某种类型的循环,但除此之外,我迷路了。唯一的另一个例子是在 C# 中,对我没有任何帮助。

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 TextChangedevent handler:

谁能给我一个简单的模型来解决,这样我就可以把这个方法记入记忆中?现在,我所拥有的只是来自我的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

回答by silencedmessage

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 txtWordInputinside your txtSpelltext changed event handler. If you are entering the text in the txtWordInputinput, you'll want to handle this in the txtWordInput textChangedevent handler:

在您提供的代码中,您引用了txtWordInputtxtSpell文本更改事件处理程序中命名的对象。如果您在输入中输入文本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


Follow-up:

跟进

  • The TextChangedevent is the correct event for this.
  • In your code, you are assigning lblShowInput.Textto txtWordInput.Text, but in the txtSpell TextChangedevent handler.
  • You want to be in the TextChanged event handler for whatever TextBoxyou would like to use to update the label, as the text is changing.
  • 框TextChanged事件是这个正确的事件。
  • 在您的代码中,您正在分配lblShowInput.TexttxtWordInput.Text但在txtSpell TextChanged事件处理程序中。
  • 您希望在 TextChanged 事件处理程序中处理TextBox您想用来更新标签的任何内容,因为文本正在更改

To give a better example, I have created a simple Winforms VBapplication that has only a textbox named InputTextBoxand a labelnamed Output Label.

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

The Form:

表格:

Simple VB Form with textbox and label

带有文本框和标签的简单 VB 表单

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_TextChangedis the method name generated by Visual Studio for our event handler
  • Handles InputTextBox.TextChangedties the method to an actual event it is handling.
  • When the InputTextBoxtext property is changed (typically by user input), whatever we have in our InputTextBox_TextChanged Subwill execute. In this case, I am assigning the Textof OutputLabelto the Textof the InputTextBox
  • InputTextBox_TextChanged是 Visual Studio 为我们的事件处理程序生成的方法名称
  • Handles InputTextBox.TextChanged将方法与它正在处理的实际事件联系起来。
  • InputTextBoxtext 属性改变时(通常是通过用户输入),我们所拥有的任何东西都InputTextBox_TextChanged Sub将执行。在这种情况下,我分配TextOutputLabelTextInputTextBox

Output:

输出:

enter image description here

在此处输入图片说明

Resources:

资源: