【问题标题】:Using next and previous buttons to navgiate through a list使用下一个和上一个按钮浏览列表
【发布时间】:2014-11-15 00:37:46
【问题描述】:

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

无论如何,我的主要问题是我需要使用查看按钮来浏览我的列表

这就是我的表单的样子:

我当前的表单代码是这样的:

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

请帮帮我,让我知道最好的方法是什么。

【问题讨论】:

  • 数据绑定是一种方式。

标签: c# winforms list button using


【解决方案1】:

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

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

一旦你这样做了,你应该能够做这样的事情。

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;

        ...
    }
}

【讨论】:

    【解决方案2】:

    使用 DataBinding ,如果您需要更改某些字段的内容,您可以使用按钮进行更新,或者像我在这里所做的那样简单地将 DataSourceUpdateMode 设置为 OnPropertyChange ,并且每次您更改其中一个文本框中的文本时,它都会更新数据源(在本例中为列表)。您可以单独定义 Binding 对象,然后将其添加到 textbox.DataBindings 以便您可以格式化和解析它,但这里我只是出于演示目的直接添加它们:

    List<Customer> customers;
    
    public Form1()
    {
         InitializeComponent();
    
         //you could add the Customers directly to the add method of the list.
         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);
    
         customers = new List<Customer>();
    
         customers.Add(c1);
         customers.Add(c2);
         customers.Add(c3);
         customers.Add(c4);
         customers.Add(c5);
    
         textBox1.DataBindings.Add("Text", customers, "Name",true,DataSourceUpdateMode.OnPropertyChanged);
         textBox2.DataBindings.Add("Text", customers, "Suburb", true, DataSourceUpdateMode.OnPropertyChanged);
         textBox3.DataBindings.Add("Text", customers, "Postcode", true, DataSourceUpdateMode.OnPropertyChanged);
         textBox4.DataBindings.Add("Text", customers, "Credit_Balance", true, DataSourceUpdateMode.OnPropertyChanged);
         textBox5.DataBindings.Add("Text", customers, "Savinig_Balance", true, DataSourceUpdateMode.OnPropertyChanged);
    }
    

    然后在你的上一个和下一个按钮中:

    private void PreviousBtn_Click(object sender, EventArgs e)
    {
        --this.BindingContext[this.customers].Position;
    }
    
    private void NextBtn_Click(object sender, EventArgs e)
    {
        ++this.BindingContext[this.customers].Position;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多