Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

visual studio 2010 - C# “does not contain a constructor that takes '1' arguments”

I have read through some of the posts on this site relating to this error but I still can't work out how to do this - I'm quite new to C#.

I am trying to pass multiple text box data (only 2 to start with) from Form1 to Form3 (Form2 will be an intermediary added after I get this working) The idea being to create several forms which pass data to the last form and display using labels, Form3 at the moment, and then Form3 will save everything to a file or database. Hope that makes sense.

So, here's a couple of snippets from my code:

On Form1 I have:

    public Form1()
    {
        InitializeComponent();
    }

    private void nextBtn_Click(object sender, EventArgs e)
    {
        Form3 a = new Form3(firstNameTxtBox.Text);
        a.Show();

        Form3 b = new Form3(lastNametextBox.Text);
        b.Show();

        this.Hide();
    }

On Form3 I have:

    public partial class Form3 : Form
    {
        public Form3(string a, string b)
        {
           InitializeComponent();
           firstNameLbl.Text = a;
           lastNameLbl.Text = b;
        }
    }

Now, if I take out string b, it works fine so what am I doing wrong please?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here

Form3 a = new Form3(firstNameTxtBox.Text);

you are calling the Form3 constructor with one argument.

As the error explains, Form3 does not contain a constructor that takes a single argument. This is why when you remove the second argument from the constructor the error goes away.

You have two options:

1) Remove the second constructor argument.

public Form3(string a)
{
    InitializeComponent();
    firstNameLbl.Text = a;
}

2) Add the second argument to all the places where you call the Form3 constructor.

If you need the second constructor argument I suggest option 2.

For example:

Form3 a = new Form3(firstNameTxtBox.Text, lastNametextBox.Text);

Your final Form1 code would look something like:

public Form1()
{
    InitializeComponent();
}

private void nextBtn_Click(object sender, EventArgs e)
{
    Form3 a = new Form3(firstNameTxtBox.Text, lastNametextBox.Text);
    a.Show();

    this.Hide();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...