【问题标题】:pass dictionary to new form c#将字典传递给新表单c#
【发布时间】:2016-03-26 20:22:01
【问题描述】:

在我的主表单中,我创建了一个新表单:

private void buttonCreatePositionForm_Click(object sender, EventArgs e)
{ 
 f3 = new FormPosition(textBoxFiltermachineid.Text, textBoxFilterPositionFile.Text);
 f3.Show();
}

我还使用了一个使用主表单填充的字典:

Dictionary<String, List<Reorder>> reorderPerFilename = new Dictionary<string, List<Reorder>>();

class Reorder
        {
            public int reordernumber { get; set; }
            public string reordervalue { get; set; }
            public int reorder0 { get; set; }
            public int reorder1 { get; set; }
            public int reorder2 { get; set; }
            public int reorder3 { get; set; }
            public int reorder4 { get; set; }
            public int reorder5 { get; set; }
            public int reorder6 { get; set; }
        }

现在我想将此字典及其内容传递给新表单,以便可以使用这些值:

        public FormPosition(string machineId, string reorderCondition)
        {
            InitializeComponent();

            if (!string.IsNullOrWhiteSpace(machineId) && !string.IsNullOrWhiteSpace(reorderCondition))
            {
                labelMachineId.Text = machineId;
                labelReorderCondition.Text = reorderCondition;     
            }  
        }

我不断收到错误

错误 4 参数 1:无法从 'System.Collections.Generic.Dictionary>' 转换为 'System.Collections.Generic.Dictionary>'

尝试这样做时:

private void buttonCreatePositionForm_Click(object sender, EventArgs e)
        {
            f3 = new FormPosition(reorderPerFilename, textBoxFiltermachineid.Text, textBoxFilterPositionFile.Text);
            f3.Show();
        }

class Reorder2
        {
            public int reordernumber { get; set; }
            public string reordervalue { get; set; }
            public int reorder0 { get; set; }
            public int reorder1 { get; set; }
            public int reorder2 { get; set; }
            public int reorder3 { get; set; }
            public int reorder4 { get; set; }
            public int reorder5 { get; set; }
            public int reorder6 { get; set; }
        }

        public FormPosition(Dictionary<String, List<Reorder2>> reorderPerFilename, string machineId, string reorderCondition)
        {
            InitializeComponent();

            if (!string.IsNullOrWhiteSpace(machineId) && !string.IsNullOrWhiteSpace(reorderCondition))
            {
                labelMachineId.Text = machineId;
                labelReorderCondition.Text = reorderCondition;     
            }  
        }

我在这里做错了什么?

【问题讨论】:

  • 我可能不应该在创建新表单时传递字典。但话又说回来,如何从新创建的表单中访问 form1 中字典中的数据。换句话说,我仍然希望从其他类中读取填充的字典。

标签: class dictionary newforms


【解决方案1】:

您可以将属性添加到人员类。但它应该是静态属性。

无需创建实例即可访问静态属性。

它们在您的应用程序启动时创建,它们一直存在到您的应用程序关闭

【讨论】:

    猜你喜欢
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    相关资源
    最近更新 更多