2015-09-02 43 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中創建了一個在單例作用域中創建對象並使用這兩種形式的構造器注入,它將是不必要的開銷。此外,我已經通過單例完成了,因爲我想集中鍵列表(存儲),而不是兩個單獨的列表。 –