Contextmenu not to show on the mouse header click

4 posts, 0 answers
  1. Beatrice
    Beatrice avatar
    10 posts
    Member since:
    Jun 2014

    Posted 27 Apr Link to this post

    Hi,

     

    I have a popup contextmenu that needs to show on the mouse right click on the data cell, BUT SHOULD NOT SHOW when the user clicks or right clicks on the grid header.

    Here is my code. How to capture the Grid header click by the mouse?

    Private Sub grdInstPymt_MouseDown(sender As Object, e As MouseEventArgs) Handles grdInstPymt.MouseDown
    Try
         If e.Button = System.Windows.Forms.MouseButtons.Right Then
             Me.ContextMenuStrip = PopUpMenu
             PopUpMenu.Show()
         End If
    Catch ex As Exception
    End Try
    End Sub

  2. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 28 Apr Link to this post

    Hello Beatrice,

    Thank you for writing.

    You can use the GetElementAtPoint method to check what is clicked:
    private void RadGridView1_MouseDown(object sender, MouseEventArgs e)
    {
        var clickedElemnt = radGridView1.ElementTree.GetElementAtPoint(e.Location);
        if (clickedElemnt is GridDataCellElement)
        {
            //show your menu here
        }
    }

    Let me know if you have additional questions.

    Regards,
    Dimitar
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Beatrice
    Beatrice avatar
    10 posts
    Member since:
    Jun 2014

    Posted 28 Apr in reply to Dimitar Link to this post

    What type would I declare the variable clickedElemnt?

    Dim clickedElemnt as ???

    clickedElemnt = radgrid1.ElementTree.GetElementAtPoint(e.Location)
    If (clickedElemnt Is GridDataCellElement) Then
         Me.ContextMenuStrip = PopUpMenu
         PopUpMenu.Show()
    End If

  5. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 29 Apr Link to this post

    Hello Beatrice,

    Thank you for writing back.

    Yes, you do not need to declare a specific type in this case. The code in Visual Basic will look like this:
    Private Sub RadGridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
        Dim clickedElemnt = radGridView1.ElementTree.GetElementAtPoint(e.Location)
        If TypeOf clickedElemnt Is GridDataCellElement Then
            'show your menu here
        End If
    End Sub

    Another approach would be to cast the object first:
    Private Sub RadGridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
        Dim clickedElemnt = TryCast(radGridView1.ElementTree.GetElementAtPoint(e.Location), GridDataCellElement)
        If clickedElemnt IsNot Nothing Then
            'show your menu here
        End If
    End Sub

    Let me know if I can assist you further.

    Regards,
    Dimitar
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top