Monday, 8 April 2013

Passing Value Between Form in Csharp (C#) and VB.Net

Passing Value Between Form in Csharp (C#) and 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 Value Form1 to Form2 in Csharp (C#) and VB.Net
  • Here we send the value with calling object properties.
Csharp (C#)
In Form1
private void button1_Click(object sender, EventArgs e)
   {
      Form2 frm = new Form2();
            frm.textBox1.Text = "Hi";
            frm.Show();
        }

  • Send the value as arguments of the constructor.

Csharp (C#)
In Form2
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {

        }
        public Form2(string txt)
        {
            InitializeComponent();
            textBox1.Text = txt;
        }
    }
}

VB.Net
Private Sub button1_Click(sender As Object, e As EventArgs)
 Dim frm As New Form2()
 frm.textBox1.Text = "Hi"
 frm.Show()
End Sub

Public Class Form2
    Public Sub New(ByVal sText As String)
        InitializeComponent()
        Me.textBox1.Text = sText
    End Sub
End Class


Passing Value Form2 to Form1 in Csharp (C#) and VB.Net

Csharp (C#)
In Form1
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Form2 frm;
        public Form1()
        {
            InitializeComponent();
        }

        public string GetValueForm2
        {
            get { return m_value; }
            set
            {
                m_value = value;
            }
        }
        private string m_value;
        
        //Open Form2
        private void Button1_Click(object sender, EventArgs e)
        {
            frm = new Form2(this);
            frm.Show();
        }
        //Click Button2 to Get Passed Value
        private void Button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = m_value;
        }

    }
}

In Form2
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        Form1 ParForm;
        public Form2(Form1 myParForm)
        {
            ParForm = myParForm;
            InitializeComponent();
        }
        
        //Click Button1 to Passing Value to Form1
        private void Button1_Click(object sender, EventArgs e)
        {
            ParForm.GetValueForm2 = textBox1.Text;
        }

    }
}



VB.Net
Public Class Form1

 Private frm As Form2

 Public Property GetValueForm2() As String
  Get
   Return m_value
  End Get
  Set
   m_value = value
  End Set
 End Property
 Private m_value As String

   'Open Form2
 Private Sub Button1_Click(sender As Object, e As EventArgs)
  frm = New Form2(Me)
  frm.Show()
 End Sub
 'Click Button2 to Get Passed Value
 Private Sub Button2_Click(sender As Object, e As EventArgs)
  TextBox1.Text = m_value
 End Sub

End Class

In Form2
Public Class Form2

    'In Form2 add a "New" constructor
    Dim ParForm As Form1
    Public Sub New(ByVal myParForm As Form1)
        MyBase.New()
        ParForm = myParForm
        InitializeComponent()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ParForm.GetValueForm2 = TextBox1.Text
    End Sub
End Class

Thank You

Download Sample Application using Microsoft Visual Studio 2010
Csharp | VB.Net