Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hello I have 2 Form, that have variables, I would like to send variables from Form1 to Form2,but I did not do this, my codes are below, Could you please help me out;

I have 2 textbox and 1 button for Form1 , For Form2 I have 1 label and 1 button.I need to send d and b values to Form2 label1


Form1 Codes:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim a As Decimal
Dim b As Decimal
Dim c As Decimal
Dim d As Decimal

a = Val(TextBox1.Text)
b = Val(TextBox2.Text)

c = a + b * 5 / 8

d = c / 3
MessageBox.Show(d)

Form2.Show()


End Sub



form2;

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Label1.Text = d + b

End Sub

What I have tried:

see my trails above but I cannot achieve to pass variables between forms
Posted
Updated 14-Feb-17 0:06am

Create a property on Form2 that saves the value you wish to pass into a private variable, call the property before showing the form.

Fr the example below Form1 would say Form2.Setb = b to set the b value.

VB
Public Class Form2
Private b As Decimal
Private d As Decimal

Public WriteOnly Property Setb As Decimal
        Set(value As Decimal)
            b = value
        End Set
    End Property
Public WriteOnly Property Setd As Decimal
        Set(value As Decimal)
            d = value
        End Set
    End Property

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Label1.Text = (d + b).ToString

End Sub


Alternatively create a public global variable on Form2 and set it directly.

VB
Public Class Form2
Public b As Decimal ' You can access these from Form1 by Form2.b = ?
Public d As Decimal

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Label1.Text = (d + b).ToString

End Sub
 
Share this answer
 
v4
Comments
Member 12860952 14-Feb-17 6:05am    
can you help me on codes how can make this?
Michael_Davies 14-Feb-17 6:10am    
Updated the answer, the property method is the best way to go.
Karthik_Mahalingam 14-Feb-17 6:16am    
5
Ralf Meier 14-Feb-17 6:29am    
Please change the Properties from ReadOnly to WriteOnly - in this constellation the code doesn't work ... :(
Michael_Davies 14-Feb-17 6:34am    
That's me all over too fast on the fingers, thank you.
There are a number of ways to pass data between forms, read Passing Data Between Forms[^]
 
Share this answer
 
That would be the easiest (but for me not the best) way :
VB
On Form 1 :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 Form2.Label1.Text = (d + b).tostring

 End Sub


The best way would be to work with properties ...
 
Share this answer
 
v2
Comments
Michael_Davies 14-Feb-17 6:11am    
I was already typing the code but sent the answer half done by mistake, full answer now present.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900