.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> General >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Different Ways of Passing Values from One Form to another in VB.NET and C#

Posted By:Hefin Dsouza       Posted Date: September 25, 2009    Points: 25    Category: General    URL: http://www.dotnetspark.com  
 

Introduction

This article will demonstrate the different ways using which one can transfer values from one form to another. There are many possible ways of doing this but I have explained some of the simplest ones in this article.

A.     VB.NET

1)  Method 1 : Because VB.NET Creates all Objects as Public one can easily access the Components from another form without much complexity.
On the Load Event of the Form2 write the Following code

Label1.Text = Form1.TextBox1.Text

 

2)      Method 2  : Creating Constructors in the Form2

Public Sub New(ByVal value As String)

        InitializeComponent()

        Label1.Text = value

End Sub


And Creating the Object of Form2 from Form1 using the following code

Private
Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim f As New Form2(TextBox1.Text)

      f.Show()

End Sub

 

3)      Method 3 : Using the Application.OpenForms - Forms Collection

In the Load Event of the Form2 use the Following Code
' This will Call the Instance of the Open form on the zero index.

Dim f As Form1 = Application.OpenForms(0)

Label1.Text = f.TextBox1.Text

 

B.      C#. The techniques are little different in C# the First Method in VB.NET will not work in C#.

1.        Method 1 : Create  a Public Method in Form2 and Pass the value to the Method from Form1
Example
Form2 :-

        public void SetValue(string value)

        {

            label1.Text=value;

        }

Form 1:- Create an Instance of Form2 and Call the Public Method

           Form2 f = new Form2();

                    f.SetValue(textBox1.Text);

           f.Show();

 

 

2.       Method 2 : Creating Constructors in the Form2
Form 2

     public Form2(string value)

     {

            InitializeComponent();

            label1.Text = value;

        }
Form 1
   private void button2_Click(object sender, EventArgs e)
   {
        
Form2 f = new Form2(textBox1.Text);
         f.Show();
   }

3.       Method 3 : Using the Application.OpenForms - Forms Collections
In the Load Event of the Form2 write the Following code

Form1 f = (Form1)Application.OpenForms[0];

label1.Text = f.TextBoxValue; 

 

These are few techniques which can be used while transfering values from one Form to another.

Regards
Hefin Dsouza


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend