In the Winform interface, you can pass values to parameters of Multi-document forms.

Source: Internet
Author: User

In the Winform interface, you can pass values to parameters of Multi-document forms.

In the Winform interface, we generally use multiple documents for presentation, that is, you can load multiple forms interfaces in a way similar to common tabs. In general, it is easy to pass parameters to a new form if we open a new form. However, at the framework level, a common form is dynamically created, and the type of the form is generally input, in the multi-document set, it is determined that activation is enabled if there is one. If not, the method is created. Therefore, we may encounter some problems when passing parameters. This article describes how to pass parameters to the form object in this way to implement the corresponding data processing function.

Whether the main interface contains a tree list on the left or a toolbar on the top, it may involve passing initialization parameters when opening the form to facilitate the update and display of the form, this method of directly passing values requires a little more complexity. We can use interfaces and events for processing. Next I will introduce the entire implementation method.

1. Create or activate a multi-document form

In my Winform development framework, when loading multi-document forms, we use a unified method to build existing forms that do not exist or activate them. The Code is as follows.

        private void tool_Purchase_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)        {            ChildWinManagement.LoadMdiForm(this, typeof(FrmPurchase));        }        private void tool_TakeOut_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)        {            ChildWinManagement.LoadMdiForm(this, typeof(FrmTakeOut));        }        private void tool_StockSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)        {            ChildWinManagement.LoadMdiForm(this, typeof(FrmStockSearch));        }

The LoadMdiForm function mainly checks whether there are corresponding objects in the Multi-document set. If this function is not created, the display is activated. The Code is as follows.

/// <Summary> /// uniquely loads a certain type of form. If yes, it is displayed. Otherwise, it is created. /// </Summary> /// <param name = "mainDialog"> main form object </param> /// <param name = "formType"> type of the form to be displayed </param> // <param name = "json"> the parameter content, custom Json format </param> // <returns> </returns> public static Form LoadMdiForm (Form mainDialog, Type formType, string json) {bool bFound = false; form tableForm = null; foreach (Form form in mainDialog. mdiChildren) {if (form. getType () = formType) {bFound = true; tableF Orm = form; break ;}} if (! BFound) {tableForm = (Form) Activator. createInstance (formType); tableForm. mdiParent = mainDialog; tableForm. show ();} tableForm. bringToFront (); tableForm. activate (); return tableForm ;}

The multi-document interface built in this way is as follows.

 

2. Process Parameters passing through multiple file forms

First, to implement this method, we need to first create an interface, which is the base class of our form interface to implement this interface. Then, when loading, convert to the corresponding interface for processing. The specific interface code is as follows.

/// <Summary> /// use the ChildWinManagement helper class to process a multi-document loading form. After building or activating the form, an event of the notification form is triggered to facilitate passing relevant parameters to the target form. /// For more general processing, the passed parameters use JSON-defined strings. /// </Summary> public interface ILoadFormActived {// <summary> // process the event activated by the form /// </summary> /// <param name =" json "> Transmitted parameter content, custom JSON format </param> void OnLoadFormActived (string json );}

Here, the parameters are generic. We define them as the JSON content of strings to facilitate more powerful parameter processing.

After modifying this, we need to implement the added interface in the base-class form BaseForm, as shown below.

/// <Summary> /// basic class of the general interface /// </summary> public partial class BaseForm: XtraForm, IFunction, ILoadFormActived

This interface is easy to implement. To make it easier for business forms (inherited from the base-class form BaseForm), we provide an event for processing. The specific code is as follows.

/// <Summary> /// basic class of the general interface /// </summary> public partial class BaseForm: DevExpress. xtraEditors. xtraForm, IFunction, ILoadFormActived {// <summary> // define the processing delegate type after a form is activated /// </summary> /// <param name = "json"> </ param> public delegate void FormActiveHandler (string json ); /// <summary> /// use the ChildWinManagement helper class to process a multi-document loading form. After building or activating the form, an event of the notification form is triggered to facilitate passing relevant parameters to the target form. /// For more general processing, the passed parameters use JSON-defined strings. /// </Summary> public event FormActiveHandler LoadFormActived;

At the same time, we can implement the interface by directly calling the event. The specific code is as follows.

/// <Summary> /// event processing for form activation /// </summary> /// <param name = "json"> parameter content, custom JSON format </param> public virtual void OnLoadFormActived (string json) {// by default, nothing is done. // if you need to process the transmitted parameters, the Json parameter can be processed here as if (LoadFormActived! = Null) {LoadFormActived (json );}}

In this way, we have completed the processing of base-class forms. We have introduced the LoadMdiForm function when dynamically building and loading forms, since our interface implements the above ILoadFormActived interface, when we dynamically create or activate a form, we can use this interface to process the corresponding event. Therefore, we can modify the code of the form loading function, as shown below.

/// <Summary> /// uniquely loads a certain type of form. If yes, it is displayed. Otherwise, it is created. /// </Summary> /// <param name = "mainDialog"> main form object </param> /// <param name = "formType"> type of the form to be displayed </param> // <param name = "json"> the parameter content, custom Json format </param> // <returns> </returns> public static Form LoadMdiForm (Form mainDialog, Type formType, string json) {bool bFound = false; form tableForm = null; foreach (Form form in mainDialog. mdiChildren) {if (form. getType () = formType) {bFound = true; tableF Orm = form; break ;}} if (! BFound) {tableForm = (Form) Activator. createInstance (formType); tableForm. mdiParent = mainDialog; tableForm. show () ;}// when the form is activated, pass the corresponding parameter information ILoadFormActived formActived = tableForm as ILoadFormActived; if (formActived! = Null) {formActived. OnLoadFormActived (json);} tableForm. BringToFront (); tableForm. Activate (); return tableForm ;}

Remember that the code for opening a multi-document form is to use this interface to create or activate a specified type of form, as shown below.

ChildWinManagement.LoadMdiForm(this, typeof(FrmItemDetail));

After adding the new function parameter Json, if we need to pass a specified parameter to the corresponding form, modify the call. For example, in the following example, to test, I input a dynamically constructed class information, convert it to a Json string information to the receiving form, and load the form.

// Call var obj = new {ItemNo = "123456789", ItemName = "test name"} using custom parameters; var param = JsonConvert. serializeObject (obj, Formatting. indented); ChildWinManagement. loadMdiForm (this, typeof (FrmItemDetail), param );

We have introduced a base-class form, implemented and defined an event, and processed these notification interfaces, as shown below.

Then we loadFrmItemDetailWhat needs to be done is to process the event, as shown below.

In this way, we can implement corresponding events and process the entire notification event. Let's take a look at the final interface effect, as shown below. After receiving the form event, A prompt dialog box is displayed in the lower right corner.

Of course, we can actually do more. For example, we can pass some specific information so that it can be displayed on the interface.

As shown in the following figure, the parameter passing case interface of one customer based on the Winform development framework is as follows.

This article only explores and implements the transfer of values to different multi-document forms at the framework layer. The idea is to process the values based on common interfaces and event-driven methods, in order to achieve convenient and efficient purposes, if you have better suggestions, you also hope to have more exchanges.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.