简体   繁体   English

在表格之间传递参数的C#错误

[英]C# Error Passing Parameters between forms

let me explain the context, I have a form called "Menu" to call another form called "Bill", I'm using the Bill form to call SearchClient form. 让我解释一下上下文,我有一个名为“菜单”的表单来调用另一个名为“ Bill”的表单,我正在使用“ Bill”表单来调用SearchClient表单。 In Bill form I have a datagridview to receive "passing rows" by SearchClient form. 在Bill表单中,我有一个datagridview来接收SearchClient表单的“传递行”。

I call Bill by Menu button: 我通过菜单按钮致电Bill:

private void button2_Click(object sender, EventArgs e)
    {  Bill bill = new Bill();
       bill.Show();}

However, I have parameters in Bill class. 但是,我在Bill类中有参数。

public Bill(string code, string name, string email)
    {
        InitializeComponent();
        grid.Rows.Add();
        grid.Rows[0].Cells[0].Value = code;
        grid.Rows[0].Cells[1].Value = name;
        grid.Rows[0].Cells[2].Value = email;

    }

I call the "SearchClient" form using Bill button: 我使用“帐单”按钮调用“ SearchClient”表单:

private void button1_Click(object sender, EventArgs e)
    { SearchClient searchcli = new SearchClient();
        searchcli.ShowDialog();}

In SearchClient I have a button to passing rows by a datagridview to another datagridview (found in Bill) 在SearchClient中,我有一个按钮,用于将datagridview的行传递到另一个datagridview(在Bill中找到)

Bill bill = new Bill(grid.SelectedRows[0].Cells[0].Value.ToString(), 
            grid.SelectedRows[0].Cells[1].Value.ToString(),
            grid.SelectedRows[0].Cells[8].Value.ToString());
            bill.Show();

When I run the programm the message error is : Error 'SisStore.Bill' does not contain a constructor that takes 0 arguments. 当我运行程序时,消息错误为:错误'SisStore.Bill'不包含带有0个参数的构造函数。

I know the error is here, because is missing parameters: 我知道错误在这里,因为缺少参数:

 private void button2_Click(object sender, EventArgs e)
    {  Bill bill = new Bill();
       bill.Show();}

But I just need pass information between Bill and SearchClient. 但我只需要在Bill和SearchClient之间传递信息。 The Menu is just a way to summon the Bill form and later SearchClient. 菜单只是召唤Bill表单和后来的SearchClient的一种方式。 Somebody can help me? 有人可以帮助我吗? Thanks. 谢谢。

Your Bill form should have a zero argument constructor. 您的Bill表单应具有零参数构造函数。 I suggest the following 我建议以下

protected Bill()
{
    InitializeComponent();
}

public Bill(string code, string name, string email): this()
{
    grid.Rows.Add();
    grid.Rows[0].Cells[0].Value = code;
    grid.Rows[0].Cells[1].Value = name;
    grid.Rows[0].Cells[2].Value = email;

}

Or you can just keep a zero argument constructor and use object initialisation instead for creating Bill 或者,您可以只保留零参数构造函数,而使用对象初始化代替创建Bill。

Bill bill = new Bill 
{ 
    code = grid.SelectedRows[0].Cells[0].Value.ToString(), 
    name = grid.SelectedRows[0].Cells[1].Value.ToString(),
    email = grid.SelectedRows[0].Cells[8].Value.ToString()
};

It doesn't look like you are using the framework quite right, but in your case, couldn't you just set the properties of Bill after you constructed it, like so: 看起来您使用的框架不太正确,但是在您的情况下,构造后不能直接设置Bill的属性,就像这样:

public Bill()
{
    InitializeComponent();
}

public void init(string code, string name, string email)
{
    grid.Rows.Add();
    grid.Rows[0].Cells[0].Value = code;
    grid.Rows[0].Cells[1].Value = name;
    grid.Rows[0].Cells[2].Value = email;
}

In SearchClient: 在SearchClient中:

private Bill bill;
public void setBill(Bill bill)
{
    this.bill = bill;
}

And then in Menu: 然后在菜单中:

private Bill bill;
private void button2_Click(object sender, EventArgs e)
{  
    this.bill = new Bill();
    bill.Show();
}
private void button1_Click(object sender, EventArgs e)
{ 
    SearchClient searchcli = new SearchClient();
    searchcli.setBill(this.bill);
    searchcli.ShowDialog();
}

Again, in SearchClient: 同样,在SearchClient中:

... your button click function ...
{
     this.bill.init(grid.SelectedRows[0].Cells[0].Value.ToString(), 
        grid.SelectedRows[0].Cells[1].Value.ToString(),
        grid.SelectedRows[0].Cells[8].Value.ToString())
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM