如何连接两个TextBox中的字符串?

时间:2014-03-17 14:34:40

标签: vb.net string concatenation

例如在此代码中

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

如何使TextBox4.Text显示数字,(=)符号和(+)符号,即

TextBox1.Text =“2”和TextBox2.Text =“3”和TextBox3.Text =“5”

如何制作TextBox4.Text =“2 + 3 = 5”

(字符串不是值)

1 个答案:

答案 0 :(得分:1)

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

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

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

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

或者,对于更复杂的连接,如此,您可能会发现使用String.Format更容易,如下所示:

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