The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Using attributes to specify the contents of a menu

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Richard Jonas

Posts: 147
Nickname: rjonas
Registered: Nov, 2005

Richard Jonas is a .NET sofware developer living in the UK.
Using attributes to specify the contents of a menu Posted: Dec 3, 2006 9:56 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Richard Jonas.
Original Post: Using attributes to specify the contents of a menu
Feed Title: Richard Jonas
Feed URL: http://feeds.feedburner.com/blogspot/ouNA
Feed Description: Richard Jonas's blog about .NET, web development and agile methodologies.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Richard Jonas
Latest Posts From Richard Jonas

Advertisement

Most people think it is a good idea to separate business and presentation logic in your application, so you can update one without changing the other. When you create an application with a menu bar, the menu items will often call functions in your business logic, and if you add a new business logic function you will need to add a handler in your presentation layer and the business logic function in your business logic layer.



This article describes how you can add attributes in your business logic layer and have these dynamically bound to a menu when your application runs. The attributes will look like the following:
   public class MenuHandler
{
[MenuOption ("Menu Option 1")]
public void MenuHandler(object sender,
EventArgs e)
{
//Business Logic
}
}


Creating your Attribute

The first thing you need to do is to set up your attribute. Create a class derived from System.Attribute as follows.


    [AttributeUsage(AttributeTargets.Method)]
public class MenuOptionAttribute : System.Attribute
{
private string MenuText;

public MenuOptionAttribute(String menutext)
{
MenuText = menutext;
}

public string GetMenuText
{
get
{
return MenuText;
}
}
}


The first line of this specifies your attribute can be applied to methods (and not to classes). The constructor takes one argument, which is a string representing the menu text.


Applying your attribute to your business logic

Next, you write your business logic layer to use the MenuOption attribute. All you need to do is to put this before any functions you want on your menu, with the text to go on your menu:


    public class BusinessLogic
{
[MenuOption ("Menu Option 1")]
public void Option1(object sender, EventArgs e)
{
// Option 1 code
}

[MenuOption("Option 2")]
public void Option2(object sender, EventArgs e)
{
// Option 2 code
}

You can add as many of these functions as you like.


Setting up your menu

Finally, you need to create your presentation layer. You can drag a MenuStrip to a form and add a title for your drop down menu. If you call the title "Main", when you add the title, it will create a ToolStripMenuItem, called "mainToolStripMenuItem" by default.


You need to use reflection to iterate over all the methods in the MenuHandler class, and see which of those methods have the MenuOptionAttribute attribute set.


If it is set, you need to call the GetMenuText() function on the attribute to find out the text to display on the menu bar and add the menu item.


Next, you need to create a delegate to your method.


Finally, you add the event handler to your newly created menu item. It's easier to understand this if you read the code example.


  public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BusinessLogic bl = new BusinessLogic();

mainToolStripMenuItem.DropDownItems.Clear();
foreach (MethodInfo method in (typeof
(BusinessLogic)).GetMethods())
{
foreach (object attribute in method.
GetCustomAttributes(true))
{
if (attribute is MenuOptionAttribute)
{
ToolStripItem newitem =
mainToolStripMenuItem.DropDownItems.Add((attribute as
MenuOptionAttribute).GetMenuText);
EventInfo ci = typeof
(ToolStripItem).GetEvent("Click");
Type tdelegate = ci.
EventHandlerType;
Delegate del = Delegate.
CreateDelegate(tdelegate,bl,method);
ci.AddEventHandler(newitem,
del);
}
}
}
}
}

You could add some code to your attribute to specify its position, and use this to order your menu items. You might want to specify more than one string, one for the heading and one for the item itself, so you can put menu items in under different headings.

Read: Using attributes to specify the contents of a menu

Topic: Klassen- und Namespace-Informationen zum .NET Framework erfolgreich finden Previous Topic   Next Topic Topic: Run ASP.NET applications on Ubuntu (for developers)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use