C# 在表单之间使用列表

C# 在表单之间使用列表,c#,winforms,list,C#,Winforms,List,我在form1上生成了一个列表,内容是数字和一个K000-000形式的字母。然后我想打开form2。在表格2中,我有一个文本框、一个列表框和一个按钮。在文本框中,您将键入更多的数字,如12345。当你点击这个按钮时,我想让它添加form1列表的内容,最后加一个“-”,以及你在Form2文本框中键入的内容。因此,列表框将为K000-000-12345。我不知道如何正确使用Form1在Form2上的列表,并将其添加到列表中 表格1 DesignNo.FindByItem(eletype,(int.P

我在form1上生成了一个列表,内容是数字和一个K000-000形式的字母。然后我想打开form2。在表格2中,我有一个文本框、一个列表框和一个按钮。在文本框中,您将键入更多的数字,如12345。当你点击这个按钮时,我想让它添加form1列表的内容,最后加一个“-”,以及你在Form2文本框中键入的内容。因此,列表框将为K000-000-12345。我不知道如何正确使用Form1在Form2上的列表,并将其添加到列表中

表格1

DesignNo.FindByItem(eletype,(int.Parse)(dwgno));
列表电极=设计编号FindByItem(eletype,(int.Parse)(dwgno));

如果(电极计数1-使用静态特性

  public static List<int> list;


    public Form1()
    {
        list=new List<int>();
        InitializeComponent();
    }
2-使用构造函数

    public static List<int> list;
    public Form1()
    {
        list=new List<int>();
        InitializeComponent();
    }
   public void ShowForm2()
    {
     var form2=new Form2(List);
     form2.show();
    }
公共静态列表;
公共表格1()
{
列表=新列表();
初始化组件();
}
公开表格2()
{
var form2=新的form2(列表);
表2.show();
}
表格2

    public partial class Form2 : Form
    {
    public Form2()
    {
     InitializeComponent();
    }

    public static List<int> _list;
    public Form2(List<int> list)
    {
        _list=list;
        InitializeComponent();
    }
    }
公共部分类表单2:表单
{
公共表格2()
{
初始化组件();
}
公共静态列表_列表;
公共表格2(列表)
{
_列表=列表;
初始化组件();
}
}

1-使用静态属性

  public static List<int> list;


    public Form1()
    {
        list=new List<int>();
        InitializeComponent();
    }
2-使用构造函数

    public static List<int> list;
    public Form1()
    {
        list=new List<int>();
        InitializeComponent();
    }
   public void ShowForm2()
    {
     var form2=new Form2(List);
     form2.show();
    }
公共静态列表;
公共表格1()
{
列表=新列表();
初始化组件();
}
公开表格2()
{
var form2=新的form2(列表);
表2.show();
}
表格2

    public partial class Form2 : Form
    {
    public Form2()
    {
     InitializeComponent();
    }

    public static List<int> _list;
    public Form2(List<int> list)
    {
        _list=list;
        InitializeComponent();
    }
    }
公共部分类表单2:表单
{
公共表格2()
{
初始化组件();
}
公共静态列表_列表;
公共表格2(列表)
{
_列表=列表;
初始化组件();
}
}

在表单2中创建公共属性

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public string Content { get; private set; }

    public void ButtonOkOnClick()
    {
        this.Content = this.textBox1.Text;
        this.Close();
    }
}
在Form2关闭后使用Form1中的公共属性

    public Form1()
    {
        InitializeComponent();

        Form2 form2 = new Form2();
        form2.Show();
        form2.Closed += (sender, args) => this.list.Add(form.Content);
    }

这样,依赖关系是正确的方向,form2只是一个输入表单,可以在没有任何依赖关系的情况下重用。

在form2内创建一个公共属性

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public string Content { get; private set; }

    public void ButtonOkOnClick()
    {
        this.Content = this.textBox1.Text;
        this.Close();
    }
}
在Form2关闭后使用Form1中的公共属性

    public Form1()
    {
        InitializeComponent();

        Form2 form2 = new Form2();
        form2.Show();
        form2.Closed += (sender, args) => this.list.Add(form.Content);
    }

这样,依赖关系是正确的方向,form2只是一个输入表单,可以在没有任何依赖关系的情况下重用。

共享某些状态(在您的密钥字典中)的干净方法是通过一些共享服务(singleton),因此:

您可以创建一个类(例如,
ElectrodeManager
),该类将保存电极字典(首先是空的)

在Form1中,您将通过该类上的指定方法填充该字典(例如,
AddElectrode(string electrodeType,string electrodeKey)
->,它将向字典中添加新项) -因此,您将拥有一个
字典
,其中包含例如{“T1”、“K000-000”}、{“T2”、“K000-0001”}

