vb.net 如何在 Visual Basic 中自动附加到 RichTextBox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16110214/
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 13:19:48  来源:igfitidea点击:

How to automatically append to RichTextBox in Visual Basic

vb.netvisual-studio-2010

提问by user2300114

I tried Google for an answer but didn't find what I was looking for.

我尝试谷歌寻找答案,但没有找到我要找的东西。

I created a very simple app that lets users use a barcode scanner to scan barcodes into a text file. Everything works fine, I just want to simplify it a bit more.

我创建了一个非常简单的应用程序,允许用户使用条码扫描器将条码扫描到文本文件中。一切正常,我只是想进一步简化它。

Originally, I have a textbox1.text field where the scanned barcode would appear, then the user had to click the 'Add' button (Button1.Click) I placed next to the textbox field to append the barcode serial into a RichTextBox right below. Well the user found it tedious to have to click the 'Add' button every time they scanned an individual barcode.

最初,我有一个 textbox1.text 字段,其中将出现扫描的条形码,然后用户必须单击我放置在文本框字段旁边的“添加”按钮 (Button1.Click) 以将条形码序列附加到下面的 RichTextBox 中。好吧,用户发现每次扫描单个条形码时都必须单击“添加”按钮很乏味。

My Question

我的问题

Is there a way I can have the text in textbox1.text automatically append to RichTextBox as soon as a barcode is scanned? I want to eliminate having to click the 'Add' button.

有没有办法可以在扫描条形码后立即将 textbox1.text 中的文本自动附加到 RichTextBox?我想消除必须单击“添加”按钮的情况。

Here is my current code (code for the Button1.Click button):

这是我当前的代码(Button1.Click 按钮的代码):

Dim scanData As String = TextBox1.Text

RichTextBox1.AppendText(scanData + " " + Format(TimeOfDay, "HH:mm:ss") + vbNewLine)

TextBox1.Clear()
TextBox1.Focus()

回答by Hanlet Esca?o

First off, I would make sure the user cannot input text themselves by disabling the TextBoxcontrol (TextBox1.Enabled=False), then add your code to the TextChangedevent:

首先,我会通过禁用TextBox控件 ( TextBox1.Enabled=False)来确保用户无法自己输入文本,然后将您的代码添加到TextChanged事件中:

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
    Dim scanData As String = TextBox1.Text

    RichTextBox1.AppendText(scanData + " " + Format(TimeOfDay, "HH:mm:ss") + vbNewLine)

    TextBox1.Clear()
    TextBox1.Focus()
End Sub

Before appending to the RTB, I would check to make sure the BarCode is valid.

在附加到 RTB 之前,我会检查以确保条码有效。