mhaine
mhaine

Reputation: 1

2 different forms that can both open form3

*i can't figure out how to allow form1 to be passed to form3 and form2 to be able to pass to form3 too. PLEASE HELP!

it has this error: does not contain a constructor that takes '1' argument on the ff lines:

f3 = new Form3(this); on Form1

f3 = new Form3(this); on Form2

Here are the codes:

Base Form(choose to form1 or form2):

namespace WindowsFormsApplication1
{
    public partial class baseform : Form
    {
        Form1 f1;
        Form2 f2;

        public baseform()
        {
            InitializeComponent();
            f1 = new Form1(this);
            f2 = new Form2(this);
        }

        private void baseform_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            f1.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            f2.Show();
        }
    }
}

Form1:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
            baseform bf;
            Form3 f3;

            public Form1(baseform bf1)
            {
                    InitializeComponent();
                    f3 = new Form3(this);
                    bf = bf1;
            }

            private void button1_Click(object sender, EventArgs e)
            {

            }
    }
}

Form2:

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
            baseform bs;
            Form3 f3;

            public Form2(baseform bs1)
            {
                    InitializeComponent();
                    f3 = new Form3(this);
                    bs = bs1;
            }

            private void button1_Click(object sender, EventArgs e)
            {

            }
    }
}

Form3:

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
            Form1 f1;
            Form2 f2;

            public Form3(Form1 f1a, Form2 f2a)
            {
                    InitializeComponent();
                    f1 = f1a;
                    f2 = f2a;
            }

            private void Form3_Load(object sender, EventArgs e)
            {

            }
    }
}

Upvotes: 0

Views: 530

Answers (5)

John Bartels
John Bartels

Reputation: 2763

            //This code is placed where you want to Switch Back to Form 1, so 
            // wherever in Form2 that you determine you have completed using Form2, such as
            // a submit button or whatever... 

            private void submitButton_Click(object sender, EventArgs e)
            {
                Form1 switchBack = (Form1)Application.OpenForms["Form1"];
                switchBack.childFormMethodCall(submitItemTextBox.Text);
            }

            //Then in Form1.cs, define the Method you wish to call from the second Form
            //In my case, I did a simple method called childFormMethodCall with a single passed
            //in parameter, a string 
            //I then set the string that was Passed in from Form2 as the Text for richTextBox1
            //richTextBox1 is located on Form1

            internal void childFormMethodCall(string p)
            {
                richTextBox1.Text = p; 
            }

Upvotes: 0

Alex
Alex

Reputation: 758

You try to create a form3 with constructor with 1 paramentrs.

  f3 = new Form3(this);

But your constructor in Form3 is defined for 2 parameters

   public Form3(Form1 f1a, Form2 f2a)

chnage your code to this.(given for for form3 and form2)

    namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
            public Form1 f1{get;set;}
            public Form2 f2{get;set;}

            public Form3()
            {
                    InitializeComponent();
            }

            private void Form3_Load(object sender, EventArgs e)
            {

            }
    }
}


namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
            baseform bs;
            Form3 f3;

            public Form2(baseform bs1)
            {
                    InitializeComponent();
                    f3 = new Form3();
                    //and same for for form 2
                    f3.f2=this;
                    bs = bs1;
            }

            private void button1_Click(object sender, EventArgs e)
            {

            }
    }
}

Upvotes: 0

Maciej
Maciej

Reputation: 7961

Your are having cyclic references here. I suggest instead of constructor parameters use public properties to let each form know about other forms. Before doing anything with such a reference check if it is assigned (!=null). You need to create all forms first and then assign their properties to establish references.

public partial class Form3 : Form
{
    public Form1 f1 { get;set;}
    public Form2 f2 { get;set;}

    ...

    private void button1_Click(object sender, EventArgs e)
    {
        if(f1 != null)
            f1.Show();
    }
}

Then do that:

Form f1 = new Form1();
Form f2 = new Form2();
Form f3 = new Form3();
f3.f1 = f1;
f3.f2 = f2;

Follow that pattern with other forms too.

Upvotes: 0

Shadow Wizzard
Shadow Wizzard

Reputation: 66388

Plainly speaking, the error is pretty clear: Form3 does not have constructor that take one argument. It can take only 2 arguments.

However, you problem is deeper than that. The most simple workaround to your problem will be to make the fields static, then add extra method to initialize them:

public partial class Form3 : Form
{
    static Form1 f1;
    static Form2 f2;

    public Form3()
    {
        InitializeComponent();
    }

    public static void InitForms(Form1 f1a, Form2 f2a)
    {
        f1 = f1a;
        f2 = f2a;
    }

    private void Form3_Load(object sender, EventArgs e)
    {   
        //use f1 and f2...
    }
}

Then have this:

    public baseform()
    {
        InitializeComponent();
        f1 = new Form1(this);
        f2 = new Form2(this);
        Form3.InitForms(f1, f2);
    }

Upvotes: 0

ChrisF
ChrisF

Reputation: 137158

Well you're only constructor in class Form3 has two arguments:

public Form3(Form1 f1a, Form2 f2a)

but in both places you are trying to call it with only one:

f3 = new Form3(this);

So, either call it with two arguments (perhaps null?) or create a constructor (or two) that takes one argument. In former case you'll need to have:

f3 = new Form3(this, null);

when calling from Form1 and:

f3 = new Form3(null, this);

when calling from Form2

Upvotes: 1

Related Questions