Pass (Send) TextBox and DateTimePicker value to another Form using C# and VB.Net in Windows Application

Last Reply 6 months ago By dharmendr

Posted 6 months ago

Here is the scenario,

I want to pass the values that are IN TEXTBOX AND DATETIMEPICKER in the form1 by clicking a button and it will pass the values in the form2.

Pls help me because i've been searching this for almost 3DAYS. and still haven't found it.

Posted 6 months ago

Hi HAMZA11,

Check this example. Now please take its reference and correct your code.

Form Design

Code

C#

Form1

public static System.Data.DataRow Row { get; set; }
private void btnSend_Click(object sender, EventArgs e)
{
    System.Data.DataTable dt = new System.Data.DataTable();
    dt.Columns.Add(new System.Data.DataColumn("Name", typeof(string)));
    dt.Columns.Add(new System.Data.DataColumn("Date", typeof(DateTime)));
    System.Data.DataRow dr = dt.NewRow();
    dr["Name"] = txtName.Text.Trim();
    dr["Date"] = Convert.ToDateTime(dateTimePicker1.Value);
    dt.Rows.Add(dr);
    Row = dt.Rows[0];
    Form2 form2 = new Form2();
    form2.Show();
}

Form2

private void Form2_Load(object sender, EventArgs e)
{
    lblName.Text = Form1.Row["Name"].ToString();
    lblDate.Text = Convert.ToDateTime(Form1.Row["Date"]).ToShortDateString();
}

VB.Net

Form1

Public Shared Property Row As DataRow
Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
    Dim dt As DataTable = New DataTable()
    dt.Columns.Add(New DataColumn("Name", GetType(String)))
    dt.Columns.Add(New DataColumn("Date", GetType(DateTime)))
    Dim dr As DataRow = dt.NewRow()
    dr("Name") = txtName.Text.Trim()
    dr("Date") = Convert.ToDateTime(dateTimePicker1.Value)
    dt.Rows.Add(dr)
    Row = dt.Rows(0)
    Dim form2 As Form2 = New Form2()
    form2.Show()
End Sub

Form2

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    lblName.Text = Form1.Row("Name").ToString()
    lblDate.Text = Convert.ToDateTime(Form1.Row("Date")).ToShortDateString()
End Sub

Screenshot