如何在输入更改时使用文本框动态更新标签 [英] How to Dynamically Update Label with TextBox As Input Changes
问题描述
我有一个在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
中的所有内容.在这种情况下,我要将OutputLabel
的Text
分配给InputTextBox
的
Text
输出:
资源:
- 如果您需要仔细查看,我已经将这个简单的演示上传到 GitHub . /li>
- 看看 TextChanged文档
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
totxtWordInput.Text
, but in thetxtSpell 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 handlerHandles 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 ourInputTextBox_TextChanged Sub
will execute. In this case, I am assigning theText
ofOutputLabel
to theText
of theInputTextBox
Output:
Resources:
- I've uploaded this simple demo to GitHub if you'd like a closer look.
- Take a look at the TextChanged documentation
这篇关于如何在输入更改时使用文本框动态更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!