HighTechTalks DotNet Forums  

Windows Forms - ContextMenu events

Dotnet Academic General Discussions microsoft.public.dotnet.academic


Discuss Windows Forms - ContextMenu events in the Dotnet Academic General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Dan Doyle via .NET 247
 
Posts: n/a

Default Windows Forms - ContextMenu events - 03-07-2005 , 02:03 PM






I have created a form that contains a table of cells like anExcel spreadsheet. The cells(Labels) are activated with a leftor right mouse click. On a left mouse click, the cell willunhighlight, highlight itself, or highlight a range of cells. Ona right click, a ContextMenu opens with behavior options for thecell or group of cells
highlighted. The ContextMenu is associated with all cells.

Summary: Each cell is registered to handle a mouse click event.As well, each cell has a ContextMenu associated with it. TheContextMenu has the Popup event handled to create the menu andeach MenuItem mouse click event is handled. The control clickand menu item click are seperate handlers. This shouldn't make adifference. Every time the Context Menu is opened, it covers acouple of cells. Every time a MenuItem is selected, a clickevent is thrown for the cell directly below the MenuItem thatwas selected, as well as for the MenuItem that was selected. Theclick event for the cell located beneath the ContextMenu isthrown first and then the MenuItem click is thrown.

Problem: How do I know when a person has opened the ContextMenuand then selected a new cell instead of a Menu Item?

Why this is a problem: The events fired are identical to when aMenu Item is selected.

Scenario 1: User selects Menu Item
1) user right clicks cell
a) Popup event thrown to build menu and highlight cell
2) ContextMenu displays
3) User selects Menu Item
a) Click event thrown for cell located beneath Menu Item
b) Click event thrown for Menu Item
4) Process menu item selected

Scenario 2: User doesn't select Menu Item
1) user right clicks cell
a) Popup event thrown to build menu and highlight cell
2) ContextMenu displays
3) User selects new cell
a) Click event thrown for new cell
4) Highlight cell

So, each scenario has identical event paths up to part 3b ofscenario 1. Well, if the user is in scenario 2, how to I knowwhether to expect a click event for a menu item or not. In otherwords, how do I know that a Menu Item click event will happen ornot. I can't see how to do this without setting a timer tosample whether the Menu Item click is thrown or no.

My first attempt to make sure I was handling the MenuItem Clickevent was to set a flag indicating the menu is active when thePopup event is fired. Therefore, if the form receives a clickevent and the menu is active, ignore the left click. This is abug though. As illustrated above, the bug is that it isreasonable for the user to select a cell instead of a MenuItemwhile the ContextMenu is open. This activity should close theContextMenu and highlight the new cell. However, this is notwhat happens. Currently, if the Menu is open and the userselects a different cell, the menu remains open and nothinghappens. The user can still go to the menu and select an optionto resume use of the form.

I can't find a property of the ContextMenu that I can check inthe Click handler to see if the ContextMenu has somethingselected. I am not sure if there are MenuItem properties thatcan be checked in the controls Click handler as I would imagineI would have to enumerate through them. Maybe I need to do ahittest, but my gut tells me that this will return the controlthat the click event was thrown for. Maybe there is a way toprevent the Click event from being thrown when a ContextMenu isover a control?

One of the kickers for me is that this works correctly for Excel?How did MS do it? A special control to behavecorrectly(DataGrid)? Well, the datagrid does not work for myparticular situation as the cell heights need to vary and is whyI use labels and build the table manually.

Thanks for any and all help.
--------------------------------
From: Dan Doyle

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>vKqOka6v6k2vlO54L/ujuA==</Id>

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.