We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

GridDesktop Context Menu

Hi,


I need 3 different context menus; each one to be opened on a specific cell type.
According to you what is the best way to do it ?
To build each one during the form load and switch between them when the selected cell type wanted is detected ?

Why in the second example in C# of Managing GridDesktops Context Menu
it is the gridDesktop.ContextMenu which is used and not GridDesktop.ContextMenuManager.ContextMenu ?

With the same example, how do I know where (on which cell), the user clicked to open the context menu ?

Here is the example copied from your site
//Define a new menu item and specify its event handler
MenuItem mi = new MenuItem(“newMenuItem”, new System.EventHandler(miClicked));

//Set the label
mi.Text = “New Item”;

//Add the menu item to the GridDesktop’s context menu
gridDesktop1.ContextMenu.MenuItems.Add(mi);

//Event Handler for the new menu item
private void miClicked(object sender, EventArgs e)
{
MenuItem mi = (MenuItem)sender;
MessageBox.Show("miCliked: " + mi.Text);
}

Hi,

Thanks for your posting and using Aspose.Cells.GridDesktop.

Please provide us windows form application using the latest GridDesktop for our reference. It will help us look into your issue more closely and precisely and we will help you with a sample code. Thanks for your cooperation;

Hi,


Could you just tell me for a beginning why in the second example in C# of Managing GridDesktops Context Menu, it is the gridDesktop.ContextMenu which is used and not GridDesktop.ContextMenuManager.ContextMenu ?
The example does not work for me. the gridDesktop.ContextMenu being the System.Windows.Form.ContextMenu.

Hi again,


Sorry I was mistaken, it works. GridDesktop.ContextMenu and GridDesktop.ContextMenuManager.ContextMenu are the same object ?

Hi Daniel,


We confirm that the GridDesktop.ContextMenu and GridDesktop.ContextMenuManager.ContextMenu are the same.

Thank you,


One thing is clarified.
Now I have a second issue: How to get the cell on which a right-click has been performed ?
Is it possible to use the ContextMenuPopup event handler parameters to get the cell row and column on which was the cursor when there was a right click ?
private void gridDesktop1_Load(object sender, EventArgs e)
{
gridDesktop1.ContextMenuManager.ContextMenu.Popup += new EventHandler(ContextMenu_Popup);
}

void ContextMenu_Popup(object sender, EventArgs e)
{
Aspose.Cells.GridDesktop.Worksheet ws;

ws = gridDesktop1.Worksheets[gridDesktop1.GetActiveWorksheet().Index];
???;
}

Hi Daniel,


You may consider using the Worksheet.GetFocusedCell method for your requirement. The aforementioned method returns an instance of GridCell, and can be used to point out the cell where right click event was triggered.

C#

void ContextMenu_Popup(object sender, EventArgs e)
{
var sheet = gridDesktop1.Worksheets[gridDesktop1.Worksheets.ActiveSheetIndex];
MessageBox.Show(sheet.GetFocusedCell().Name);
}

Thanks Babar,


As simple as that! I didn’t realize a right click changed the focus.

I still need help with contextMenu. I need to switch different context-menus according to cell content. For example when there is a button in a cell. I tried the following, but the context-menu remains as it is by default:
void ContextMenu_Popup(object sender, EventArgs e)
{
Aspose.Cells.GridDesktop.Worksheet ws;
int curRow, curCol;

ws = gridDesktop1.Worksheets[gridDesktop1.GetActiveWorksheet().Index];
curRow = ws.GetFocusedCell().Row;
curCol = ws.GetFocusedCell().Column;
Aspose.Cells.GridDesktop.Button bt = (Aspose.Cells.GridDesktop.Button)ws.Controls[curRow, curCol];
if (bt != null)
{
MenuItem[] menuItems = new MenuItem[] { new MenuItem(“Item1”, new System.EventHandler(ctxtMenuItemClicked)), new MenuItem(“Item2”) };
ContextMenu newcm = new ContextMenu(menuItems);
gridDesktop1.ContextMenu = newcm;
}
}

Do you have an idea why ?

Thanks again

Hi Daniel,


It appears you are creating the ContextMenu from scratch or am I misunderstanding the scenario and you wish to add another entry to existing ContextMenu. If it is the later case, please try the following.

C#

//Event Handler for the new menu item
private void miClicked(object sender, EventArgs e)
{
MenuItem mi = (MenuItem)sender;
MessageBox.Show("miCliked: " + mi.Text);
}
void ContextMenu_Popup(object sender, EventArgs e)
{
//Define a new menu item and specify its event handler
MenuItem mi = new MenuItem(“newMenuItem”, new System.EventHandler(miClicked));
//Set the label
mi.Text = “New Item”;
//Add the menu item to the GridDesktop’s context menu
gridDesktop1.ContextMenu.MenuItems.Add(mi);
}

Hi again,


Please try the following if you wish to create the ContextMenu from scratch.

C#

private void ctxtMenuItem1Clicked(object sender, EventArgs e)
{
MenuItem mi = (MenuItem)sender;
MessageBox.Show("ctxtMenuItem1Clicked: " + mi.Text);
}

private void ctxtMenuItem2Clicked(object sender, EventArgs e)
{
MenuItem mi = (MenuItem)sender;
MessageBox.Show("ctxtMenuItem2Clicked: " + mi.Text);
}

void ContextMenu_Popup(object sender, EventArgs e)
{
gridDesktop1.ContextMenuManager.ContextMenu.MenuItems.Clear();
gridDesktop1.ContextMenuManager.ContextMenu.MenuItems.Add(new MenuItem(“Item1”, new System.EventHandler(ctxtMenuItem1Clicked)));
gridDesktop1.ContextMenuManager.ContextMenu.MenuItems.Add(new MenuItem(“Item2”, new System.EventHandler(ctxtMenuItem2Clicked)));
}