将客户添加到程序中

时间:2012-01-24 22:19:48

标签: c#

我希望我的程序知道已经添加了一个新客户我不知道如何在我的CustomerManager类和我的Form类之间建立链接,我已经完成了所需的代码,请看这里

表格1

if (customerframe.ShowDialog() == DialogResult.OK) //if button OK is clicked then value will be inserted
            {
                listviewitem = new ListViewItem("1");
                listviewitem.SubItems.Add(customerframe.firstName);
                listviewitem.SubItems.Add(customerframe.lastName);
                listviewitem.SubItems.Add(customerframe.zipcode);
                //there will be more here later

               this.listView1.Items.Add();

窗体2

        private void btnOk_Click(object sender, EventArgs e)
                {
                    MainForm main = new MainForm();
                    contact.FirstName = tbFirstName.Text;           
                    firstName = contact.FirstName;

                    contact.LastName = tbLastName.Text;            
                    lastName = contact.LastName;

//there will be more code here later as well

CustomerManager类

class CustomerManager
    {
        private Customer CustomerIN;
        private List<Customer> customers = new List<Customer>();
        private int nrOfCustomers;
        private int id = 100;

        public CustomerManager()
        {
          throw new System.NotImplementedException();
        }

        public int Count
        {
            get
            {
              return nrOfCustomers;
            }
            set
            {
                foreach (Customer customer in customers)
                    nrOfCustomers++;
            }
        }

        public int GetNewID
        {
            get
            {
                return id;
            }
            set
            {
            }
        }

        public bool AddCustomer()
        {
                if (customers != null)
                {
                     foreach (Customer customer in customers)
                     {
                    customers.Add(customer);
                    CustomerIN.ID = id++;
                     }
                    return true;
                }
                else return false;
            }

        public bool ChangeCustomer()
        {
            throw new System.NotImplementedException();
        }

        public bool DeleteCustomer()
        {
            if (customers != null)
            {
                foreach (Customer customer in customers)
                {
                    customers.Remove(customer);
                }
                return true;
            }
            else return false;
        }

        public Customer GetCustomer()
        {
            throw new System.NotImplementedException();
        }

        public void TestValues()
        {
            throw new System.NotImplementedException();
        }
    }
}

我希望上面的课程在我的程序中与我的表单进行交互,因为我认为我的代码非常混乱。我该怎么办

1 个答案:

答案 0 :(得分:2)

您应该使用事件来告知smth对象状态的变化

class Form1
  : Form
{
  private CustomerManager manager;

  public Form1()
  {
    ...

    manager = new CustomerManager();
    manager.CustomerAdded += OnCustomerAdded;

    ...
  }

  private void OnCustomerAdded(object source, CustomerEventArgs eventArgs)
  {
    var listviewitem = new ListViewItem(eventArgs.Customer.FirstName);
    listviewitem.SubItems.Add(eventArgs.Customer.LastName);

    this.listView1.Items.Add(listviewitem);
  }
}

class CustomerEventArgs
  : EventArgs
{
  public readonly Customer Customer;

  public CustomerEventArgs(Customer customer)
  {
    Customer = customer;
  }
}

class Customer
{
  public string FirstName{get;set;}
  public string LastName{get;set;}
}

class CustomerManager
{
  private List<Customer> list = new List<Customer>();

  public event EventHandler<CustomerEventArgs> CustomerAdded;
  private void OnCustomerAdded(Customer customer)
  {
     var handler = CustomerAdded;
     if(handler != null)
     {
       handler(this, new CustomerEvenetArgs(customer));
     }
  }

  public event EventHandler<CustomerEventArgs> CustomerDeleted;
  private void OnCustomerDeleted(Customer customer)
  {
     var handler = CustomerDeleted;
     if(handler != null)
     {
       handler(this, new CustomerEvenetArgs(customer));
     }
  }

  public int Count
  {
     get
     {
       return list.Count;
     }
  }

  public bool AddCustomer(Customer customer)
  {
    if(list.Any(c => c.FirstName == customer.FirstName && c.LastName == customer.LastName))
    {
      return false;
    }

    list.Add(customer);
    OnCustomerAdded(customer);
    return true;
  }

  public bool DeleteCustomer(Customer customer)
  {
    var currentCustomer = list.FirstOrDefault(c => c.FirstName == customer.FirstName && c.LastName == customer.LastName);
    if(currentCustomer != null)
    {
       list.Remove(currentCustomer);
       OnCustomerDeleted(currentCustomer);
       return true;
    }

    return false;
  }
}