Question

I can't seem to figure out how to display Account ID and Account Balance into a textbox after the selected index is changed in the listbox. I have a Customer class and a Checking account class(which is a subclass of a Bank Account class). The part of the code that is messing up is at the bottom.

This is the part of my code that I'm having trouble with:

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

This is the rest of the code:

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

}

(This is what the error message says) System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

Was it helpful?

Solution

Welcome to SO, Guy!

It looks to me that you are appending the .ToString() method to all of your controls except for this one:

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

Change your code to:

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

And you should be fine!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top