C# Tutorials and offshore development in India
Tutorials Resources Forum Reviews Communities Interview Jobs Projects Training Your Ad Here


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » General »

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


Posted Date: 24 Sep 2009    Resource Type: Articles    Category: General
Author: Hefin DsouzaMember Level: Diamond    
Rating: 1 out of 5Points: 12



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


Attachments

  • Download Sample (33169-241445-PassingValues.zip)


  • Responses to the resource: "Different Ways of Passing Values from One Form to another in VB.NET and C#"
    Author: kaleeswaran    30 Sep 2009Member Level: Gold   Points : 1
    Hi,

    Good presentation.
    keep on posting

    Regards,
    Kaleeswaran
    http://4xchange.blogspot.com


    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Passing values from one form to another  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Smart Client Application Components
    Previous Resource: JavaScript Questions and Solutions
    Return to Discussion Resource Index
    Post New Resource
    Category: General


    Post resources and earn money!
     
    More Resources




    About Us    Contact Us    Privacy Policy    Terms Of Use