During the form, complex data
Source code download, please click here
When designing form programs, there is often a need to transfer complex data between forms that call each other, and sometimes a child form modifies the content of the parent form. In the blog garden, I saw many people discussing this problem. In the comments of Haitian 1 Gull "Forms, I'm inter-form", I saw that there are such a few practices: 1) Open a static variable 2) Creating a public field in a child form; 3) Use commission and events in the parent form; 4) The child form is used as a parent form.
These measures don't feel very good, it will cause the parent window to be coupled to the child form, and the modification of any form needs to recompile another form. According to the principle of "relying on invert", this tight coupry can be avoided by introducing a result object, and can also pass any complex data. If you need to change the parent form status in the sub-form, you can define the delegate and events in this result object to achieve the purpose. I have given my solution here.
First define a result object to store the results returned by the subform. Also define some events, you can modify the status of the parent form. code show as below:
Using
System;
Namespace
WinParam
{Public delegate void TextChangedHandler (string s); public class cResult {public string Result1 = ""; public string Result2 = ""; public event TextChangedHandler TextChanged; public void ChangeText (string s) {if (! TextChanged = null) TextChanged ( s);}}}
Add a sub-form constructor to allow reception of a result object:
Private
CRESULT R;
public
FRMCHILD (CRESULT R):
THIS
()
{This.r = r;}
Create a child form in a parent form, and subscribe to the CRESULT event:
Private
Void
BtncallChild_Click
Object
Sender, System.EventArgs E)
{CResult r = new cResult (); r.TextChanged = new TextChangedHandler (this.EventResultChanged); frmChild fc = new frmChild (r); fc.ShowDialog (); txtCallResult.Text = "The Result is:" r. Result1 " R.Result2;
Private
Void
EventResultchanged
String
S)
{TXTEVENTRESULT.TEXT = S;}
This ensures that the parent form knows the child, and the child form does not know the parent form. The parent form is not required to recompile the subform. At the same time, both forms rely on the results object, and the stability of the results object also determines the stability of the parent form and the sub-form. Below is the program operation results:
Note: The code provided is just a functional demonstration. If you need to add some additional accessory (object release, cancel event subscription, etc.).
Posted on 2004-08-17 10:28 Lu Zhaoyu Reading (470) Comments (12)
edit
Collect
comment
#
RE: Data between forms
2004-08-17 11:19
Wave Suma. Xpilot
Collect
#
RE: Data between forms
2004-08-17 11:34
Lu Zhenyu
I miss Visual FoxPro, the above practice is to learn from the data navigation tools provided from the VFP. VFP returns multiple results at a time in this way.
#
RE: Data between forms
2004-08-17 12:06
Haitian one gull
This method is really good!
Recently made a MDA tool development, export the model to XML, then use the generator to export XML to the JSP Serverlet or ASP.NET page.
Among them, the modeler is a typical transfer of UI information to the background model object, which involves the delivery of many form information, but all operates the same object.
In order to catch progress, the entire frame design is not good, the redundant code is more, the multiplexing is less, the solution is not good.
I hope that after the 1.0 version is complete, in the next version, the architecture is greatly changed:
1. Using the concept of the object manager, the UI is completely decoupped with the background XML data model object.
2. The sequence function of the object uses the template method because the field serialization code of most objects is similar. If there is an template method, you can write a lot less.
3. Improve formal values
#
RE: Data between forms
2004-08-21 16:03
ASS
good stuff
#
RE: Data between forms
2004-08-27 16:49
Yogong
Collect
#
RE: Data between forms
2004-09-03 16:21
Searching
Collect
#
RE: Data between forms
2004-09-03 16:28
Searching
good stuff
#
RE: Data between forms
2004-09-20 16:49
Thousand degrees
Great
#
RE: Data between forms
2004-09-20 16:55
Thousand degrees
No, I have two sentences, I have been doing it for two days. Thank you!
#
RE: Data between forms
2004-10-17 14:48
C # beginner
I am an initiator, take the liberty to ask, doing this to change a variable value is not much better than a lot of code, what is the benefit? ?
#
RE: Data between forms
2004-10-17 14:51
C # beginner
Object release, cancel event subscription, etc. Is it place to put it, what do you need? ?
#
RE: Data between forms
2004-10-17 19:15
Lu Zhenyu
@ C # beginner
In order to make the system more stable, usually the program will follow the principle of high polymerization and loose coupling when writing. Although the code is complicated, it guarantees loose coupling. Thus, when the code of the two form changes, it does not affect the other form.
Release, cancel event subscription typically calls when the sub-form is released.