Best practice for transferring information between windows.forms

What do you do to transfer information between forms? Go straight ahead (sorry) using the properties or parameters in the New () or DoStuff () method, but what about sending back when the user completed the second form? (IE identifier of the selected item) We used all of this:

  • Passed the calling form to the called form as ref so that the called form can access properties or methods in the calling form. I really do not like it, because the two forms are very dependent on each other. Passing the calling form as an object only improves this a bit.
  • Use events . This separates the code somewhat, but the signatures must match the event handler.
  • Use an open interface . I'm talking about .NET built into one, but I suppose you could create your own. It seems to me the best for me.

Now lift the panel, what if the forms are in two different DLLs? As long as the forms do not depend on each other, I would think that this would not be a big step.

+4
source share
6 answers

Create public properties for the form, then wait for the form to close and check the properties before deleting the new form.

NewForm myForm = new NewForm(); myForm.ShowDialog(); string x = myform.MyProperty; 
+3
source

I found that when you have a well-designed Object Model of an object object or just business objects. These tasks are much easier.

If you do not have domain objects such as Employee, Account, Location, etc., you find yourself writing forms with a bunch of properties and creating tons of uncomfortable dependencies. Over time, this can be very dirty.

Once you have a domain entity, it is much easier to deal with you. For example, to change your Employee using a form, you can simply create an Employee property as follows:

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

As far as events are concerned, for Winform / WPF applications, it is almost useful for creating a global EventManager using a publish / subscribe template to handle the connection between forms. Very rarely, I will ever have one form of "speak" directly in any other form. This is another topic, so I will not go into details if you need examples that I can provide to several that I have made.

Rifford Brookshire

+3
source

Do you want to access the user interface elements of the second form? I think a cleaner way is to use a shared object to pass data back to the calling form. Pass the object as a parameter to the second constructor of the form, which can fill in the instance fields and return this instance to the calling form. This object can also raise any events (for example, property change events) to notify the calling form (or subscribers), if necessary.

0
source

If we are talking about the main form, creating an instance of another form, waiting for the form to do some work, and then close and check its result, then the presence of common properties or listening to events makes the most sense. None of these things will be affected by the presence of forms in different assemblies. You get an explicit binding contract between the two forms, but if the properties were described (as you propose) in an open interface, then as long as everyone agrees with the conditions of the interface, you are fine.

I'm not sure how much harder it is for you to get it. I used to use a static singleton object to store application state. The app-state object will expose event handlers that can listen to other parts of the program. The main form would create a state application (just get a link to it) and listen to certain events on it. Then the main form would create child forms and controls to do the job. Children will change the properties of the app-state object, which in turn will trigger fire events that the primary form will listen to. Thus, the various forms and controls were separated from each other. The disadvantage is that they were closely related to the state app's singlet.

0
source

One thing that I was successful was to create an easy system for registering publications / signatures in the application. This was in .net 1.1 and was not sure how this would change with generics. Essentially, we had a singleton that contained a hash table with a string key and multi-sheeted delegates.

In singleton there were methods such as RegisterForEvent (string key, delegate handler), RaiseEvent (key, data), etc.

Then we defined the standard delegate and said that all users should implement this template, for example, our handlers should have been: void method (object sender, CustomEventArgs args). Publishers would define their own derived class CustomEventArgs.

It's nice that this allowed to completely isolate the system. We had many builds and I had no problems, just need to make sure your eventargs are defined where other submasters can be accessed.

We had what we called different subsystems, for example, we had one that controlled the Internet connection, and when he raised the event, the user interface changed to indicate the status of their connection, we also had a queue service that sent messages to the server, when he saw that the connection was disconnected, we stopped publishing.

The disadvantage is that it is very closely associated with at least our implementation, but there are ways to improve this.

0
source

this.Hide (); string [] name = new string []; new frmFormName = new frm (string what, string you, string going, string to, string put, stiring in); this.ShowDialog (); this.Show ();

0
source

Source: https://habr.com/ru/post/1277573/


All Articles