Multiple Forms in C#

Posted on

How to share data among multiple forms in C#?

In some cases we need to pass data from one page to another page in win forms.

For these one have various alternatives. This can be accomplished by using either of the following:

1. Constructor

2. Objects

3. Properties

4. Delegates

Instructions before using:

1. Create a new form by selecting on windows application or either by pressing shift + alt + A. Name it as Form_sender. We are going to use this form to send values to another form named as Form_reciever.

2. Now drag a textbox and a button on the sender form.

3. Add another winfrom using the same process as discussed in step 1 and

name it as “Form_reciever”.

4. Now place a label on receiver form where we are going to receive the value from the sender form.

1. Passing values using Constructor

For this first create a constructor of receiver form as shown below:

On Receiver Form

Public Form_reciever (string strtextbox)

{

InitializeComponent();

Label1.text=strtextbox; //Here we are receiving value and passing it to label

}

On sender Form

On button click event:

Create an object of receiver form to whose constructor you are sending a string value.

Form_reciever frm=new Form_reciever(textbox1.text);

// in this we are creating an object and passing value to the constructor of the receiving form.

2. Passing values using Object

Objects are of reference types and are created using new keyword. In this section we are going to pass data using objects. This method is quite simple. As in receiver form just create the object of the sender class. Then create object of receiver form on the button click event of sender form. After that pass the values of sender form object to the object of receiver form. In the last call the receiver form to show it using show() method.

Procedure for such is given below:

1. Change the textbox on sender form from private to public:

public class Form_Sender

{

public System.Windows.Forms.TextBox textbox1;

2. On its button click event type following code:

Form_reciever frm=new Form_reciever();

Frm.frm1=this; //frm1 is an object created on receiver form

frm.show();

3. On Form_Reciever, create an object of Sender class

Public Form_sender frm1;

4. On the load event of receiver form, type cast the object of form_sender to form_sender and access form_sender’s textbox and assign its text to label’s text.

label1.text=((Form_sender)frm1).textbox1.Text;

3. Passing values using Properties approach

Properties helps user to access the member of another class like their own members. In this method we use property to navigate the values of one form to another. In this case we create a property for retrieving values of one form namely form 1 to another form namely form2.

For this on form 1 we create a property , to retrieve the value of the textbox and one form to we set the value of the property to label’s text property.

Step 1

Add a property in form1 to recover esteem from textbox:

Shroud Copy Code

open string _textBox1

{

get{return textBox1.Text;}

}

Step 2

Add a property in form2 to set the names’ content:

Shroud Copy Code

open string _textBox

{

set{label1.Text=value;}

}

Step 3

In form1’s catch click occasion handler, include the accompanying code:

Conceal Copy Code

private void button1_Click(object sender, System.EventArgs e)

{

Form2 frm=new Form2();

frm._textBox=_textBox1;

frm.Show();

}

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s