473,326 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

ContextMenu does not work in DataGridView

I cant get a contextmenu to popup in the context of a datagridview. It
pops up in the ListBox, and in the form itself. But not the
DataGridView. How could that be?

here is my code:

using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace demo_DataGridView2
{

public partial class Form1 : Form
{
DataGridView mGrid1 = null ;
ContextMenu mContextMenu = null;
ListBox mListBox1 = null;

public Form1()
{
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "Form1";
this.Width = this.Font.Height * 40;

mContextMenu = new ContextMenu();
mContextMenu.Popup += contextMenu_Popup;

mListBox1 = new ListBox();
mListBox1.Parent = this;
mListBox1.Location = new Point(10, 30);
mListBox1.Height = this.Font.Height * 5;
mListBox1.Width = this.Font.Height * 30;
mListBox1.Items.Add("line 0");
mListBox1.Items.Add("line 1");

mGrid1 = new DataGridView();
mGrid1.Parent = this;
mGrid1.Location = new Point(10, 120);
mGrid1.Height = this.Font.Height * 5;
mGrid1.Width = this.Font.Height * 30;
mGrid1.ColumnCount = 3;
mGrid1.AutoGenerateColumns = true;

string[] row1 = new string[3] {"col1", "col2", "col3"} ;
string[] row2 = new string[3] { "col1", "col2", "col3" };
mGrid1.Rows.Add(row1);
mGrid1.Rows.Add(row2);

this.ContextMenu = mContextMenu;
mListBox1.ContextMenu = mContextMenu;
mGrid1.ContextMenu = mContextMenu;
}

protected void contextMenu_Popup(System.Object InSender,
System.EventArgs InArgs)
{
mContextMenu.MenuItems.Clear();
if (mContextMenu.SourceControl == this)
{
mContextMenu.MenuItems.Add( new MenuItem("form menu item")) ;
}
else if (mContextMenu.SourceControl == mGrid1)
{
mContextMenu.MenuItems.Add( new MenuItem("grid menu item")) ;
}
else if (mContextMenu.SourceControl == mListBox1)
{
mContextMenu.MenuItems.Add(new MenuItem("ListBox menu item"));
}
}

} // end class Form1
} // end namespace demo_table

Aug 27 '07 #1
0 1528

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
by: DJ WIce | last post by:
Hi all, I did make a script/css thing to replace the contextmenu on the website with a new one: http://www.djwice.com/contextmenu.html It works nice in MSIE, but on Netscape (and probable...
0
by: MD | last post by:
I have a ContextMenu that is assigned to 3 RichTextBox's (i.e., if you right-click on any of the 3 RichTextBox's, you get the same ContextMenu). My ContextMenu has 2 items, each of which have their...
1
by: Brian Martel | last post by:
I am trying to create a dynamic flyout submenu off of a contextmenu. I can right click and get the base contextmenu ok. On the Popup event I add MenuItem's to the collection of one of the items so...
0
by: Daniel K. | last post by:
I've got a form with a usercontrol and a contextmenu for it. I put everything together in the VS form designer. the codesnipped below is from that dialog. The problem: Before i added the...
1
by: chinimimita.hernandez | last post by:
Does anyone know how windows service work with notifyicon and contextmenu? I was able to run the windows service with notifyicon part but when I inserted the contextmenu command it didn't work....
7
by: TryingLikeHeck | last post by:
I have a usercontrol on which I've generated a context menu in designer. The properties of the control lists for ContextMenu (none) The menu shows up in the designer window and I can edit it To...
11
by: Graham Charles | last post by:
I'm writing a control inheriting from ComboBox, and I'd like to add an entry to the ContextMenu for that combo box. I have no problem if I create an entirely new ContextMenu from scratch, but I'd...
6
by: emmajoh | last post by:
Hi I'm trying to disable IE contextmenu (on a right click) as I want to create my own. The problem is that since I've installed the latest version of the google desktop, the script doesn't...
1
by: Darin | last post by:
I understand microsoft's change from contextmenu to contextmenustrip - the strip looks "prettier". But, since the contextmenu isn't available in the designer, it suer woul dhave been nice when...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.