vb.net 你如何连接来自两个 TextBox 的字符串?

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

How do you concatenate strings from two TextBoxes?

vb.netstringconcatenation

提问by user3428637

for example in this code

例如在这段代码中

Public Class Form1

    Dim a As Object
    Dim b As Object
    Dim c As Object

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        a = Val(TextBox1.Text)
        b = Val(TextBox2.Text)
        c = Val(TextBox3.Text)

        TextBox3.Text = a + b

       ' TextBox4.Text = "a + b = c"
    End Sub

End Class

How can i make TextBox4.Text show the numbers, (=) sign, and (+) sign i.e.

我怎样才能让 TextBox4.Text 显示数字、(=)符号和(+)符号,即

TextBox1.Text = "2" and TextBox2.Text = "3" and TextBox3.Text = "5"

TextBox1.Text = "2" and TextBox2.Text = "3" and TextBox3.Text = "5"

How can i make TextBox4.Text = "2 + 3 = 5"

我怎样才能让 TextBox4.Text = "2 + 3 = 5"

(the string not the value)

(字符串不是值)

回答by PugFugly

You can concatenate strings by using either the &or +operator, like this:

您可以使用&or+运算符连接字符串,如下所示:

TextBox4.Text = TextBox1.Text & " + " & TextBox2.Text & " = " & TextBox3.Text

In VB.NET, the &operator is preferred for string concatenations, but, as long as you have Option Strict On, the +operator is just as safe to use:

在 VB.NET 中,&运算符首选用于字符串连接,但是,只要您有Option Strict On+运算符就可以安全使用:

TextBox4.Text = TextBox1.Text + " + " + TextBox2.Text + " = " + TextBox3.Text

Alternatively, for more complicated concatenations, like this one, you may find it easier to use String.Format, like this:

或者,对于更复杂的串联,比如这个,你可能会发现它更容易使用String.Format,就像这样:

TextBox4.Text = String.Format("{0} + {1} = {2}", TextBox1.Text, TextBox2.Text, TextBox3.Text)