vba 如何在文本框 VB+ 中设置默认值

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

HOw to set default value in text box VB+

vb.netvbavisual-studio-2012

提问by AMPS

I am working On VB and Arduino At same time. My main intention is to get data serially displayed on Textbox of VB

我同时在 VB 和 Arduino 上工作。我的主要目的是在VB的Textbox上串行显示数据

Now i wanted to how can assign string or Serial data out put to textbox. I have google it but the sytax is not working I created simple textbox AND assigned variable abcd , Now i wanted to display Abcd on text box.

现在我想如何将字符串或串行数据输出到文本框。我用谷歌搜索了它,但语法不起作用我创建了简单的文本框并分配了变量 abcd ,现在我想在文本框中显示 Abcd 。

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        TextBox1.Text = "ABCD"
    End Sub

If i wanted to read serial data display it on textbox

如果我想读取串行数据,将其显示在文本框中

Private Sub Current_Read_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Open()
        Dim str As String
        str = SerialPort1.ReadExisting
        Current_Read.Text = str
        SerialPort1.Close()
    End Sub

So why it not working

那么为什么它不起作用

回答by Suji

Your code doesn't result as you expected because the code which you are using will executed only when you type any thing in the textbox, while using this code you can't able to type anything in the textbx when you type any thing in it will result in "ABCD". to avoid this Move this code TextBox1.Text = "ABCD"from TextBox1_TextChangedevent to form_Loadevent. that is

您的代码不会像您预期的那样产生结果,因为您正在使用的代码只会在您textbox在 .结果在"ABCD". 避免这种情况 将此代码TextBox1.Text = "ABCD"从一个TextBox1_TextChanged事件移动到form_Load另一个事件。那是

  Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       TextBox1.Text = "ABCD"
  End Sub

回答by heysulo

Try this for some reason. Add a button and double click the button so you can write your code to the event Button_Click

出于某种原因尝试这个。添加一个按钮并双击该按钮,以便将代码写入事件 Button_Click

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    SerialPort1.Open()
    Dim str As String
    str = SerialPort1.ReadExisting
    Current_Read.Text = str
    SerialPort1.Close()
End Sub

run the application. and click the button

运行应用程序。然后点击按钮