Best practice to pass information between windows.forms

后端 未结 6 391
误落风尘
误落风尘 2020-12-19 15:42

What do you do to pass information between forms? Forward is straight forward (sorry) using Properties or maybe parameters in a New() or DoStuff() method, but what about se

6条回答
  •  北海茫月
    2020-12-19 16:21

    I have found that once you have a well designed Domain Entity Object Model or simply business objects. These tasks become much easier.

    If you dont have Domain Entities such as Employee, Account, Location etc., you find yourself writing forms with a bunch of properties and create tons of awkward dependencies. Over time this can be very messy.

    Once you have the Domain Entity in place things are much easier to deal with. For example, to edit your Employee using a form you can simply create an Employee property like this:

    NewForm myForm = new NewForm();             
    myForm.Employee = employeeToEdit; // This can have state 
    myForm.ShowDialog(); 
    Employee editedEmployee= myform.Employee;
    
    EmployeeFacade.SaveEmployee(editedEmployee); // Or whatever
    

    Regarding Events, for Winform/WPF apps it it almost aways helpful to create a global EventManager using publish / subscribe pattern to handle communication between forms. It is very rare I will ever have one form 'talk' directly to any other form. That is another topic so I will not go into detail, if you want examples I can provide several I have done.

    Raiford Brookshire

提交回复
热议问题