vb.net controls:
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. (watch for wrapping) '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
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
Don't see what you're looking for? Try a search.
|