index > Visual C# General > Does the event Collapse in Contextmenu work?

Does the event Collapse in Contextmenu work?

The contextmenu provide event Collapse, but it seems do not work.

I can write the eventhandler for it, but the code respond to this event can not be executed no matter which operation is processed.

Is it a bug ?

{

.....................

ContextMenu buttonMenu = new ContextMenu(menuItems);
buttonMenu.Show(button1, new Point(0, button1 .Height ) );

buttonMenu.Collapse += new EventHandler(buttonMenu_Collapse);

}

void buttonMenu_Collapse(object sender, EventArgs e)
{

//no chance to see it
throw new Exception("The method or operation is not implemented.");
}

zhu.zheng
The ContextMenu variable is out of scope when the method exits. So the event will never be executed.



** Microsoft Community Moderator ** http://born2code.net **
PJ. van de Sande

PJ. van de Sande wrote:
The ContextMenu variable is out of scope when the method exits. So the event will never be executed.

Thank you very much.

But it does not work even I use the contextmenu as a member variable.

------------------------------------------

private ContextMenu m_cmMenu;

{
InitializeComponent();

......

m_cmMenu = new ContextMenu();
m_cmMenu.Collapse += new EventHandler(m_cmMenu_Collapse);

}

void m_cmMenu_Collapse(object sender, EventArgs e)
{
//throw new Exception("The method or operation is not implemented.");
}

zhu.zheng
But now you only create a ContextMenu, you need to bind it to a control also or call the Show method yourself:


// Bind menu to a control
lstMyListView.ContextMenu = m_cmMenu;

// Show it programaticly
m_cmMenu.Show( _myControl, MousePosition );





** Microsoft Community Moderator ** http://born2code.net **
PJ. van de Sande
PJ. van de Sande wrote:
But now you only create a ContextMenu, you need to bind it to a control also or call the Show method yourself:


// Bind menu to a control
lstMyListView.ContextMenu = m_cmMenu;

// Show it programaticly
m_cmMenu.Show( _myControl, MousePosition );


Thanks.

Yes, I do.

I'm writing a dropdownbutton. It look like a button, it will popup a menu when you click it.

Most of task complete successful, such as add menu item, show menu. but there is a trouble ---- how to know when the menu Collapse?

Below is a simple sample code. It can show menu, but can not fire the event Collapse .

--------------------------------------------------------------------

public partial class Form1 : Form
{

ContextMenu buttonMenu;


public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//Declare the menu items and the shortcut menu.
MenuItem menu1 = new MenuItem("Some Button Info");

MenuItem menu2 = new MenuItem("Some Other Button Info");

MenuItem[] menuItems = new MenuItem[] { menu1, menu2, new MenuItem("Exit") };

ContextMenu buttonMenu = new ContextMenu(menuItems);
buttonMenu.Show(button1, new Point(0, button1.Height));

buttonMenu.Collapse += new EventHandler(buttonMenu_Collapse);
}

void buttonMenu_Collapse(object sender, EventArgs e)
{
throw new Exception("The method or operation is not implemented.");
}

}

zhu.zheng
You wire the event after you show the menu, then it isn't triggerd. And when button1 is clicked and the menu is shown, the method exits and the menu is out of scope and will never be there again.

So first wire the event and then show the menu.



** Microsoft Community Moderator ** http://born2code.net **
PJ. van de Sande

PJ. van de Sande wrote:
You wire the event after you show the menu, then it isn't triggerd. And when button1 is clicked and the menu is shown, the method exits and the menu is out of scope and will never be there again.

So first wire the event and then show the menu.

................................

Thanks.

It seems that the half of your suggestion is right.

When I wire the event popup before show the menu, this event does work.

But the Collapse still do not work.

buttonMenu.Popup += new EventHandler(buttonMenu_Popup);
buttonMenu.Collapse += new EventHandler(buttonMenu_Collapse);

buttonMenu.Show(button1, new Point(0, button1.Height - 2));


}

void buttonMenu_Popup(object sender, EventArgs e)
{
MessageBox.Show("Popup");
}

void buttonMenu_Collapse(object sender, EventArgs e)
{
MessageBox.Show("Collapse");
}

zhu.zheng
reply 7

You can use google to search for other answers

 

More Articles

• Problem with user default database
• Syncing data - do i need to lock this property?
• using System.Drawing namespace in a Class Library Output
• Progress Bar
• Updating and retreiving entries from event log!
• Screen( Image from Desktop )
• global variable problem
• Three questions regarding C# and .NET of today and tomorrow
• How can I select a cell in DataGridView ?
• inserting theEntry.name to treeView.
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Other application windows appear between
• Amazon Web Services
• Load CD Files
• How do you import multiple files?
• Can't load assembly in a new AppDomain d
• Trigger functionality within a C# applic
• Switch to C# Programming
• How can I check if a specific registry e
• Custom File Properties
• Two clients, server and TCP
• C#, COM WordApp and Textboxes???
• Should I use obj = null?
• How to create the pop up on the button c
• Error Help please!
• What is the best way to send and receive

Hot Articles

• Converting from vb toc#
• java PushbackInputStream
• Move form without titlebar
• Layered transparency
• Better way? Last appended item.
• Adding/Removing Ip Addresses
• GridView
• Joining Pics
• Help
• about blocking popup Ad websites
• little help here
• xml node creating
• implementing preprocessor directive (#re
• How can I zip a file xml
• Context menu question

Recommend Articles

• Application.Exit() not working
• Aplication update automatically
• How to change the windows Display(Appera
• Datagridview change row color on form load
• To Load SHDOCLC.dll and customize contex
• override virtual method
• Windows Service - Passing arguments
• smart card application in visual C# 2005
• Impersonate user
• run an pre made program in a window fram
• Databound combobox.selectedvalue
• Can I display any text on RowHeaders in
• i have installed .net 2005 professional
• Send Email...
• Assigning permissions to external proces