-2

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.

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

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

(the string not the value)

djv
  • 15,168
  • 7
  • 48
  • 72
user3428637
  • 39
  • 1
  • 1
  • 10

1 Answers1

1

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

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:

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:

TextBox4.Text = String.Format("{0} + {1} = {2}", TextBox1.Text, TextBox2.Text, TextBox3.Text)
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
PugFugly
  • 504
  • 2
  • 6
  • I would use & and not + to concatenate... TextBox4.Text = TextBox1.Text & " + " & TextBox2.Text & " = " & TextBox3.Text – Mych Mar 17 '14 at 14:42
  • I think + is right....look here http://stackoverflow.com/questions/8630146/how-to-get-text-and-a-variable-in-a-messagebox – Frank Tudor Mar 17 '14 at 14:44
  • If you want to gain more reputation, I recommend making your answers more explanatory, clear, and thorough. I've edited your answer to show you an example of what I mean. I've found that often being thorough and helpful is just as important as being correct. – Steven Doggart Mar 17 '14 at 14:49
  • @FrankTudor it is not right... look here http://stackoverflow.com/questions/1088053/string-manipulation-with-or-in-vb-net . Your linked question even uses legacy VB6 functions such as `MsgBox` which should be avoided in favor of .NET's `MessageBox.Show()` – djv Mar 17 '14 at 15:40