Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How should i access controls on other forms


Example: i have a datagridview in Form2, I will send values to it from form1.
I tried to access it by creating object of that form but no results.
please help how can i do it
Posted

Have a look at this article.You may get some idea.

Passing Data Between Forms[^]
 
Share this answer
 
Comments
TweakBird 20-Jul-11 12:54pm    
Good Article. CP Rocks. My 5!.
 
Share this answer
 
Comments
Uday P.Singh 20-Jul-11 12:23pm    
nice link my 5!
[no name] 20-Jul-11 14:17pm    
Thanks Uday.
TweakBird 20-Jul-11 12:54pm    
Good call. My 5!
[no name] 20-Jul-11 14:18pm    
Thanks Tweak.
Create a public method in Form2...

public void AddToDataGrid()
{
   // pass whatever it is you wish to add, and add it to the gridview here.
}


Then, you can call this method from Form1 using the reference you have to Form2...

Form2 otherForm = new Form2();
otherForm.Show();

otherForm.AddToDataGrid();
 
Share this answer
 
Comments
TweakBird 20-Jul-11 12:55pm    
Nice effort. My 5!
fcronin 20-Jul-11 13:40pm    
Thanks :)
This is a popular question about form collaboration. Most robust way is implementing appropriate interface in form class.

Please see more detail in my past answer to this question: How to copy all the items between listboxes in two forms[^]. See other suggestions and the discussion.

—SA
 
Share this answer
 
You can use the Reference Type feature of the class ..... Form that you create is actually a class only .... What you can do is ... For Example :

If you have two forms .... Form1 and Form2

You have some data on Form1 and you want to pass it on to Form2 .... for example you want to pass the text value of the TextBox which is on Form1 to Form2

Then you can create a Overloaded Constructor of Form2 and pass the current object of Form1 to that ..... refer the code below

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2(this);
            frm.Show();
        }
    }

public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        Form1 obj;
        public Form2(Form1 frm1)
        {
            InitializeComponent();
            obj = frm1;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(obj.Controls["textbox1"].Text);
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900