使用下一个和上一个按钮浏览列表 [英] Using next and previous buttons to navgiate through a list

查看:125
本文介绍了使用下一个和上一个按钮浏览列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在使用C#表单时遇到了一些麻烦.我创建了一个客户列表,其中存储了那里的名称,郊区和银行帐户余额.使用下一个和上一个按钮,我需要能够允许用户浏览列表中的当前5个对象.我打算如何做的是,当用户单击任一按钮时,文本框将填充相关信息.客户和其他详细信息是按钮的原因是以后我需要能够更新存储在这些字段中的信息,所以我认为这样做的一个好方法是删除文本框中已经存在的内容,键入新的信息,然后按按钮进行更新.

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

这是我的表单的样子:

我当前的表单代码是:

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) { }
}

我的Customer类是:

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.

推荐答案

您需要做的第一件事是使您的客户列表具有类级别的可见性,以便您可以在按钮单击事件中访问它.

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;

        ...
    }
}

这篇关于使用下一个和上一个按钮浏览列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