473,321 Members | 1,708 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,321 software developers and data experts.

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 intMyDataGridView_CurrentRow As Int16
Private MyContextMenu As ContextMenu = Nothing
Private bCtrl_Alt_Shift As Boolean = False

Private Sub myForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Me.MyContextMenu = New ContextMenu()
Me.MyContextMenu.MenuItems.Add("MyContextMenu's Action", New
EventHandler(AddressOf Me.MyContextMenu_Action))
End Sub

Private Sub MyContextMenu_Action(ByVal sender As Object, ByVal e
As System.EventArgs)
MsgBox("My Data Grid View Action called!")
End Sub

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

Private Sub MyForm_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.Control AndAlso e.Alt AndAlso e.Shift Then
Me.bCtrl_Alt_Shift = True
Else
Me.bCtrl_Alt_Shift = False
End If
End Sub

Private Sub MyDataGridView_MouseDown(ByVal sender As Object, ByVal
e As System.Windows.Forms.MouseEventArgs) 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_Shift Then
Dim hti As DataGridView.HitTestInfo =
Me.MyDataGridView.HitTest(e.X, e.Y)
If hti.ColumnIndex = -1 Then 'no content menu on header
row
Else
Try
Me.ContextMenu = Me.MyContextMenu
Me.intMyDataGridView_CurrentRow = hti.RowIndex
Catch ex As Exception
Me.LogError("MyDataGridView_MouseDown",
"MyDataGridView CONTEXT MENU", ex.ToString)
End Try
End If
End If
End Sub
End Class

Oct 20 '07 #1
3 5209
bump

Oct 22 '07 #2
bump

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

Versus updating a global variable (bCtrl_Alt_Shift) 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.MouseEventArgs) Handles
MyDataGridView.MouseDown
Try
Me.MyContextMenu.Items(0).Visible = False 'Make the menu
item(s) invisible
Dim hti As DataGridView.HitTestInfo =
Me.MyDataGridView.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.MouseButtons.Right AndAlso
Control.ModifierKeys = Keys.Control + Keys.Shift + Keys.Alt Then
Me.MyContextMenu.Items(0).Visible = 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
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...
0
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...
0
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...
2
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...
3
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...
2
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...
5
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. ...
7
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...
0
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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

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.