none
Transfering data from one Windows form to another. RRS feed

  • Question

  • Hello, I am a Novice Programmer.

    I have a problem, of getting data from one form to another. Let me explain.

    In Windows Form1, I have Input Boxes for decimal numbers for me to input and these are link to be added and divided to form a total, to give a average. The problem is, I would like to output the result onto a second form, either resided on a label or a textbox in Form2. Now, I do not have a problem with having an output result on form1 by providing it with a label or a textbox on that form, but not with the second form. the only connection with the second form at the moment is a 'Next' button Me. hide() and Form2.Show().

    This is the program listing on Windows Form1:

            Dim intFormPtsTotal As Integer = 0
            Dim decFastBreakTotal As Decimal = 0, decGradedTimeTotal As Decimal = 0, decWinGradedTimeTotal As Decimal = 0

            intFormPtsTotal = Val(TextBox2.Text) + (TextBox3.Text) + (TextBox4.Text) + (TextBox5.Text) + (TextBox6.Text)

            decFastBreakTotal = (Val(TextBox7.Text) + (TextBox8.Text) + (TextBox9.Text) + (TextBox10.Text) + (TextBox11.Text)) / 5

            decGradedTimeTotal = (Val(TextBox12.Text) + (TextBox13.Text) + (TextBox14.Text) + (TextBox15.Text) + (TextBox16.Text)) / 5

            decWinGradedTimeTotal = (Val(TextBox17.Text) + (TextBox18.Text) + (TextBox19.Text) + (TextBox20.Text) + (TextBox21.Text)) / 5

    The above, is to work out the average.

           And this is what I put on Windows Form2:

            Me.Label11.Text = Form1.intFormPtsTotal.Text
            Me.Label12.Text = Form1.decFastBreakTotal.Text
            Me.Label13.Text = Form1.decGradedTimeTotal.Text
            Me.Label14.Text = Form1.decWinGradedTimeTotal.Text

            The outcome of this, was no response when I run the program. Any suggestions will be appreaciated. thank you.


    • Edited by MuCanceri10 Monday, February 25, 2019 11:59 AM
    Monday, February 25, 2019 12:54 AM

All replies

  • Hi,

    You want to transfer the value of Form1 to Form2,code:

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Form2.Show()
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Form2.TextBox1.Text = Me.TextBox1.Text
        End Sub
    End Class

    Best Regards,

    Alex


    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, February 25, 2019 1:51 AM
  • Hi

    First off, please post code by using the codeblock tool from the toolbar.

    *

    Here is one way to show results in second form.

    Form1 has TextBox1 and TextBox2, Label1 and Button1.

    When Button1 is clicked, Form1 is hidden and Form2 (named f2 here) is shown with the results of TextBox1 times TextBox2 in Label1.

    When F2 is closed, Form1 is shown again.

    *

    The key thing is to fully qualify the statements in the code.

    *

    Form1 code

    ' Form1 with TextBox1,TextBox2,
    ' Label1 and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim f2 As New Form2
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim a As Decimal = CDec(TextBox1.Text)
        Dim b As Decimal = CDec(TextBox2.Text)
        Label1.Text = (a * b).ToString

        f2.Show()
        Hide()
      End Sub
    End Class

    Form2 (f2) code

    ' Form1 with Label1
    Option Strict On
    Option Explicit On
    Public Class Form2
      Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        '-------------------------
        ' this is one way
        Label1.Text = Form1.Label1.Text
        '-------------------------

        '-------------------------
        ' and this is another way
        'Dim a As Decimal = CDec(Form1.TextBox1.Text)
        'Dim b As Decimal = CDec(Form1.TextBox2.Text)
        'Label1.Text = (a * b).ToString
        '-------------------------

        ' there are other ways too
      End Sub
      Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Form1.Show()
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Monday, February 25, 2019 1:53 AM
  • Thank You! Much obliged.
    Monday, February 25, 2019 11:46 AM
  • It is, with thanks!
    Monday, February 25, 2019 11:50 AM
  • Hi,

    If you resolve the issue,  please mark the helpful as answer.

    Best Regards,

    Alex


    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.

    Tuesday, February 26, 2019 5:42 AM