The Winform interface implements event processing for common toolbar buttons. The winform Toolbar

Source: Internet
Author: User

The Winform interface implements event processing for common toolbar buttons. The winform Toolbar

In a project for the customer, the interface requires you to modify the event processing of the general toolbar button, that is, to place several fixed function operation buttons on the main interface, when opening different pages, implement the corresponding page function processing. This is different from my standard interface processing method. The standard list interface usually contains some common buttons on the interface, such as query/update, new, edit, delete, import, export, and other general operations, now you need to put the buttons at the level of the main interface, this processing method also provides a convenient way. This article describes how to implement this function and the code implementation logic.

1. interface functions

The general toolbar button event processing is implemented here. The specific interface is like this. We are in a multi-document main interface and place these general toolbar buttons on the top toolbar of the main interface, in order to improve the processing convenience and efficiency.

From the interface above, we can see that there are some common operations on the top toolbar of the main interface of the program, including buttons such as query, new, edit, delete, import, and export, it can provide convenience for functional operations of the program.

When we open a new form page, the corresponding button event should also be related to the form, or we switch to another form, the button Processing Event should also change to the corresponding form, which is what we need.

The above logic is that we create a new form or switch a form, so we need to notify the top toolbar to update or perform a docking process.

2. Processing of the interface form base class

Because we want to reduce the development workload as much as possible, we hope to encapsulate some events or interfaces in the base class to reduce coding when creating a list form.

For a processing event such as a query operation, we need to define an interface to implement this function so that we can convert it to the corresponding interface for processing when opening the form.

For example, we define an IMenuAction interface. Taking an update event as an example, we need to implement three interfaces: one is the event definition, the other is to determine whether an update event is contained, and the other is the specific processing logic.

/// <Summary> /// define the universal button event in the menu /// </summary> public interface IMenuAction {/// <summary> // update the button event // /</summary> event EventHandler Refresh_MenuEvent; /// <summary> /// whether the update event is included /// </summary> bool HasEventRefresh {get ;} /// <summary> /// update /// </summary> void ProcessRefresh (object sender, EventArgs e );

We also define a total of six other standard button events. The interface definition of the entire IMenuAction is as follows.

/// <Summary> /// define the universal button event in the menu /// </summary> public interface IMenuAction {/// <summary> // update the button event // /</summary> event EventHandler Refresh_MenuEvent; /// <summary> /// create button event /// </summary> event EventHandler Add_MenuEvent; /// <summary> /// edit button event /// </summary> event EventHandler Edit_MenuEvent; /// <summary> /// delete button event /// </summary> event EventHandler Delete_MenuEvent; /// <summary> /// import button event /// </summary> event EventHandler Import_MenuEvent; /// <summary> /// export button event /// </summary> event EventHandler Export_MenuEvent; /// <summary> /// whether the update event is included /// </summary> bool HasEventRefresh {get ;} /// <summary> /// whether or not the added event is included /// </summary> bool HasEventAdd {get ;} /// <summary> /// whether the edit event is included /// </summary> bool HasEventEdit {get ;} /// <summary> /// whether the deletion event is included /// </summary> bool HasEventDelete {get ;} /// <summary> /// whether the import event is included /// </summary> bool HasEventImport {get ;} /// <summary> /// whether the export event is included /// </summary> bool HasEventExport {get ;} /// <summary> /// update /// </summary> void ProcessRefresh (object sender, EventArgs e ); /// <summary> /// add operation /// </summary> void ProcessAdd (object sender, EventArgs e ); /// <summary> /// edit operation /// </summary> void ProcessEdit (object sender, EventArgs e ); /// <summary> /// Delete /// </summary> void ProcessDelete (object sender, EventArgs e ); /// <summary> /// import operation /// </summary> void ProcessImport (object sender, EventArgs e ); /// <summary >/// export /// </summary> void ProcessExport (object sender, EventArgs e );}

In addition to inheriting from the standard form XtraForm, the basic form of the List interface also enables it to implement the corresponding IMenuAction interface, the following is the form definition of the base class BaseDock on the list interface.

/// <Summary> /// base class used for the General List interface /// </summary> public partial class BaseDock: XtraForm,IMenuAction

The base class of this BaseDock needs to implement the interfaces of common button events, as shown below.

# Region universal button menu event // <summary> // Update button event // </summary> public event EventHandler Refresh_MenuEvent; /// <summary> /// determine whether the event exists. /// </summary> public bool HasEventRefresh {get {return Refresh_MenuEvent! = Null; }}/// <summary> // call event definition // </summary> public void ProcessRefresh (object sender, EventArgs e) {if (Refresh_MenuEvent! = Null) {Refresh_MenuEvent (sender, e) ;}}...... ...... # endregion

