1

在列表框中更改所选索引后,我似乎无法弄清楚如何在文本框中显示帐户 ID 和帐户余额。我有一个客户类和一个支票账户类(它是银行账户类的子类)。混乱的代码部分在底部。

这是我遇到问题的代码部分:

txtAccountID.Text = frmStart.GetCustomer()[lstTabPage1.SelectedIndex].GetCheckers()[lstFrmStartChecking.SelectedIndex].GetAcctNumber();
txtBalance.Text = frmStart.GetCustomer()[lstTabPage1.SelectedIndex].GetCheckers()[lstFrmStartChecking.SelectedIndex].GetBalance().ToString();

这是代码的其余部分:

public partial class frmStart : Form
{
    private static List<Customer_Account> customers;
    private Customer_Account aCustomer;

    private Saving_Account aSaver;
    private static List<Saving_Account> savers;
    private Checking_Account aChecker;
    private static List<Checking_Account> checkers;

    public frmStart()
    {
        InitializeComponent();
        customers = new List<Customer_Account>();
        checkers = new List<Checking_Account>();
        savers = new List<Saving_Account>();
    }

    #region New form for Savings and Checking
    private void lstFrmStartChecking_DoubleClick(object sender, EventArgs e)
    {
        //Shows Checking form
        frmChecking showCheckingForm = new frmChecking();
        this.Hide();
        showCheckingForm.ShowDialog();
        this.Close();
    }

