2012-04-19 110 views
0

我正在編寫一個帶有兩個選項卡的程序。在第一個選項卡上,用戶輸入有關客戶帳戶的信息。在第二個選項卡上有一個組合框,用於保存帳戶的名稱,當選擇存儲的第一個選項卡上輸入的相同信息時,應在第二個選項卡上填充具有相同信息的文本框。我以前做過這個,我使用相同的結構,但它不工作。我也從相關課程中提取這些信息,但一切都看起來不錯。有人可以告訴我什麼是錯的。從組合框選擇填充文本框

public partial class Form1 : Form 
{ 
    ArrayList account; 

    public Form1() 
    { 
     InitializeComponent(); 
     account = new ArrayList(); 
    } 

    //here we set up our add customer button from the first tab 
    //when the information is filled in and the button is clicked 
    //the name on the account will be put in the combobox on the second tab 
    private void btnAddCustomer_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      CustomerAccount aCustomerAccount = new CustomerAccount(txtAccountNumber.Text, txtCustomerName.Text, 
      txtCustomerAddress.Text, txtPhoneNumber.Text); 
      account.Add(aCustomerAccount); 

      cboClients.Items.Add(aCustomerAccount.GetCustomerName()); 
      ClearText(); 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("Make sure every text box is filled in!", "Error", MessageBoxButtons.OK); 
     } 
    } 


    private void ClearText() 
    { 
     txtAccountNumber.Clear(); 
     txtCustomerName.Clear(); 
     txtCustomerAddress.Clear(); 
     txtPhoneNumber.Clear(); 
    } 

這是我遇到麻煩的地方。它說,沒有用於「了accountNumber」或任何其他

private void cboClients_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     txtAccountNumberTab2.Text = account[cboClients.SelectedIndex].accountNumber 
     txtCustomerNameTab2.Text = account[cboClients.SelectedIndex].customerName; 
     txtCustomerAddressTab2.Text=account[cboClients.SelectedIndex].customerAddress; 
     txtCustomerPhoneNumberTab2.Text=account[cboClients.SelectedIndex].customerPhoneNo; 
    } 
+0

'account'指'arraylist' 。看起來你錯過了演員陣容。或者甚至更好,將'arraylist'改爲'List '。 – 2012-04-19 21:07:07

+0

如果你不受你的.Net版本的限制,可以看看用更現代的泛型集合替換ArrayList。這是一個很好的閱讀http://en.csharp-online.net/CSharp_Generics_Recipes%E2%80%94Replacing_the_ArrayList_with_Its_Generic_Counterpart – 2012-04-19 21:12:38

回答

2

ArrayList中持有的對象沒有定義,你需要將其轉換爲CustomerAccount

private void cboClients_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    CustomerAccount custAccount = account[cboClients.SelectedIndex] as CustomerAccount; 
    if(custAccount != null) 
    { 
     txtAccountNumberTab2.Text = custAccount.accountNumber 
     txtCustomerNameTab2.Text = custAccount.customerName; 
     txtCustomerAddressTab2.Text=custAccount.customerAddress; 
     txtCustomerPhoneNumberTab2.Text=custAccount.customerPhoneNo; 
    } 
} 
+0

非常感謝! – 2012-04-19 23:32:58