I want to retrive some controls form form1 to form2.

i dont know how to do it.

Please any one help me to solve this problem...

virusisfund

Hi,

I have already used this thread code in my prject but its not working. or I dont understant that where the code is written and how. Because when i tried to use code in my project it display me an error message "form1 is not in current
context."

as i understant i wirte the code like this:

In form1

public partial class Form1 : Form1
    {
        public Form1()
        {
            InitializeComponent();

            form2 = new Form1(this);
        }

on form 2

public partial class Form1 : Form
    {
        public Form1(Form1 F)
        {
            InitializeComponent();
            form1 = F;

        }

set lable modifier property to public

Is this is the correct way to code this ..

Hmm, what you need to do is.

Make a Get Method. Say you want Form1.label1.text

You would do this in Form1.

string getLabel1()
{
    return Label1.Text;
}

Now in Form2 you do the following

private void button1_Click(object sender, EventArgs e)
{
    Form1 frm1 = new Form1()
    string Frm1_Lbl1 = frm1.getLabel1();
}

Repeat as necessary.

enjoy.

Change code for Form1:

public partial class Form1 : [B]Form[/B]
    {

        [B]Form2 form2;[/B]

        public Form1([B]Form2 F[/B])
        {
            InitializeComponent();

            [B]form2 = F;[/B]
        }

And Form2:

public partial class [B]Form2[/B] : [B]Form[/B]
{

        [B]Form1 form1;[/B]

        public Form2([B]Form1 F[/B])
        {
            InitializeComponent();

            [B]form1 = F;[/B]
}

If there's a control on form1 that you want to access on form2 then change its Modifiers property to "Public".

And you can access it in Form2 like this:

// ControlName is the name of the control
form1.[I]ControlName[/I].Text = "test";

Thanks

Edited 6 Years Ago by farooqaaa: n/a

finito and farooqaaa have it right between them.
If you want to access members of Form1 from Form2 then Form2 needs to contain a refernce to Form1. Use the code he showed you to pass a reference in Form2's constructor.
However, it is bad OO to directly access a member of a class, so settign the control modifier to public isnt good OO design. Instead, use a property as finito showed you. He was accessing the property of a freshly created Form instead of the existing one, but the code for the property is correct.

now the controls can nvokeable but when i run the program it show me the following error message.
Please tell me why this happen.


Application.Run(new Form1());

'WindowsFormsApplication1.Form1' does not contain a constructor that takes '0' arguments

Please post some code.

This can mean that you are not passing the Values correctly to Form2 or w/e your form is called.

Sorry, I made a mistake.

You won't have to pass "Form2" as argument in form1 because you will already have an instance of form2 within form1.

Change Form1:

public partial class Form1 : Form
    {

        Form2 form2;

        public Form1()
        {
            InitializeComponent();
        }

Now when you want to show form2 just pass form1 as argument:

form2 = new Form2(this);

form2.Show();
finito and farooqaaa have it right between them.
If you want to access members of Form1 from Form2 then Form2 needs to contain a refernce to Form1. Use the code he showed you to pass a reference in Form2's constructor.
However, it is bad OO to directly access a member of a class, so settign the control modifier to public isnt good OO design. Instead, use a property as finito showed you. He was accessing the property of a freshly created Form instead of the existing one, but the code for the property is correct.

The OP is a complete beginner. He won't feel comfortable making a specific property for each control he wants to access.

The OP is a complete beginner. He won't feel comfortable making a specific property for each control he wants to access.

True but bad practices stick!

finito is spot on, breaking bad habits is much harder than learning the correct way first.
I appreciate the fact that adding the properties to the sample code adds a level of complexity but if the OP needs help with creating the properties there are plenty of people here who can help him.
It is much better to teach correct practice from the start to avoid ingraining bad ones.

Comments
Thanks for elaborating the point. :)

I agree with you guys.

It takes courage to admit when one is wrong.
I applaud you. cheers.

Edited 6 Years Ago by finito: n/a

thanks but I got it to work I created a class and created static members so that I can use the back, next and cancel buttons.

This article has been dead for over six months. Start a new discussion instead.