Contextmenu position in vb.net - vb.net

i have a datagridview. On right click it shows a contextmenu but it is always in the right upper corner. I want it so that the menu appears on the cell where user right clicks. It could be Cell 1 or two or whatever.
Thanks
Furqan

The easiest way to do this is to handle showing your context menu on your own (not using the context menu property on the grid view) on MouseDown for the data grid. Like this:
Private Sub DataGridView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
ContextMenuStrip1.Show(CType(sender, Control), e.Location)
End If
End Sub

Related

Mousemove detect left click not firing in usercontrol winform

I know this should be simple to do but for some reason I can't get a simple mousemove when I left click the mouse. this inside a custom button I have made with 2 labels in it. I am moving only on the button area and not the label. When I move over the button I see "Mousemove" printed to the console but when I hold the left click I get nothing...Not even the "Mousemove". I looking to implement a dragdrop for this button and was just starting out simple to detect the left click and mousemove. Am I missing another setting. I had a Click event and event removed that just to detect this movement.
Thanks in advance for any info.
Public Sub New()
'InitializeComponent()
DoubleBuffered = True
AllowDrop = True
End Sub
Private Sub CustomerButton_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
Debug.Print("MouseMove")
If e.Button = MouseButtons.Left Then
Debug.Print("MouseMove and Left Click")
End If
End Sub

Show ContextMenuStripItem without clicking off the current cell

I'm using an UltraGrid which has a ContextMenuStrip with 2 items. These are shown when right-clicking an UltraGridCell.
However, in order to show them, the user has to first click off the cell to take it out of edit mode, then right click on it to show the ContextMenuStripItems.
This has become confusing and irritating to the user, so I was wondering if there is any way that it can be changed to show them when right clicking whilst still in edit mode?
I've tried this to take it out of edit mode after a key is pressed, but it doesn't work.
Private Sub ugComm_keyup(sender As Object, e As KeyEventArgs) Handles ugComm.KeyUp
ugComm.UpdateData()
If ugComm.ActiveCell.IsInEditMode = True Then
ugComm.ActiveCell.Row.Update()
End If
End Sub
I also tried something in the MouseClick that was suggested on the Infragistics forums, but again it didn't work.
Is there any way that a user right-clicking a cell that is in edit mode can bring up the ContextMenuStripItems rather than this menu?
The above image shows what is currently show when right-clicking a cell in edit mode (The cell is the bottom right white cell). I don't want this to appear, but the CMS instead.
EDIT
I've tried the suggestions in the current answers, but neither of those worked for me. Possibly because the grids are a slightly older version?
My most recent effort was done with the following code:
Private Sub ugComm_MouseDown(sender As Object, e As MouseEventArgs) Handles ugComm.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
Me.cmCommRate.Show(mouseX, mouseY)
End If
End Sub
But this wasn't triggered until the cell was no longer in edit mode.
NEITHER OF THE ANSWERS BELOW RESOLVE THE ISSUE. STILL NEEDS AN ANSWER
When any cell of the grid enters in edit mode a TextBox is drawn over the cell. The nice part here is this text box is reused for all the cells in the grid. When you right click on the cell in edit mode the default context menu, that comes from MS, shows. What you need to do is get this text box and assign it your context menu strip. You can do this by handling ControlAdded event of the grid like this:
' create a field to store the TextBox
Private cellTextBox As TextBox
Private Sub grid_ControlAdded(sender As Object, e As ControlEventArgs) Handles grid.ControlAdded
' Check if added control is TextBox
If TypeOf e.Control Is TextBox Then
' If added control is TextBox store it in your private field and set its ContextMenuStrip
If Me.cellTextBox Is Nothing Then
Me.cellTextBox = DirectCast(e.Control, TextBox)
Me.cellTextBox.ContextMenuStrip = Me.ctx
End If
End If
End Sub
I have tried to write an event handler for the MouseUp event with this code
Private Sub grid_MouseUp(sender As Object, e as MouseEventArgs) Handles grid.KeyUp
grid.PerformAction(UltraGridAction.ExitEditMode)
grid.ContextMenuStrip.Show()
End Sub
and it works.
The ContextMenuStrip was added in code with this text (as example)
ContextMenuStrip ctx = new ContextMenuStrip()
ctx.Items.Add("Test1")
ctx.Items.Add("Test2")
ctx.Items.Add("Test3")
grid.ContextMenuStrip = ctx

VB.Net - Some menustrip questions

So my MenuStrip property Dock is: None
My menustrip is not visible when I align it with tabcontrol but my linklabel is visible, how can I make my menustrip visible aswell?
http://i.imgur.com/XSV9Pcb.png
I want to change how the submenu will show when I click my Menu, by default it is showing at the right side
http://i.imgur.com/IWoiyf9.png
I want to make it show below the menu like this (edited)
http://i.imgur.com/dOtF6Ve.png
For your Third Query : Change Alignment of Your Parent Menu (e.g. Settings in your image ) to Right.
This would probably be easier if you just used a button to show a ContextMenuStrip:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ContextMenuStrip1.Show(Button1, _
New Point(Button1.Width - ContextMenuStrip1.Items(0).Width, _
Button1.Height))
End Sub
Result:

ContextMenuStrip not showing nearby cursor

I'm trying to create an event that shows a contextmenu when I right click a row in my datgridview.
Here is an image of the problem that is happening:
And here is the code I am currently using:
Private Sub dgvStudents_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvStudents.CellMouseDown
Dim rowClicked As DataGridView.HitTestInfo = dgvStudents.HitTest(e.X, e.Y)
'Select Right Clicked Row if its not the header row
If e.Button = Windows.Forms.MouseButtons.Right AndAlso e.RowIndex > -1 Then
'Clear any currently sellected rows
dgvStudents.ClearSelection()
Me.dgvStudents.Rows(e.RowIndex).Selected = True
ContextMenuStrip1.Show(dgvStudents, Control.MousePosition)
End If
End Sub
P.S the screen capture doesn't show my cursor >.> but it's definitely not synced with the context menu!
EDIT: Ok guys I've solved it,
I simply replaced Control.MousePosition to MousePosition and it worked!
Neither of these worked for me. The solution that got the menu to pop up under the mouse was:
ContextMenuStrip1.Show(MousePosition.X, MousePosition.Y)
Mouse.Position is in screen coordinates. You'll need to provide the relative coordinates, relative from dgvStudents. They are handed to you on a silver platter through the event argument:
ContextMenuStrip1.Show(dgvStudents, e.Location)
Context menus are normally displayed in response to mouse-up so do favor the CellMouseUp event instead.

Tab Control Troubles

Best to explain with an example.
TabControl has Tab1 and Tab2.
I'm trying to make it so when Tab1 is selected, Button1 is visible and stays visible.
When Tab2 is selected, Button1 is invisible and stays invisible. I need it to work when the Tab is clicked, not when the content area of the Tab is clicked.
Thank you.
Try this:
Private Sub TabControl1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControl1.MouseClick
Me.Button1.Visible = TabControl1.SelectedTab Is TabPage1
Me.Button2.Visible = TabControl1.SelectedTab Is TabPage2
End Sub
Btw, why do you need that? If Button1 is on TabPage 1 and Button2 is on TabPage2 they will appear/hide automatically.
Regards
One way you can do that is to add an "onclickclick" function to each tab head that sets the visibility style of the buttons you're trying to show / hide.