2014-09-20 92 views
0

所以我在使用C#表單時遇到了一些麻煩。我創建了一個客戶名單,其中存儲了名稱,郊區和銀行賬戶餘額。使用下一個和上一個按鈕我需要能夠讓用戶瀏覽列表中的當前5個對象。我的計劃是這樣做的,當用戶點擊按鈕時,文本框將填充相關信息。客戶和其他細節是按鈕的原因是後來我需要能夠更新存儲在這些字段中的信息,所以我認爲這樣做的好方法是擦除文本框中已有的內容,輸入新的信息,然後按按鈕進行更新。使用下一個和上一個按鈕瀏覽列表

不管怎麼說,我的主要問題是,我需要用我的視圖按鈕在我的列表中移動

這是我的形式看起來像:

enter image description here

我目前的形式代碼是這樣的:

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

請幫助我,讓我知道什麼是最好的方式去做這件事。

+1

數據綁定是一種方式。 – terrybozzio 2014-09-20 15:50:30

回答

3

您需要做的第一件事是讓您的客戶列表具有課程級別的可見性,以便您可以在按鈕點擊事件中訪問它。

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; 

     ... 
    } 
} 
+1

這工作得很好!非常感謝! – 2014-09-20 16:36:11

+1

不客氣。 – 2014-09-20 16:36:37

3

使用數據綁定,如果你需要更改一些字段的內容,你可以使用該按鈕來更新或簡單地設置DataSourceUpdateMode像我一樣在這裏OnPropertyChange和每次更改文本中的一個文本框它將更新數據源(在這種情況下是List)。您可以分別定義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; 
} 
相關問題