By using the base class, we can assign a value to the corresponding event in the specific list form.

3. Processing of the list form Interface

For example, we process the list management interface of an Application menu, and define the form as follows, inheriting the base class BaseDock of IMenuAction.

/// <Summary> /// Application menu management /// </summary> public partial class FrmApplicationMenu: BaseDock

Then we need to specify several events to process. The initialization code is as follows.

/// <Summary> /// process the initialization public menu button /// </summary> private void InitMenuAction () {this. refresh_MenuEvent + = (s, e) =>{ btnSearch_Click (s, e) ;}; this. add_MenuEvent + = (s, e) =>{ btnAddNew_Click (s, e) ;}; this. edit_MenuEvent + = (s, e) =>{ winGridViewPager1_OnEditSelected (s, e) ;}; this. delete_MenuEvent + = (s, e) =>{ winGridViewPager1_OnDeleteSelected (s, e) ;}; this. import_MenuEvent + = (s, e) =>{ btnImport_Click (s, e) ;}; this. export_MenuEvent + = (s, e) =>{ btnExport_Click (s, e );};}

In this way, we will know how the corresponding interface is related to a specific page event.


4. Processing of the main form Interface

When building the function tree on the left of the main interface, we load the corresponding form through the selected event in the tree list. The Code is as follows.

// Process the click operation of the tree menu. If a TAG exists, parse and load the corresponding page to the treeView in multiple documents. afterSelect + = (sender, e) =>{ string tag = e. node. tag as string; if (! String. IsNullOrEmpty (tag )){LoadPlugInForm(Tag );}};

In this function, we finally construct the corresponding form dynamically by configuring. The code for the final implementation of LoadPlugInForm logic is as follows.

    var form = LoadMdiForm(this.mainForm, objType, isShowDialog);    RefreshButton(form);

Here we construct a form or activate a form to get a form object, and then refresh the status of the function button to process it.

/// <Summary> /// Update button status /// </summary> /// <param name = "form"> current form </param> public void RefreshButton (form form) {this. currentForm = form; IMenuAction action = form as IMenuAction; if (action! = Null) {// judge menuButton after event processing. refresh. enabled = (action. hasEventRefresh); menuButton. add. enabled = (action. hasEventAdd); menuButton. edit. enabled = (action. hasEventEdit); menuButton. delete. enabled = (action. hasEventDelete); menuButton. import. enabled = (action. hasEventImport); menuButton. export. enabled = (action. hasEventExport );}}

In this way, you can implement multi-document processing on the main interface, whether it is to create a list form or to activate the switch to another form, the button status is bound to the corresponding form to update the display at any time.


Of course, we need to initialize the event of the general toolbar button. It is uniformly processed according to the selected form. The specific code is as follows.

/// <Summary> /// event processing for the initialization menu button /// </summary> private void InitMenuEvent () {// event binding // bind the event to menuButton first. add. itemClick + = (s, e) =>{ if (CurrentForm! = Null) {IMenuAction action = CurrentForm as IMenuAction; if (action! = Null) {action. ProcessAdd (s, e) ;}}; menuButton. edit. ItemClick + = (s, e) =>{ if (CurrentForm! = Null) {IMenuAction action = CurrentForm as IMenuAction; if (action! = Null) {action. ProcessEdit (s, e) ;}}; menuButton. delete. ItemClick + = (s, e) =>{ if (CurrentForm! = Null) {IMenuAction action = CurrentForm as IMenuAction; if (action! = Null) {action. ProcessDelete (s, e) ;}}; menuButton. import. ItemClick + = (s, e) =>{ if (CurrentForm! = Null) {IMenuAction action = CurrentForm as IMenuAction; if (action! = Null) {action. ProcessImport (s, e) ;}}; menuButton. export. ItemClick + = (s, e) =>{ if (CurrentForm! = Null) {IMenuAction action = CurrentForm as IMenuAction; if (action! = Null) {action. ProcessExport (s, e) ;}}; menuButton. refresh. ItemClick + = (s, e) =>{ if (CurrentForm! = Null) {IMenuAction action = CurrentForm as IMenuAction; if (action! = Null) {action. ProcessRefresh (s, e );}}};}

The above event only needs to be bound once, so it does not cause multiple binding issues for button events. Although it is bound once, the specific processing is still related to the current form, it converts the current form to the corresponding IMenuAction interface, and then calls the corresponding handler function implementation function binding.

The above are my ideas and specific logic code for implementing this kind of universal button event processing for your reference and learning. Please forgive me for any mistakes or omissions.

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: 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.