locked
add values of two text box and show the result in the another form RRS feed

  • Question

  • I have values of two text box in two different forms. I am trying to add these values and show the result in label in the third form but it did not work!!! the code i have used in form2 is :

    form3.lable1.text = Textbox2.Text +form1.Textbox.1

    Monday, July 3, 2017 3:38 AM

Answers

  • I have values of two text box in two different forms. I am trying to add these values and show the result in label in the third form but it did not work

    The text property of a textbox is a string, so it cannot be added.   You need to convert that string to a number first.  See here for one method that is relatively simple (there are several ways it can be done).
    https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx
    https://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx

    That method allows you to easily detect a failure: you will need to decide what to do when the conversion fails.  Note that the property of the text box control is '.Text' - your original code would not compile.

    Then you need to get access to the forms.   The 'form3' and 'form1' references that you are using are the default, but they are unreliable and should be avoided.   The procedure depends on how the forms are created, but one way or another you need a variable that references the instance of the form where the control is: a form is just a particular type of class, and there could be many instances of the form object.  See here for some alternatives for getting the form instance into a variable that you can use to reference controls on the form, or values that you create as properties of the form (3, 4 and 5 are probably most suitable):
    https://vbdotnetblog.wordpress.com/forms/

    • Marked as answer by ahmed199653 Thursday, July 6, 2017 7:22 AM
    Monday, July 3, 2017 5:37 AM

All replies

  • I have values of two text box in two different forms. I am trying to add these values and show the result in label in the third form but it did not work

    The text property of a textbox is a string, so it cannot be added.   You need to convert that string to a number first.  See here for one method that is relatively simple (there are several ways it can be done).
    https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx
    https://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx

    That method allows you to easily detect a failure: you will need to decide what to do when the conversion fails.  Note that the property of the text box control is '.Text' - your original code would not compile.

    Then you need to get access to the forms.   The 'form3' and 'form1' references that you are using are the default, but they are unreliable and should be avoided.   The procedure depends on how the forms are created, but one way or another you need a variable that references the instance of the form where the control is: a form is just a particular type of class, and there could be many instances of the form object.  See here for some alternatives for getting the form instance into a variable that you can use to reference controls on the form, or values that you create as properties of the form (3, 4 and 5 are probably most suitable):
    https://vbdotnetblog.wordpress.com/forms/

    • Marked as answer by ahmed199653 Thursday, July 6, 2017 7:22 AM
    Monday, July 3, 2017 5:37 AM
  • Hi ahmed199653,

    You can try to use the constructor to do this.

    Form 1:

     Public a As Integer
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            a = Convert.ToInt32(TextBox1.Text.ToString())
            Dim f As New Form2(a)
            f.ShowDialog()
        End Sub

    Form 2:

     Public b As Integer
        Public Sub New(a As Integer)
            b = a
            InitializeComponent()
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f1 As New Form1
            b += Convert.ToInt32(TextBox1.Text.ToString())
            Dim f As New Form3(b)
            f.ShowDialog()
        End Sub

    Form 3:

     Public c As Integer
        Public Sub New(b As Integer)
            c = b
            InitializeComponent()
        End Sub
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Label1.Text = c
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 3, 2017 7:56 AM