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

Dani AI

Generated

Several good ideas already in this thread. rightly recommends exposing values rather than exposing controls; flags the OO problem with making controls public; clarified the constructor/instantiation flow; and shows a static approach (works for simple cases but has trade‑offs). The runtime error reported by ("does not contain a constructor that takes 0 arguments") is the direct consequence of replacing or removing Form1's parameterless constructor — Application.Run expects to be given an instance that can be constructed the way Program.Main calls it.

Practical fixes and best practices (no public control fields)

  • Restore a parameterless Form1 constructor (or change Program.Main so it constructs Form1 with correct arguments) rather than trying to create a new Form1 from inside Form2 to read values — creating a new Form1 returns a fresh instance with default state.
  • Prefer exposing a small, purpose‑specific API (property or method) that returns only the required data instead of making controls public. That keeps encapsulation and avoids brittle coupling.

Decoupling patterns that avoid tight coupling

  • Interface injection: define a tiny interface implemented by Form1 and pass that into Form2 so Form2 only depends on the interface.
    
    interface ILabelProvider { string LabelText { get; } }

class Form1 : Form, ILabelProvider
{
public string LabelText => myLabel.Text; // exposes value only
}

class Form2 : Form
{
public Form2(ILabelProvider provider)
{
// use provider.LabelText to populate controls
}
}


- Event callback: Form2 exposes an event or Func<string> that Form1 wires up so Form2 pulls current values when needed.  
- Owner/casting: set Form2.Owner = this when showing it and cast Owner to the interface inside Form2 (less explicit than injection but common for dialog flows).

Quick troubleshooting checklist
- If Application.Run(...) fails, check Form1 constructors and restore a parameterless ctor or update Program.Main.  
- Avoid constructing a new Form1 just to read values. Pass the data or a small API instead.  
- Prefer interfaces or events for testable, maintainable code. Static state is easiest but increases coupling and lifecycle issues.  
- If control access happens from another thread, marshal calls with Invoke/BeginInvoke.

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.