Search code examples
c#winformslistbuttonusing

Using next and previous buttons to navgiate through a list


So I am having some trouble with my C#forms. I have created a list of customers in which they have a there name, suburb, and bank account balances stored. Using a next and previous buttons I need to be able to allow the user to navigate through the current 5 objects in the list. How I was planning on doing so was when the user would click either the button, the text boxes would be filled with the relevant information. The reason why customer and other details are buttons is that later i need to be able to update the information stored in those fields, so i thought a good way to do that would be to erase what was already in the text box, type the new information, then press the button to update.

Anyways, my main issue is i need to use my view buttons to move through my list

This is how my form looks like:

enter image description here

My current form code is this:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        Customer c1 = new Customer("Sibel Yilmaz", "Wollongong", 2500, 3000, 5000);
        Customer c2 = new Customer("John Doe", "Figtree", 2547, 2500, 3655);
        Customer c3 = new Customer("Mariah Moore", "Coniston", 2500, 7000, 36000);
        Customer c4 = new Customer("Jessica Blackshaw", "Bellambi", 3500, 6000, 4750);
        Customer c5 = new Customer("Suzan Yilmaz", "Wollongong", 2500, 2000, 47110);

        List<Customer> customers = new List<Customer>();

        customers.Add(c1);
        customers.Add(c2);
        customers.Add(c3);
        customers.Add(c4);
        customers.Add(c5);
    }

    private void Form1_Load(object sender, EventArgs e) { }
    private void button2_Click(object sender, EventArgs e) { }
    private void button6_Click(object sender, EventArgs e) { }
    private void textBox4_TextChanged(object sender, EventArgs e) { }
    private void Customer_Click(object sender, EventArgs e) { }
}

And my Customer class is:

public class Customer
{
    protected string name;
    protected string suburb;
    protected int postcode;
    protected double credit_balance;
    protected double saving_balance;

    public Customer(string name, string suburb, int postcode, double credit_balance,
                    double saving_balance)
    {
        this.name = name;
        this.suburb = suburb;
        this.postcode = postcode;
        this.credit_balance = credit_balance;
        this.saving_balance = saving_balance;
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public string Suburb
    {
        get { return suburb; }
        set { suburb = value; }
    }

    public int Postcode
    {
        get { return postcode; }
        set { postcode = value; }
    }

    public double Credit_Balance
    {
        get { return credit_balance; }
        set { credit_balance = value; }
    }

    public double Savinig_Balance
    {
        get { return saving_balance; }
        set { saving_balance = value; }
    }
}

Please help me out, and let me know what the best way to go about this would be.


Solution

  • The first thing that you need to do is make your customers list have class level visibility so that you can access it in your button click events.

    public partial class Form1 : Form
    {
        int index = 0;
        List<Customer> customers = new List<Customer>();
        public Form1()
        {
          ... The remainder of your Constructor code
    

    Once you do that you should be able to do something like this.

    private void next_Click(object sender, EventArgs e)
    {
        if (index < customers.Count - 1)
        {
            index += 1;
            textBox1.Text = customers[index].Name;
    
            ...
        }
    }
    
    private void previous_Click(object sender, EventArgs e)
    {
        if (index > 0)
        {
            index -= 1;
            textBox1.Text = customers[index].Name;
    
            ...
        }
    }