-2

此代码的新 Form1() 部分(此代码在我的 Main.cs 中)不断给我这个错误 - HomeInventory2.Form1 不包含采用 0 个参数的构造函数。

private void cDsToolStripMenuItem_Click(object sender, EventArgs e)
{
    var form = new Form1();
    // show the form
    form.Show();
}

Form1代码如下

namespace HomeInventory2
{
    public partial class Form1 : Form
    {

        public Form1(IEnumerable<string> prepopulated)
        {
            InitializeComponent();
            IEnumerable<String> lines = prepopulated;
            textBoxAmount.Text = lines.ElementAtOrDefault(0);
            textBoxCategories.Text = lines.ElementAtOrDefault(1);
            textBoxProperties.Text = lines.ElementAtOrDefault(2);
            textBoxValue.Text = lines.ElementAtOrDefault(3);
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            CreateInventory create = new CreateInventory();
            create.ItemAmount = textBoxAmount.Text;
            create.ItemCategory = textBoxCategories.Text;
            create.ItemProperties = textBoxValue.Text;
            create.ItemValue = textBoxValue.Text;

            InventoryMngr invtryMngr = new InventoryMngr();
            invtryMngr.Create(create);

        }
    }
}

我尝试在其中放置一个空白构造函数 - 但是当然当调用该按钮或菜单项时,它只会显示一个空白屏幕。

4

5 回答 5

4

.net 中的所有类都有不带任何参数的默认构造函数。当您实现自己的构造函数时,框架不会为您的类生成空构造函数。同样的事情也发生在这里。在您的cDsToolStripMenuItem_Click方法中,您需要传递IEnumerable<string>参数。

于 2012-10-19T22:04:50.917 回答
2

尝试添加一个不接受这样的参数的构造函数

namespace HomeInventory2
{
    public partial class Form1 : Form
    {

        public Form1(IEnumerable<string> prepopulated)
        {
            InitializeComponent();
            IEnumerable<String> lines = prepopulated;
            textBoxAmount.Text = lines.ElementAtOrDefault(0);
            textBoxCategories.Text = lines.ElementAtOrDefault(1);
            textBoxProperties.Text = lines.ElementAtOrDefault(2);
            textBoxValue.Text = lines.ElementAtOrDefault(3);
        }

        public Form1()
        {
            InitializeComponent();
            IEnumerable<String> lines = null;
            textBoxAmount.Text = lines.ElementAtOrDefault(0);
            textBoxCategories.Text = lines.ElementAtOrDefault(1);
            textBoxProperties.Text = lines.ElementAtOrDefault(2);
            textBoxValue.Text = lines.ElementAtOrDefault(3);
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            CreateInventory create = new CreateInventory();
            create.ItemAmount = textBoxAmount.Text;
            create.ItemCategory = textBoxCategories.Text;
            create.ItemProperties = textBoxValue.Text;
            create.ItemValue = textBoxValue.Text;

            InventoryMngr invtryMngr = new InventoryMngr();
            invtryMngr.Create(create);

        }
    }
}
于 2012-10-19T22:04:34.677 回答
1

查看 Form1 的构造函数:

public Form1(IEnumerable<string> prepopulated)

它要求IEnumerable<string>您在实例化表单时必须提供一个类型的值。像这样

IEnumerable<string> someValue;
.
.
// Actually assign a value to someValue
.
.
var form = new Form1(someValue);

您还可以添加无参数构造函数。

public Form1()
        {
            InitializeComponent();            
        }

调用InitializeComponent()基本上用于使用所有控件和处理程序填充您的表单。

于 2012-10-19T22:04:12.623 回答
0

您应该将代码更改为提供参数:

private void cDsToolStripMenuItem_Click(object sender, EventArgs e)
{
    string[] prepopulated= ...;
    var form = new Form1(prepopulated);
    // show the form
    form.Show();
}

要么创建一个没有参数的构造函数。

public Form1()
{
    InitializeComponent();
}
于 2012-10-19T22:04:17.080 回答
0

您必须提供IEnumerable<string> prepopulated输入:

make new Form1(null) or new From1(new List{"a","b"})

于 2012-10-19T22:04:22.510 回答