在表格2中,您将从
ElectrodeManager
使用该词典,并将文本框中的字符串附加到电极键上

例如:

public class ElectrodeManager
{
    #region Singleton Pattern

    private static ElectrodeManager instance;
    public static ElectrodeManager Instance
    {
        get
        {
            if (instance == null)
                instance = new ElectrodeManager();
            return instance;
        }
    }

    private ElectrodeManager()
    {
        electrodes = new Dictionary<string, string>();
    }

    #endregion

    #region Fields

    private Dictionary<string, string> electrodes;

    #endregion Fields

    #region Methods

    public void AddElectrode(string eType, string eKey)
    {
        if (!electrodes.ContainsKey(eType))
        {
            electrodes.Add(eType, eKey);
        }
    }

    public void AppendStringToElectrodeKey(string eType, string keyAddendum)
    {
        string electrodeKey = String.Empty;
        if (electrodes.TryGetValue(eType, out electrodeKey))
        {
            electrodes[eType] = String.Format("{0}-{1}", electrodes[eType], keyAddendum);
        }
    }

    public IDictionary<string, string> GetElectrodes()
    {
        return electrodes;
    }

    #endregion Methods
}
在Form2内部(单击按钮):


当然,如果对您更合适,您可以轻松地将数据类型切换到
列表。

共享某些状态(在您的密钥字典中)的干净方法是通过一些共享服务(单例),因此:

您可以创建一个类(例如,
ElectrodeManager
),该类将保存电极字典(首先是空的)

在Form1中,您将通过该类上的指定方法填充该字典(例如,
AddElectrode(string electrodeType,string electrodeKey)
->,它将向字典中添加新项) -因此,您将拥有一个
字典
,其中包含例如{“T1”、“K000-000”}、{“T2”、“K000-0001”}

在表格2中,您将从
ElectrodeManager
使用该词典,并将文本框中的字符串附加到电极键上

例如:

public class ElectrodeManager
{
    #region Singleton Pattern

    private static ElectrodeManager instance;
    public static ElectrodeManager Instance
    {
        get
        {
            if (instance == null)
                instance = new ElectrodeManager();
            return instance;
        }
    }

    private ElectrodeManager()
    {
        electrodes = new Dictionary<string, string>();
    }

    #endregion

    #region Fields

    private Dictionary<string, string> electrodes;

    #endregion Fields

    #region Methods

    public void AddElectrode(string eType, string eKey)
    {
        if (!electrodes.ContainsKey(eType))
        {
            electrodes.Add(eType, eKey);
        }
    }

    public void AppendStringToElectrodeKey(string eType, string keyAddendum)
    {
        string electrodeKey = String.Empty;
        if (electrodes.TryGetValue(eType, out electrodeKey))
        {
            electrodes[eType] = String.Format("{0}-{1}", electrodes[eType], keyAddendum);
        }
    }

    public IDictionary<string, string> GetElectrodes()
    {
        return electrodes;
    }

    #endregion Methods
}
在Form2内部(单击按钮):


当然,如果对您更合适的话,您可以轻松地将数据类型切换到
List

Form2
添加一个构造函数,该构造函数将
List
作为参数。当您创建
Form2
时,将
List
实例作为参数传入其中。
Form2
还需要一个属性来存储列表,可以通过
Form1
访问,或者通过引用传递也可以。只需在表单之间搜索传递对象/值;这已经被问了无数次了……向
Form2
添加一个构造函数,该构造函数以
列表
为参数。创建
Form2
时,传递
列表
Form2
还需要一个属性来存储列表,由
Form1
访问,或者通过引用传递也可以。只需搜索表单之间的传递对象/值;这已经被问了无数次了……我想你所有的方法都会在错误的方向。没有表单应该知道主表单。最好是表单2上的公共属性,在表单2关闭后添加到列表中。我认为您所有的方法都会创建一个指向错误方向的依赖项。没有表单应该知道主表单。最好是表单2上的公共属性,在表单2关闭后添加到列表中。阅读更多ab关于单例如此糟糕的原因,我同意这种实现不是很好,可能应该避免。但是,如果我在单例作用域中引入IoC/DI,并在两种形式中创建对象和构造函数注入,这将是不必要的开销。另外,我通过单例来实现,因为我希望有一个集中的密钥列表s(存储),而不是两个单独的列表。阅读更多关于为什么单例如此糟糕的信息,我同意这个实现不是很好,可能应该避免。但是,如果我在单例作用域中引入IoC/DI,在单例作用域中创建对象,在这两个作用域中都引入构造函数注入,这将是不必要的开销