Dynamically edit the data between two forms in WinForms c#

一曲冷凌霜 提交于 2020-06-29 04:22:54

问题


I have two forms. The main form is where the user select items, through buttons and they are added to a listview in the form. Then there is a button "Check out" which opens the second form, where the user can edit the created order. I used serialization to save the data so when get back to the previous form, the current state of the list shows up. But when I try to add new items, the previous are replaced. How to make a dynamic communication between the two forms without loosing any data?

This is what I have in the first form:

private void buttonCheckOut_Click(object sender, EventArgs e)
        {
            var binFormatter = new BinaryFormatter();
            using (var fileStream = new FileStream(@"D:\productsList.txt", 
                FileMode.Create, FileAccess.Write))
                binFormatter.Serialize(fileStream, productsList);
CheckOutForm checkOut = new CheckOutForm(TotalBill.Text, productsList, false);
            this.Hide();
            checkOut.ShowDialog();
}

And the deserialzation in the second form looks like this:

        Form1 f = new Form1(null);


        var binFormatter = new BinaryFormatter();
        using (var fileStream = new FileStream(@"D:\productsList.txt",
           FileMode.Open, FileAccess.Read))
        {
            productsList1 = (List<Product>)binFormatter.Deserialize(fileStream);
            foreach (var pr in productsList1)
            {
                    f.listView1.Items.Add(pr.ToString());
            }
        }

回答1:


What you're doing is overcomplicated. Have both of your forms reference a ProductsListService class which manages the products list (through appropriate methods).

public class ProductsListService
{
  List<Product> GetProducts();
  void AddProduct(Product product);
  ...
}

Your Main form class can create this service itself or it can be passed into it by whatever creates it. Then you can pass this service to your second form:

CheckOutForm checkOut = new CheckOutForm(TotalBill.Text, productsListService, false);
            this.Hide();

The checkoutForm will just use this service and doesn't need to pass anything back to the main form since they're both using the same object.




回答2:


The correct way to do this is to create a list of products that is shared between the two forms. The answer by @auburg covers the simple case when changes can only be done in one form at a time. This answer covers the case where the two forms should be synchronized in real-time.

In WPF this is built into the design. Put your items in a ObservableCollection that is shared between forms. If the products can change, ensure that they implement INotifyPropertyChanged and raise the event whenever something changes. This will ensure that any changes in one form is reflected in the other.

In windows forms this is a bit more cumbersome. One way to do it would be to create your own ObservableCollection that raises an event on changes and some method that updates the ListView or other UI collection whenever the list is updated.



来源:https://stackoverflow.com/questions/62280482/dynamically-edit-the-data-between-two-forms-in-winforms-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!