1: using System;
2: using System.Windows.Forms;
3:
4: namespace MVPDemo
5: {
6: public partial class CustomerView : ViewBase, ICustomerView
7: {
8: public CustomerView()
9: {
10: InitializeComponent();
11: }
12:
13: protected override object CreatePresenter()
14: {
15: return new CustomerPresenter(this);
16: }
17:
18: #region ICustomerView Members
19:
20: public event EventHandler<CustomerEventArgs> CustomerSelected;
21:
22: public event EventHandler<CustomerEventArgs> CustomerSaving;
23:
24: public void ListAllCustomers(Customer[] customers)
25: {
26: this.dataGridViewCustomers.DataSource = customers;
27: }
28:
29: public void DisplayCustomerInfo(Customer customer)
30: {
31: this.buttonOK.Enabled = true;
32: this.textBoxId.Text = customer.Id;
33: this.textBox1stName.Text = customer.FirstName;
34: this.textBoxLastName.Text = customer.LastName;
35: this.textBoxAddress.Text = customer.Address;
36: }
37:
38: public void Clear()
39: {
40: this.buttonOK.Enabled = false;
41: this.textBox1stName.Text = string.Empty;
42: this.textBoxLastName.Text = string.Empty;
43: this.textBoxAddress.Text = string.Empty;
44: this.textBoxId.Text = string.Empty;
45: }
46:
47: #endregion
48:
49: protected virtual void OnCustomerSelected(string customerId)
50: {
51: var previousId = this.textBoxId.Text.Trim();
52: if (customerId == previousId)
53: {
54: return;
55: }
56: if(null != this.CustomerSelected)
57: {
58: this.CustomerSelected(this, new CustomerEventArgs{ CustomerId = customerId});
59: }
60: }
61:
62: protected virtual void OnCustomerSaving(Customer customer)
63: {
64: if(null != this.CustomerSaving)
65: {
66: this.CustomerSaving(this, new CustomerEventArgs{ Customer = customer});
67: }
68: }
69:
70: private void dataGridViewCustomers_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
71: {
72: var currentRow = this.dataGridViewCustomers.Rows[e.RowIndex];
73: var customerId = currentRow.Cells[0].Value.ToString();
74: this.OnCustomerSelected(customerId);
75: }
76:
77: private void buttonOK_Click(object sender, EventArgs e)
78: {
79: var customer = new Customer();
80: customer.Id = this.textBoxId.Text.Trim();
81: customer.FirstName = this.textBox1stName.Text.Trim();
82: customer.LastName = this.textBoxLastName.Text.Trim();
83: customer.Address = this.textBoxAddress.Text.Trim();
84: this.OnCustomerSaving(customer);
85: }
86: }
87: }