I am struggling to determine the best way to update one form based on an event that occurs in another.
For example, my application has a form that displays a list of parts (i.e. inventory, non-inventory, etc...) in a ListView
. My application also has a form that allows the user to add a new part. The application is not a MDI
.
What are my best options for updating the form that displays the list of parts every time an item is inserted, updated, or deleted from the other form?
I realize that I could pass a reference into the second form in cases where the form is instantiated from the first but, I'd prefer not to tightly couple the forms. I have experimented with custom events but, I think that is going to require implementation of a class in the middle that takes over instantiation of the forms so that it has access to both objects.
I guess I could use .NET built in properties. What are my best options?
If we're just talking about two forms I would go the simple route (e.g. Don't use a factory class).
in your receiver form add an event (like ProductListChanged
)
add a global variable (via static class property (see below)) to hold the receiver form.
public class globals {
public static ProductForm productForm {get;set;}
}
Then you can raise the ProductListChanged event via the global static property.
来源:https://stackoverflow.com/questions/14470150/updating-one-form-after-event-occurs-in-another-form