Answered How to set left click for ContextMenuStrip for datagridview cell or row ?

rajdh75

Active member
Joined
Mar 30, 2020
Messages
29
Programming Experience
Beginner
Hello,
I am making a small project on Invoice.
I have a datagridview for Adding, Editing and Deleting products.
For Adding there is a Button.
For Editing and Deleting products I have made a ContextMenuStrip1.
The ContextMenuStrip1 is operated when someone select the cell of datagridview and right click it.
User did not know how to Edit or Delete some particular product because there is no button for it.
Can I show a message as "click for edit or delete", if he / she mouse hover the cell ?
Can I make ContextMenuStrip1 set default for left click ?
Thanks.
 
Can I show a message as "click for edit or delete", if he / she mouse hover the cell ?
Don't see why not : Screenshot

Did you read up on the available events for cell interaction?

Can I make ContextMenuStrip1 set default for left click?

Yes, listen for the mouse click events on the object you are clicking and then in an if statement, if the button is the left button, show context menu.

Post back what you've tried. Please keep note for next time, that you are asking two different questions. Each question belongs in its own topic.
 
To show a single tooltip for whole grid add a ToolTip component to form, then set grids ShowCellToolTips to False and set the text for the Tooltip:
1590964442773.png
 
Don't see why not : Screenshot

Did you read up on the available events for cell interaction?



Yes, listen for the mouse click events on the object you are clicking and then in an if statement, if the button is the left button, show context menu.

Post back what you've tried. Please keep note for next time, that you are asking two different questions. Each question belongs in its own topic.
I have tried following code for DataGridView1_MouseClick event.
VB.NET:
Private Sub DataGridView1_MouseClick(sender As Object, e As MouseEventArgs) Handles DataGridView1.MouseClick
        If Button Is the left button Then
        ContextMenuStrip1.Show()
        End If

    End Sub
but it give error on Button and left button.
So I changed code to
VB.NET:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        ContextMenuStrip1.Show(Cursor.Position.X, Cursor.Position.Y)
        
    End Sub
then it is working correct.
 
@JohnH correct me if wrong, but if memory serves me correctly, I recall you can also achieve this :
Yes, listen for the mouse click events on the object you are clicking and then in an if statement, if the button is the left button, show context menu.
By setting these properties from the designers properties windows too, which if correct is evidently much less work than i suggested.
 
OP has two unrelated questions, you answered one, I answered the other :)
 
VB.NET:
VB.NET:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        ContextMenuStrip1.Show(Cursor.Position.X, Cursor.Position.Y)
       
    End Sub
then it is working correct.
Yes, that will open the context menu in the position of where your mouse clicks. But hold up on that for a sec.
OP has two unrelated questions, you answered one, I answered the other :)
Indeed sir. :) But I think you may be able to provide a better answer to my answer. Re-read post #6. - If I recall, you can also set up the context menu in the designers properties window, so he doesn't need the suggested code I provided. Just trying to reflect for my own edification since I forget.
 
In regards this :
VB.NET:
VB.NET:
Private Sub DataGridView1_MouseClick(sender As Object, e As MouseEventArgs) Handles DataGridView1.MouseClick
        If Button Is the left button Then
        ContextMenuStrip1.Show()
        End If

    End Sub
Should be :
VB.NET:
        If e.Button = MouseButtons.Left Then
            ContextMenuStrip1.Show(Cursor.Position.X, Cursor.Position.Y)
        End If
But wait for John to get back to you on what I asked him on post 6, and 8. If I recall it's easier to set this up in the designer properties without any code writing.
 
For standard UI behaviour with right-click for context menu you would only need to assign ContextMenuStrip property in Designer, but for left-click you need that code. Setting RightToLeft will not do I'm afraid. :LOL:

You can simplify:
VB.NET:
ContextMenuStrip1.Show(Cursor.Position)
 
Ah yes. Of course. You see I remember you answered a question on this subject before on another thread, so I knew you'd know if there was an easier way by using the designer properties. And since I no longer use the windows designer and I hand-code all of my code, I was unsure if this was something that could be set from the designer properties box. Thanks for clarifying.

@rajdh75 - alter the code you have so your if statement is like mine on post #9 and alter the expression to be as is on post #10
I will mark this one as answered.
 
Last edited:
Back
Top