Net-informations.com

Passing Values between forms

How to Pass Data Between Forms

passing values between forms c# vb.net

In C# and vb.net, there are many situations the new programmers face the same problem about how to pass data and values from one form to another. We can pass values from one form to another in a number of ways. Here you can see one of the easiest method to pass values from one form to another.

passing data between forms c# vb.net

In order to communicate between the forms, we are using the Forms constructor to send these values. Constructors performs initialize the data members of the new object and it has the same name of the class. Here we send the values as arguments of the constructor.

C#
public Form2(string title,string txt)
{
    InitializeComponent();
    this.Text = title;
    label1.Text = txt;
}
VB.Net
Public Sub New(ByVal sTitle As String, ByVal sText As String)
    InitializeComponent()
    Me.Text = sTitle
    Me.Label1.Text = sText
End Sub

InitializeComponent

InitializeComponent

In the above method you can see InitializeComponent() method. This method is automatically created and managed by Windows Forms designer and it defines everything you see on the form. It is better you don't try to modify the InitializeComponent method.

Passing values step by step
passing values between windows forms c# vb.net

Open a new project and drag two text boxes and a button in the Form1.

Add another form (Form2) in the project and add Label control on it

And we are passing these two textbox values from Form1 to Form2

Here, call the constructor of Form2 and pass these values and set these values in form2

C# Source Code Form1

using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string nTitle = textBox1.Text;
            string nText = textBox2.Text ;
            Form2 frm = new Form2(nTitle, nText);
            frm.Show();
        }
    }
}
Form2

using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
        }
        public Form2(string title,string txt)
        {
            InitializeComponent();
            this.Text = title;
            label1.Text = txt;
        }
    }
}
VB.Net Source Code Form1

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
Form2

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

Windows Forms

Windows programmers have made extensive use of forms to build user interfaces. Each time you create a Windows application, your Visual Studio will display a default blank form, onto which you can drag and drop controls from the Toolbox window. More about.... C# Forms and VB.Net Forms

Form on Top of All Other Windows

You can bring a Form on top of an application by simply setting the Form.topmost form property to true will force the form to the top layer of the screen. More about.... How to keep Form on Top of All Other Windows

MDI Form

A Multiple Document Interface (MDI) programs can display multiple child windows inside them. This is in contrast to single document interface (SDI) applications, which can manipulate only one document at a time. More about.... C# MDI Form



NEXT.....Autocomplete TextBox
Tutorial
Python  | JavaScript  | Java  | jQuery  | Data Science  | C#  | Asp.Net  | Vb.Net


Interview Questions
Python  | JavaScript  | Java  | jQuery  | Data Science  | Oops  | C#  | .Net Framework  | Asp.Net  | Vb.Net


net-informations.com (C) 2023    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About