2015-04-06 39 views
0

我有兩種不同的形式,一種是我生成的客戶列表,另一種是我需要檢索添加到列表中的信息。我怎麼能通過我的第二種形式的名單?如何將一個表單中生成的列表傳遞給另一個表單(C#)?

這裏的第一個形式

List<Customers> new_customer = new List<Customers>(); 

    private void newCustomer_Load(object sender, EventArgs e) 
    { 

    } 

    private void fNameTxtBox_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void lNameTxtBox_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void addressTxtBox_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void phoneNumTxtBox_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void emailTxtBox_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void IDTxtBox_TextChanged(object sender, EventArgs e) 
    { 

    } 


    private void addNewCustButton_Click(object sender, EventArgs e) 
    { 

     if (fNameTxtBox.Text != "" && lNameTxtBox.Text != "" && addressTxtBox.Text != "" && phoneNumTxtBox.Text != "" && emailTxtBox.Text != "" && IDTxtBox.Text != "") 
     { 

      new_customer.Add(new Customers { FName = fNameTxtBox.Text, LName = lNameTxtBox.Text, Address = addressTxtBox.Text, phoneNum = phoneNumTxtBox.Text, emailAdd = emailTxtBox.Text, ID = int.Parse(IDTxtBox.Text) }); 
      MessageBox.Show("Thanks for Registering"); 
     } 

     else 
     { 
      MessageBox.Show("Customer not added! Please fill out the entire form!"); 
     } 

    } 

} 

}

而這裏的第二種形式:

namespace WindowsFormsApplication1 
{ 
public partial class Current_Customers : Form 
{ 
    public Current_Customers() 
    { 
     InitializeComponent(); 

    } 

    private void currCustComboBox_SelectedIndexChanged(object sender, EventArgs e) 
    { 

    } 
    } 
} 
+0

使用此可以將值從一個窗體傳遞給另一個窗體http://www.codeproject.com/Questions/180489/How-to-copy-all-the-items-between-listboxes-in-two – 2015-04-06 04:44:10

回答

1

像這樣創建窗口2的新構造,也可以創建在第二形式的列表好。

public partial class Current_Customers : Form 
{ 
List<Customers> new_customer = new List<Customers>(); 
public Current_Customers(List<Customers> customers) 
{ 
new_customer=customers; 
} 
} 

而當你將在Form1創建這種形式的對象做到這一點

Current_Customers cus=new Current_Customers(new_customer); 

這將通過列表第二種形式。

+0

嗨Mairaj,謝謝你的迴應。我試圖實現這一點,但是當我需要在父窗體上單擊按鈕時創建此窗體的實例時,我需要將列表傳遞給它的構造函數(因爲我們在form2上引入了構造函數方法)。我嘗試了這個列表,並且表示該列表在該上下文中不可用。我該如何解決這個問題? – 2015-04-06 18:00:39

0

你有兩種可能的方式來做到這一點。

1)在兩個表單上設置公共字段/屬性。如果兩個表單都存在於相同的範圍內,則它們可以互相引用。

2)將列表添加到這兩個表單都可以訪問的第三個類,最好是靜態類。這將是我個人的偏好。

public static class StaticData 
{ 
    public static readonly List<Customers> _Customers = new List<Customers>(); 

    public static List<Customers> CustomerList 
    { 
     get 
     { 
      if (_Customers.Count < 1) 
      { 
        //Load Customer data 
      } 
      return _Customers; 
     } 
    } 
} 

public class Form1 
{ 
    private List<Customers> new_customer = null; 
    public Form1() 
    { 
     this.new_customer = StaticData.CustomerList; 
    } 
} 

public class Current_Customers 
{ 
    private List<Customers> new_customer = null; 

    public Current_Customers() 
    { 
     this.new_customer = StaticData.CustomerList; 
    } 
} 

雖然我在這裏的例子並不是真正的線程安全,並且只是爲了指出您正確的方向。

相關問題