This is a migrated thread and some comments may be shown as answers.

Context Menu's

3 Answers 56 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Terry
Top achievements
Rank 1
Terry asked on 08 Apr 2011, 03:10 PM

Hello

Can anybody tell me how I can detect where the context menu is clicked, in particular I'm looking to detect a right click in the blank space below rows (see attached file.)

The following snippet stops the right clicking on the header, and allows right clicking on a row, but I can't seem to find out when the "whitespace" is clicked? - Any clues?

Many thanks

Terry

Private Sub RadGridView1_ContextMenuOpening(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.ContextMenuOpeningEventArgs) Handles RadGridView1.ContextMenuOpening
  
          
        Dim i As Integer = 0
        Do While i < e.ContextMenu.Items.Count
            e.ContextMenu.Items.Clear()
            i += 1
        Loop
  
        If TypeOf e.ContextMenuProvider Is GridHeaderCellElement Then Exit Sub
  
          
        Dim customMenuItem1 As RadMenuItem = New RadMenuItem()
        Dim customMenuItem2 As RadMenuItem = New RadMenuItem()
        Dim customMenuItem3 As RadMenuItem = New RadMenuItem()
  
        customMenuItem1.Text = "Insert Company History"
        customMenuItem2.Text = "Remove Company History"
        customMenuItem3.Text = "Add/Edit Roles..."
  
          
            Dim separator As RadMenuSeparatorItem = New RadMenuSeparatorItem()
            e.ContextMenu.Items.Add(customMenuItem1)
            AddHandler e.ContextMenu.Items(0).Click, AddressOf RowInsert
            e.ContextMenu.Items.Add(customMenuItem2)
            AddHandler e.ContextMenu.Items(1).Click, AddressOf RowDelete
            e.ContextMenu.Items.Add(separator)
            e.ContextMenu.Items.Add(customMenuItem3)
  
        If RadGridView1.RowCount = 0 Then
            e.ContextMenu.Items(0).Visibility = ElementVisibility.Visible
            e.ContextMenu.Items(1).Visibility = ElementVisibility.Hidden
            e.ContextMenu.Items(2).Visibility = ElementVisibility.Hidden
        End If
  
    End Sub

3 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 08 Apr 2011, 04:15 PM
Hello Terry,

The ContextMenuOpening doesn't fire for the TableElement which is the blank area below the rows.
If you wish to add a context menu to this, you can do so in the following way:

1: Add a menu member to your form
Private menu As New RadContextMenu()

2: In the constructor, add an event handler to TableElement MouseDown and create and add your menu items
AddHandler Me.RadGridView1.TableElement.MouseDown, AddressOf RadGridView1_MouseDown
Dim item1 As New RadMenuItem("Menu Item 1")
AddHandler item1.Click, AddressOf Item_Click
menu.Items.Add(item1)

3: On the MouseDown event, show the menu
Private Sub RadGridView1_MouseDown(sender As Object, e As MouseEventArgs)
    If e.Button = MouseButtons.Right Then
        menu.Show(Control.MousePosition)
    Else
        MyBase.OnMouseDown(e)
    End If
End Sub

4: handle the click event for the menu item
Private Sub Item_Click(sender As Object, e As EventArgs)
    MessageBox.Show("Item Clicked")
End Sub

Hope that helps
Richard
0
Terry
Top achievements
Rank 1
answered on 11 Apr 2011, 01:55 PM
Thats great Richard.

Many thanks for your help.

Regards

Terry
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 11 Apr 2011, 01:57 PM
You're welcome. Glad I could help.
Please remember to mark as answer.
All the best
Richard
Tags
GridView
Asked by
Terry
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Terry
Top achievements
Rank 1
Share this question
or