A Managed "Send To" Menu Class

Environment: Visual Studio .NET, managed C++, C#, Visual Basic

In one of the applications I have created is a "Send To" menu class using MFC, which displays a list of the user's "Send To" extensions and sends a file to the extension the user selects. Now, I tried to write a Microsoft .NET assembly and I chose the "Send To" menu class as stuff for the assembly.

WARNING: The assembly source code may contain errors. Use the source code of the assembly at your own risk.

Visually, the "Send To" menu is a menu containing menu sub-items with the graphical representation of the user's "Send To" extensions. The sub-items can be both with and without bitmaps.

The "Send To" menu class is a descendant of the System.Windows.Forms.MenuItem class and contains the following new public properties and methods:

// [C#] 
public bool EnableBitmap [  get,  set ]
public void Replace ( System.Windows.Forms.MenuItem menuItem,
                      System.EventHandler eventHandler )
public void Restore (  )
' [Visual Basic] 
Public Property EnableBitmap As Boolean
Public Sub Replace(ByVal menuItem As System.Windows.Forms.MenuItem, _
                   ByVal eventHandler As System.EventHandler)
Public Sub Restore()
EnableBitmap Gets or sets a value indicating whether the subitems have bitmaps
Replace Replaces an existing menu item by a Send To menu item
Restore Restores the menu item which is replaced by the Replace method

You can use the given class in two ways. The first one is to replace an existing menu item and the second one is to add a new "Send To" menu item to the application menu manually. Use the first method if you design an application menu using the designer, and the second one if you create an application menu manually.

To replace an existing menu item you have to perform the following steps:

  1. Declare a form variable based on the SentToMenuItem class.

    // [C#]
    private megaBYTE.SendToMenuItem menuItemSendTo;
    ' [Visual Basic] 
    Friend WithEvents MenuItemSendTo As megaBYTE.SendToMenuItem
  2. Create an instance of the SentToMenuItem class.

    // [C#]
    menuItemSendTo = new megaBYTE.SendToMenuItem();
    ' [Visual Basic] 
    Me.MenuItemSendTo = New megaBYTE.SendToMenuItem()
  3. If it is necessary, set the EnableBitmap property.

    // [C#]
    menuItemSendTo.EnableBitmap = false;
    ' [Visual Basic] 
    Me.MenuItemSendTo.EnableBitmap = False
  4. Call the Replace method.

    // [C#]
    menuItemSendTo.Replace(menuItemSendToStub, 
                  new EventHandler(menuItemSendToStub_Click));
    ' [Visual Basic] 
    Me.MenuItemSendTo.Replace(Me.MenuItemSendToStub, _
          New EventHandler(AddressOf Me.MenuItemSendToStub_Click))

The most interesting part is step 4. The first parameter of the Replace method is the item to be replaced and the second parameter is a new instance of the EventHandler class. The parameter of the EventHandler class constructor is the click event handler of the replaced item. The click event handler method looks like this:

// [C#]
private void menuItemSendToStub_Click(object sender, System.EventArgs e)
{
  if (e.GetType() == typeof(megaBYTE.SendToClickEventArgs)) 
  {
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.CheckFileExists = true;
    if (dlg.ShowDialog() == DialogResult.OK)
    {
      megaBYTE.SendToClickEventArgs se = (megaBYTE.SendToClickEventArgs)e;
      se.FileName = dlg.FileName;
    }
  }
}
' [Visual Basic] 
Private Sub MenuItemSendToStub_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MenuItemSendToStub.Click
  If TypeOf e Is megaBYTE.SendToClickEventArgs Then
    Dim dlg As New OpenFileDialog()
    dlg.CheckFileExists = True
    If dlg.ShowDialog = DialogResult.OK Then
      Dim se As megaBYTE.SendToClickEventArgs
      se = CType(e, megaBYTE.SendToClickEventArgs)
      se.FileName = dlg.FileName
    End If
  End If
End Sub

If you are going to add the "Sent To" menu manually, just use the SendToMenuItem class as the System.Windows.Forms.MenuItem class.

The assembly source code contains parts of the Microsoft SENDTO sample.

Downloads

Download demo project - 12 Kb
Download source - 7 Kb
Download assembly binaries - 48 Kb


Comments

  • Could u point me to Microsoft SENDTO sample?

    Posted by veasnamuch on 09/08/2006 11:36pm

    I am very interesting in your sample code and I would like to add manually to "Send To" menu but I cant find the resouse that u have focus bellow, so could you please guide me throught that? Best reagrds,

    Reply
  • Help

    Posted by Legacy on 06/04/2002 12:00am

    Originally posted by: venkatesh

    Hey all, I need to practice my programming in borland c++builder
    if I ever plan to get good, only problem is I
    can't think of any challenging exercises I could write. If you know any websites or you yourself want to see something written could you please tell me. Thanks

    Reply
  • "Send To|Folder Items" missing

    Posted by Legacy on 06/01/2002 12:00am

    Originally posted by: Neville Franks

    Hi Alexey,
    Thanks for providing this code. I assume you know that any Drives/Folders you have on your "Send To" menu aren't showing up using your code. Do you have a solution for that?

    Also have you published your MFC version of this code? That is of more interest to me personally.

    Neville Franks

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this upcoming webcast …

Most Popular Programming Stories

More for Developers

RSS Feeds