从组合框选择中填充文本框

时间:2012-04-19 20:57:26

标签: c#

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

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

1 个答案:

答案 0 :(得分: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;
    }
}