C# 从组合框选择填充文本框

C# 从组合框选择填充文本框,c#,C#,我正在写一个有两个标签的程序。在第一个选项卡上,用户输入有关客户帐户的信息。在第二个选项卡上有一个包含帐户名称的组合框,选中后,存储在第一个选项卡上输入的相同信息应使用第二个选项卡上的相同信息填充文本框。我以前做过这个,我使用相同的结构,但它不工作。我还从一个相关的类中提取了这些信息,但看起来一切都是正确的。谁能告诉我怎么了 public partial class Form1 : Form { ArrayList account; public Form1() {

我正在写一个有两个标签的程序。在第一个选项卡上,用户输入有关客户帐户的信息。在第二个选项卡上有一个包含帐户名称的组合框,选中后,存储在第一个选项卡上输入的相同信息应使用第二个选项卡上的相同信息填充文本框。我以前做过这个,我使用相同的结构,但它不工作。我还从一个相关的类中提取了这些信息,但看起来一切都是正确的。谁能告诉我怎么了

 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;
    }

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;
    }
}

帐户
指的是
数组列表
。看来你少了一个演员。或者更好的方法是,将您的
arraylist
更改为
列表
。如果您不受.Net版本的限制,请尝试用更现代的通用集合替换arraylist。这是一本好书