    private void lstFrmStartSavings_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Show Savings form
        frmSavings showSavingForm = new frmSavings();
        this.Hide();
        showSavingForm.ShowDialog();
        this.Close();
    }
    #endregion

    #region Everything needed for TabPage1
    //Sets CheckChanged event handler for either New customer or Existing Customer
    private void rdoNewCustomer_CheckedChanged(object sender, EventArgs e)
    {
        groupBox2.Visible = true;
        groupBox3.Visible = false;

    }
    private void rdoExistingCustomer_CheckedChanged(object sender, EventArgs e)
    {
        groupBox3.Visible = true;
        groupBox2.Visible = false;
    }//End of CheckChanged event handler
    //Button controls for Adding customer to our bank and Clearing the textboxes 
    //in the 1st group panel
    private void btnAddCustomer_Click(object sender, EventArgs e)
    {
        AddCustomerAccountToList();
        PopulateListBox(customers);

    }
    private void AddCustomerAccountToList()
    {
        double socialSecurity;
        double phoneNumber;
        double zipCode;


        if (double.TryParse(txtTab1SocialSecurity.Text, out socialSecurity) && double.TryParse(txtTab1PhoneNumber.Text, out phoneNumber)
            && double.TryParse(txtTab1Zip.Text, out zipCode))
        {
            aCustomer = new Customer_Account(txtTab1SocialSecurity.Text, txtTab1Name.Text, txtTab1Address.Text, txtTab1City.Text,
                txtTab1State.Text, txtTab1Zip.Text, txtTab1PhoneNumber.Text, txtTab1Email.Text);
            customers.Add(aCustomer);
        }
        else
        {
            MessageBox.Show("Please be sure to use only numeric entries for: \nSocial Security \nPhone Number \nZip Code", "Non-numeric Entry");
        }

    }//End of AddCustomerAccount

    private void btnTab1Clear_Click(object sender, EventArgs e)
    {
        foreach (Control ctrl in groupBox2.Controls)
        {
            if (ctrl is TextBox)
            {
                ((TextBox)ctrl).Clear();
            }
            txtTab1SocialSecurity.Focus();
        }
    }//end of button controls for 1st group panel
    //Add CheckingAccount to List()

    //Populate ListBox for List<>
    private void PopulateListBox(List<Customer_Account> aListCustomerAccount)
    {
        lstTabPage1.Items.Clear();
        lstTabPage2Checking.Items.Clear();
        foreach (Customer_Account customer in aListCustomerAccount)
        {
            lstTabPage1.Items.Add(customer.GetCustomerName().ToUpper());
            lstTabPage2Checking.Items.Add(customer.GetCustomerName().ToUpper());
        }
    }//End of Populate listbox
    //Search for an existing member with name
    private void txtTabPage1Search_TextChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < lstTabPage1.Items.Count; i++)
        {
            if (string.IsNullOrEmpty(txtTabPage1Search.Text))
            {
                lstTabPage1.SetSelected(i, false);
            }
            else if (lstTabPage1.GetItemText(lstTabPage1.Items[i]).StartsWith(txtTabPage1Search.Text.ToUpper()))
            {
                lstTabPage1.SetSelected(i, true);
            }
            else
            {
                lstTabPage1.SetSelected(i, false);
            }
        }
    }//End of search
    //This button will open a checking account for the customer
    private void btnOpenCheckingAccount_Click(object sender, EventArgs e)
    {
        string acctID;
        acctID = txtAccountID.Text;
        aChecker = new Checking_Account(acctID, DateTime.Today, 0, 
            200, frmStart.GetCustomer()[lstTabPage1.SelectedIndex]);

    }
    //This button will open a saving account for the customer
     private void btnOpenSavingsAccount_Click(object sender, EventArgs e)
    {
         aSaver = new Saving_Account(txtAccountID.Text, DateTime.Today, 0, 0.05, 
            frmStart.GetCustomer()[lstTabPage1.SelectedIndex]);
    }

    private static List<Customer_Account> GetCustomer()
    {
        return customers;
    }    
    #endregion

    #region Everything Needed for TabPage2
    //Search TabPage 2 Checkers
    private void txtTabPage2SearchChecking_TextChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < lstTabPage2Checking.Items.Count; i++)
        {
            if (string.IsNullOrEmpty(txtTabPage2SearchChecking.Text))
            {
                lstTabPage2Checking.SetSelected(i, false);
            }
            else if (lstTabPage1.GetItemText(lstTabPage2Checking.Items[i]).StartsWith(txtTabPage2SearchChecking.Text.ToUpper()))
            {
                lstTabPage2Checking.SetSelected(i, true);
            }
            else
            {
                lstTabPage2Checking.SetSelected(i, false);
            }
        }
    }//End Search TabPage2 Checkers

    //Display values in textboxes depending on user selection in listbox
    private void lstTabPage2Checking_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            txtTab2SocialSecurity.Text = customers[lstTabPage2Checking.SelectedIndex].GetSocialSecurity().ToString();
            txtTab2Name.Text = customers[lstTabPage2Checking.SelectedIndex].GetCustomerName().ToString();
            txtTab2City.Text = customers[lstTabPage2Checking.SelectedIndex].GetCity().ToString();
            txtTab2State.Text = customers[lstTabPage2Checking.SelectedIndex].GetState().ToString();
            txtTab2Zip.Text = customers[lstTabPage2Checking.SelectedIndex].GetZip().ToString();
            txtTab2PhoneNumber.Text = customers[lstTabPage2Checking.SelectedIndex].GetPhoneNumber().ToString();
            txtTab2Email.Text = customers[lstTabPage2Checking.SelectedIndex].GetEmail().ToString();
            txtTab2Address.Text = customers[lstTabPage2Checking.SelectedIndex].GetAddress().ToString();
            txtAccountID.Text = frmStart.GetCustomer()[lstTabPage1.SelectedIndex].GetCheckers()[lstFrmStartChecking.SelectedIndex].GetAcctNumber();
            txtBalance.Text = frmStart.GetCustomer()[lstTabPage1.SelectedIndex].GetCheckers()[lstFrmStartChecking.SelectedIndex].GetBalance().ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

    }
    #endregion

}

(这是错误消息所说的) System.ArgumentOutOfRangeException:索引超出范围。必须是非负数且小于集合的大小。

4

1 回答 1

0

欢迎来到 SO,盖伊!

在我看来,您正在将该.ToString()方法附加到除此之外的所有控件:

txtAccountID.Text = frmStart.GetCustomer()[lstTabPage1.SelectedIndex].GetCheckers()[lstFrmStartCheck‌​ing.SelectedIndex].GetAcctNumber();

将您的代码更改为:

txtAccountID.Text = frmStart.GetCustomer()[lstTabPage1.SelectedIndex].GetCheckers()[lstFrmStartCheck‌​ing.SelectedIndex].GetAcctNumber().ToString();

你应该没事!

于 2013-04-22T19:42:12.163 回答