2015-09-02 46 views
0

我在form1上生成了一个列表,其内容是数字和一个K000-000形式的字母。然后我想打开form2。在表单2上,我有一个文本框,一个列表框和一个按钮。在文本框中,您将在类似12345中键入更多数字。单击按钮时,我希望它将Form1的列表的内容添加到末尾的“ - ”和您在Form2的文本框中键入的内容。所以列表框将是K000-000-12345。我不确定如何在Form2上正确使用Form1的列表并添加到它。在表单之间使用列表

Form1中

DesignNo.FindByItem(electype, (int.Parse)(dwgno)); 

      List<DesignNo> Electrode = DesignNo.FindByItem(electype, (int.Parse)(dwgno)); 

      if (Electrode.Count <= 0) 
      { 
       MessageBox.Show("Unknown Electrode Number"); 
      } 

      frmElectrode frmelec = new frmElectrode(); 
      frmelec.Show(); 

frmelec在例子是窗体2。在Form2的访问列表

+0

将一个构造函数添加到'Form2',它将一个'List '作为参数。当你创建你的'Form2'时,把'List '实例作为参数传递给那里。 'Form2'还需要一个属性来存储列表,通过'Form1'访问,或者通过引用传递它也应该工作。只需在表单之间搜索传递对象/值;这已被问无数次。 – sab669

+1

[Duplicate 1](http://stackoverflow.com/questions/3062575/)[duplicate 2](http://stackoverflow.com/questions/7800731/)[duplicate 3](http://stackoverflow.com/问题/ 17032484 /)[duplicate 4](http://stackoverflow.com/questions/17836398/)[duplicate 5](http://stackoverflow.com/questions/25316230/)[duplicate 6](http:// stackoverflow.com/questions/29092707/)... –

回答

1

1 - 使用静态属性

public static List<int> list; 


    public Form1() 
    { 
     list=new List<int>(); 
     InitializeComponent(); 
    } 

这样

Form1.list.Add(item); 

2,使用构造

public static List<int> list; 
    public Form1() 
    { 
     list=new List<int>(); 
     InitializeComponent(); 
    } 
    public void ShowForm2() 
    { 
    var form2=new Form2(List); 
    form2.show(); 
    } 
在窗体2

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

    public static List<int> _list; 
    public Form2(List<int> list) 
    { 
     _list=list; 
     InitializeComponent(); 
    } 
    } 
+0

我认为你所有的方法都会在错误的方向上产生依赖。没有任何表格应该知道主表格。更好的方式是在Form 2关闭后添加到列表中的公共属性。 – Console

1

创建在内部形成公共财产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(); 
    } 
} 

消耗Form1的公共财产的Form2被关闭

public Form1() 
    { 
     InitializeComponent(); 

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

这样的依赖是朝着正确的方向后,窗口2只是一种输入形式可以在没有任何依赖的情况下重用。

0

一个干净的方式来分享一些状态(在钥匙你的情况字典)是通过一些共享服务(单身),所以:

你可以创建一个类(如ElectrodeManager)将举行的电极字典(首先是空的)。

在Form1你将填充该字典通过指定的方法对类(例如AddElectrode(string electrodeType, string electrodeKey) - >这将增加新项到字典) - 所以你将不得不Dictionary<string, string>保持例如{“T1”,“K000-000”},{“T2”,“K000-0001”} ...

在Form2中,您将从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 
} 

内Form1上(在生成逻辑的地方)使用方法:

ElectrodeManager.Instance.AddElectrode("T1", "K000-000"); 

ElectrodeManager.Instance.AddElectrode("T2", "K000-001"); 

内部窗体2(点击按钮):

ElectrodeManager.Instance.AppendStringToElectrodeKey("T1", textBox.Text); 

ElectrodeManager.Instance.AppendStringToElectrodeKey("T2", textBox.Text); 

当然,你可以很容易地如果更适合您,请将数据类型切换到List<string>

+0

阅读更多关于为什么单身人士是如此糟糕这里http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons – Console

+0

我同意这种实现不太好,可能应该避免。但是,如果我在IoC/DI中创建了一个在单例作用域中创建对象并使用这两种形式的构造器注入,它将是不必要的开销。此外,我已经通过单例完成了,因为我想集中键列表(存储),而不是两个单独的列表。 –