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

Recommended Answers

All 13 Replies

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

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.

commented: Thanks for elaborating the point. :) +2

I agree with you guys.

commented: Thanks for Agreeing +2

I agree with you guys.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.