Sharing data between forms in VB or getting input value from one form and sending it to another form to display in Visual basic VB.net.
In this VB.net example will see multiple examples to share data between forms.
Send input data from one form to another form in VB.net
If we want any value to be transferred from one form to another, the value from child form to display it in a parent form, we can do this thing by declaring the public static variable in child form.
In case of vise versa or reverse- if we want to pass the data to child form so you have to use the public static variable in a parent form.
For the communication between the forms, we will use the Forms constructor to send these values.
The constructors have the same name of the class it performs the task of initialising the data members of the new object.
We will send the values as arguments of the constructor. Follow the procedure to create this application
Steps to send input data from one form to another form in VB.net
Step 1:
Initialize Component – In this, you can see InitializeComponent() method it is automatically created and managed by the Windows Forms designer and it defines everything that you can see on the form, and also It is better that you don’t try to modify the InitializeComponent method.
Public Sub New(ByVal sTitle As String, ByVal sText As String) InitializeComponent() Me.Text = sTitle Me.Label1.Text = sText End Sub
Step 2:
Open a new project and drag two text boxes and one button in Form1. We will pass these two textboxes values on form2.
Now we have to call the constructor of Form2 and pass these values and set these values in form2.
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim sTitle As String Dim sText As String sTitle = TextBox1.Text sText = TextBox2.Text Dim frm As New Form2(sTitle, sText) frm.Show() End Sub End Class
Step 3:
Now take another form (Form2) in the project and add a Label control to it.
Public Class Form2 Public Sub New(ByVal sTitle As String, ByVal sText As String) InitializeComponent() Me.Text = sTitle Me.Label1.Text = sText End Sub End Class
Here we can see that we are setting the values in the arguments form1 TextBox1 value as a form2 title by argument sTitle and the form1 Textbox2 value by argument sText as lable of form 2.
When we run the application the form1 will display and when we enter values in two textboxes and press the button1 (Enter form2 title) then it will pass these two values on the form2 and show the result like below.
Output screen:
Sending Data Between Multiple Forms in VB.NET
Let’s assume a parent form that contains an input field and a button. when the user enters a value in input and clicks on this button then data should be shared in at least two forms and display the results.
Step 1:
Create one project in visual basic on Form1 drag and drop one Textbox name it username and one button as save. And write the below code on the form.
Form1
Public Class Form1 Private _username As String Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim formTwo As new Form2(TextBox1.Text) formTwo.Show() Me.Close() End Sub End Class
Step 2:
Add one more form in your project that is Form2 and take one label and a button on it name the Button as Next. And write the below code in this form.
Form 2
Public Class Form2 Private _username As String Public Sub New(username As String) _username = username End Sub Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load Label1.Text = $"Hi {_username}!" End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim formThree As new Form3(_username) formThree.Show() Me.Close() End Sub End Class
Step 3:
Now add one more form in your project that is Form3 and take another label on it. And write the code below.
Form 3
Public Class Form3 Private _username As String Public Sub New(username As String) _username = username End Sub Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load Label2.Text = _username End Sub End Class
Output:
Now run the project you will see that Form1 will display when you enter any text in textbox and click on save button then this form will close.
And Form 2 will show with the text you have entered in the TextBox of Form1.
And when you click on Next Button on this form then this form will close and Form 3 will show with the same text.
So you can see this how is our application sharing the data between multiple forms in this way we can easily share data between two or more forms in VB.NET.