Universal menu command handle Pattern

Source: Internet
Author: User

Universal menu command handle Pattern
Summary
(This article is translate version, original version written in Chinese, you can see it at http://www.cnblogs.com/xdesigner/archive/2006/10/07/522927.html)
Some winform application has a lot of menuitem, standard menum item in. net Framework is not perfect, so result in a lots of menuitem_click function, it is not easy to maintains those code, this article discuss a pattern which handle menu command universal.
I think some people complain that a part of the standard winform library in. net Framework 1.1 is poor, some body fleer that Microsoft employ hight middle school students to write system. windows. forms, of cause, this viewpoint is inflation. but in the author's experience, I find that a part of the standard winform library is really poor.
Microsoft has thousands of good software engineer, according to it's power, it can provide a powerful, perfect standard winform library. here, I guess Microsoft publish a library with some disadvantage wilful. this is nothing but a business policy. microsoft drop a blank and fetch the third party to exert. this can maintenance the Microsoft's empire. I think it is a good idea.
Now talk about the content. in winform application, you use a lot of menu, int. net IDE, it provide a Menu Designer to design applicatin's menu structure, when you put a mainmenu or contextmenu into a form, you can use Menu Designer to build menu item (type is system. windows. forms. menuitem) in a tree structure. and aim at every menuitem, you can write code to handle it's click event.
This is fit to a small application, but not fit a big, complicated application with a lot of menuitem, in a big applicatin, there are a lot of menuitem_click function, A winform application in a good design, It's logic collect together and call then use a single interface instead of distributed in lot's of menuitem_click or button_click functions. in this way, the menuitem_click function only calls a single interface.
For example, a from define a golbal logic function interface, define as "Void handlecommand (string command)", in this function there are a big switch structure, this function execute logic action bases parameter "command ". then in this form, there are some code like following
Void menuitem_open_click (Object sender, system. eventargs E)
{
Handlecommand ("open ");
}
Void menuitem_save_click (Object sender, system. eventargs E)
{
Handlecommand ("save ");
}
There are a lot's of this kind of code is not a good smell, ITT can be reform, you can let all menuitem's Click Event Handler point to a same function, this function can write in

Void menuitem_click (Object sender, system. eventargs E)
{
If (sender = menuitem_open)
Handlecommand ("open ");
Else if (sender = menuitem_save)
Handlecommand ("save ");
Else if ..........
}
But this structure is not perfection, because people who write or maintain this function must know every menuitem's name, this is do harm to maintain code, if a menuitem's function changed, you must change this menuitem's name and modify menuitem_click's code.
At there, how we hope than menuitem type has command property, if menuitem has property command, then the universal menum click handler can write like

Void menuitem_click (Object sender, system. eventargs E)
{
Handlecommand (menuitem) sender). Command );
}

Very simple, easy to maintains, all you do is manage every menuitem's command property.
But menuitem has not command property, so some people expend menuitem type, build his owner menuitem type, APPEND Command property, then can use the universal menuitem_click function.
At there, I did not create my owner menuitem type, stand two point, first, IDE's menuitem designer does not support custom menuitem type. secend, there are a lot of old application already use standard menuitem type, convert then to use custom menuitem type, it is a gread work.

So I bring out a universal menuitem command handler model, the primary code no more than 100, it can support Single Handle menu command event without Custom menuitem type, this code is following

 

Public Delegate void menucommandhandler (system. Windows. Forms. menuitem, string command );

Public class menucommandsender
{
Public menucommandsender ()
{
Myhandler = new eventhandler (this. menuclick );
}
Public event menucommandhandler menucommand = NULL;
Public void clear ()
{
Foreach (binditem item in myitems)
{
If (item. menuitem! = NULL)
{
Item. menuitem. Click-= myhandler;
}
}
Myitems. Clear ();
}
Public int count
{
Get {return myitems. Count ;}
}
Public void registe (string strcommand, system. Windows. Forms. menuitem)
{
Binditem item = new binditem ();
Item. Command = strcommand;
Item. menuitem = menuitem;
Menuitem. Click + = myhandler;

Myitems. Add (item );
}
Public String getcommand (system. Windows. Forms. menuitem)
{
Foreach (binditem item in myitems)
{
If (item. menuitem = menuitem)
Return item. Command;
}
Return NULL;
}
Public System. Windows. Forms. menuitem getmenuitem (string strcommand)
{
Foreach (binditem item in myitems)
{
If (item. Command = strcommand)
Return item. menuitem;
}
Return NULL;
}

Private system. Collections. arraylist myitems = new system. Collections. arraylist ();
Private class binditem
{
Public System. Windows. Forms. menuitem = NULL;
Public String command = NULL;
}
Private system. eventhandler myhandler = NULL;
Private void menuclick (Object sender, system. eventargs E)
{
If (menucommand! = NULL)
{
Foreach (binditem item in myitems)
{
If (item. menuitem = sender)
{
Menucommand (item. menuitem, item. Command );
Break;
}
}
}
}
} // Public class menucommandsender

In your application, you create a menucommandsender instance, define a function as following and bind it to menucommandsender's menucommand event.

Void handlemenucommand (menuitem item, string command)
{
Handlecommand (command );
}

Then use menucommandsender's registe member function to registe menu command, for example

Menucommandsender cmd = new menucommandsender ();
Cmd. menucommand + = new menucommandhandler (handlemenucommand );
Cmd. registe ("open", menuitem_open );
Cmd. registe ("save", menuitem_save );

Then, this pattern start. The work for convert old application use this model is acceptable.
In fact, this pattern can expend to handle system. Windows. Forms. button or other winform control, not only system. Windows. Forms. menuitem.
This pattern is very simple and useful, I hope it is benefic to some developer who handle a lot of menuitems.
Xdesigner Studio (http://www.xdesigner.cn/default-eng.htm)

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.