473,811 Members | 3,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Display Context Menu on DataGridView only when specifc keys are pressed...

Using VS2005, VB.NET,
I am developing a windows app that has a DataGridView. I want to
enable the display of a context menu on this DataGridView only when a
specific set of keys is also pressed (like CTRL+ALT+SHIFT) . TO this
point I have code that only displays the context menu while keys CTRL
+ALT+SHIFT are pressed - but after the keys are released the context
menu still gets displayed. Can someone tell me where the following
code is wrong.

'Assume:
' 1. There is a control named MyDataGridView on the form
' 2. That MyDataGridView' s DataSource is a DataView filled with
several rows of data
'
Public Class MyForm
Private intMyDataGridVi ew_CurrentRow As Int16
Private MyContextMenu As ContextMenu = Nothing
Private bCtrl_Alt_Shift As Boolean = False

Private Sub myForm_Load(ByV al sender As Object, ByVal e As
System.EventArg s) Handles Me.Load
Me.MyContextMen u = New ContextMenu()
Me.MyContextMen u.MenuItems.Add ("MyContextMenu 's Action", New
EventHandler(Ad dressOf Me.MyContextMen u_Action))
End Sub

Private Sub MyContextMenu_A ction(ByVal sender As Object, ByVal e
As System.EventArg s)
MsgBox("My Data Grid View Action called!")
End Sub

Private Sub MyForm_KeyDown( ByVal sender As Object, ByVal e As
System.Windows. Forms.KeyEventA rgs) Handles Me.KeyDown
If e.Control AndAlso e.Alt AndAlso e.Shift Then
Me.bCtrl_Alt_Sh ift = True
Else
Me.bCtrl_Alt_Sh ift = False
End If
End Sub

Private Sub MyForm_KeyUp(By Val sender As Object, ByVal e As
System.Windows. Forms.KeyEventA rgs) Handles Me.KeyUp
If e.Control AndAlso e.Alt AndAlso e.Shift Then
Me.bCtrl_Alt_Sh ift = True
Else
Me.bCtrl_Alt_Sh ift = False
End If
End Sub

Private Sub MyDataGridView_ MouseDown(ByVal sender As Object, ByVal
e As System.Windows. Forms.MouseEven tArgs) Handles
MyDataGridView. MouseDown
'Next LINE: Try to remove the context menu - this doesn't seem to
remove the context menu once it has already been... I only want the
context menu to display when bCtrl_Alt_Shift is True - HELP
Me.ContextMenu = Nothing
If Me.bCtrl_Alt_Sh ift Then
Dim hti As DataGridView.Hi tTestInfo =
Me.MyDataGridVi ew.HitTest(e.X, e.Y)
If hti.ColumnIndex = -1 Then 'no content menu on header
row
Else
Try
Me.ContextMenu = Me.MyContextMen u
Me.intMyDataGri dView_CurrentRo w = hti.RowIndex
Catch ex As Exception
Me.LogError("My DataGridView_Mo useDown",
"MyDataGrid View CONTEXT MENU", ex.ToString)
End Try
End If
End If
End Sub
End Class

Oct 20 '07 #1
3 5257
bump

Oct 22 '07 #2
bump

Oct 22 '07 #3
I got this to work by using
Control.Modifie rKeys = Keys.Control + Keys.Shift + Keys.Alt

Versus updating a global variable (bCtrl_Alt_Shif t) as keys were
pressed.

The new mouse down procedure looks like this:

Private Sub MyDataGridView_ MouseDown(ByVal sender As Object, ByVal e
As System.Windows. Forms.MouseEven tArgs) Handles
MyDataGridView. MouseDown
Try
Me.MyContextMen u.Items(0).Visi ble = False 'Make the menu
item(s) invisible
Dim hti As DataGridView.Hi tTestInfo =
Me.MyDataGridVi ew.HitTest(e.X, e.Y)
If hti.ColumnIndex = -1 OrElse hti.RowIndex = -1 Then 'no
content menu on header row
Else
If e.Button = Windows.Forms.M ouseButtons.Rig ht AndAlso
Control.Modifie rKeys = Keys.Control + Keys.Shift + Keys.Alt Then
Me.MyContextMen u.Items(0).Visi ble = True
End If
End If
Catch ex As Exception 'TODO handle error
End Try

Oct 22 '07 #4

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

Similar topics

1
1552
by: Doug Bell | last post by:
Hi, If I modify my Registry adding a new key HKEY_CLASSES_ROOT\*\shell\Use My App\command with a (Default) Value of C:\Program Files\Doug\DotNetApps\MyApp.exe "%1" then right clicking on files in the Explorer Windows opens the context menu with the option "Use My App" and selecting this runs My App with the selected file name. Just as I wanted. The problem I now have is that double clicking a Visual Studio file (.sln) also starts My...
0
1760
by: Derrick | last post by:
I'm trying to calculate the height of a context menu in a manner that will work in both the full framework (where the calculation is easier because of the MeasureItem event of a MenuItem) and the compact framework (no MeasureItem event, which is why I'm stuck). I have a button that, when pressed, shows a context menu that the user is to make a selection from. Right now, I am always showing the menu below the button, but this is...
0
1574
by: VP | last post by:
G'day folks, well i am attempting to get an understanding on how to create the menuitems in a context menu on the fly. So far I have managed to actually achieve the menu items being created for the context menu. However, I have stumbled across one problem. Which is that how does one go about enhancing the event handler for the context menu to invoke individual actions for each of the menu items. I have included a snippet of my code....
2
5435
by: Christoph Brüser | last post by:
Hi, in my application I want to react to certain keys when a context menu is showing. So I installed a keyboard hook, but now whenever a key is pressed when the menu is showing, the application crashes and sometimes throws a NullReferenceException in system.windows.forms.dll. I assume that the hook somehow messes up the context menu's message loop or something like this. I also had this problem with a mouse hook, so i guess its a...
3
1199
by: Just Me | last post by:
I have a usercontrol on which I have, say, five controls. I want a context menu to popup when the mouse is clicked anywhere on the userconttrol. The same context menu. What I've done in the past is to put code in each control's mouseup event. Is that the way to go or is there a better way?
2
1822
by: Doug Bell | last post by:
Hi, I would like to add a Menu Item to the Explorer Context Menu so that when the User Right Clicks on a File, they are given a choice to launch my application with the selected file passed as a command line argument. Can someone point me to some information on how to set that up? Hopefully as part of the installation? Thanks
5
6902
by: Brett Romero | last post by:
I have a simple test app with one form, one datagridview, and a contextmenustrip. I've assigned the menustrip to the datagridview and given the grid one column. There isn't any data in the grid. I never see the context menu appear when I right click the grid. Any suggestions? Thanks, Brett
7
16482
by: Ryan | last post by:
I have a DataGridView which displays numeric (Int32) data from an underlying database. I want the numbers to be displayed in numeric format "#,###" (with commas). I want to also limit the user so they can only input numerical values. Is there any way to do this? Currently my formatting works but if the user enters a value manually the formatting is not applied to the new value. Also I haven't found a way to allow only numerical input....
0
1353
by: Ed Bitzer | last post by:
Appreciate some direction on populating an unbound DataGridView with an one dimensional array of date an appointments (a calendar) read from a coma delimited text file. My original approach was to populate after searching for today's date and then increment a week at a time by repopulating after a PageDown event and changing the start index. No problem with PgUp and PgDn but I find I find myself in the middle of a nightmare controlling...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5556
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.