Windows Develop Bookmark and Share   
 index > Windows Forms General > DatagridView ContextMenustrip on Dual Monitors
 

DatagridView ContextMenustrip on Dual Monitors

Hello everyone,

i was just wondering about the behaviour of the Contextmenustrip of the DataGridView while using 2 Monitors.

When I click on the right side of the first monitor the ContextMenuStrip appears on the second monitor.
Can somebody help me, and tell me how to achieve that the ContextMenustrip is always showed on the first monitor (like it does in Outlook or Excel)?

Thank you very much in advance.

Juri
Juri G_  Friday, April 04, 2008 8:18 AM

As long as the DGV is entirely on one screen, this code will keep the ContextMenuStrip from displaying on the second screen:

Code Snippet

Private Sub ContextMenuStrip1_Opening _

(ByVal sender As Object, _

ByVal e As System.ComponentModel.CancelEventArgs) _

Handles ContextMenuStrip1.Opening

Dim cms As ContextMenuStrip = sender

Dim targetScreen As Screen = Screen.FromControl(cms)

Dim dgvBounds As Rectangle = Me.DataGridView1.Bounds

Dim extremeDGVPoint As New Point _

(PointToScreen(New Point(dgvBounds.Right, dgvBounds.Bottom)))

Dim dgvScreen As Screen = Screen.FromPoint(extremeDGVPoint)

If Not targetScreen.Equals(dgvScreen) Then

If extremeDGVPoint.X < cms.Bounds.X Then

'targetScreen is to the right of dgvScreen

cms.Show(New Point(cms.Bounds.X - cms.Width, cms.Bounds.Y))

ElseIf extremeDGVPoint.Y > cms.Bounds.Y Then

'targetScreen is below dgvScreen

cms.Show(New Point(cms.Bounds.X, cms.Bounds.Y - cms.Height))

End If

End If

End Sub

You can tweak it if you want to handle things a bit differently, but this should give you a goodidea of how to do it.
JayStation3  Friday, April 04, 2008 7:58 PM

As long as the DGV is entirely on one screen, this code will keep the ContextMenuStrip from displaying on the second screen:

Code Snippet

Private Sub ContextMenuStrip1_Opening _

(ByVal sender As Object, _

ByVal e As System.ComponentModel.CancelEventArgs) _

Handles ContextMenuStrip1.Opening

Dim cms As ContextMenuStrip = sender

Dim targetScreen As Screen = Screen.FromControl(cms)

Dim dgvBounds As Rectangle = Me.DataGridView1.Bounds

Dim extremeDGVPoint As New Point _

(PointToScreen(New Point(dgvBounds.Right, dgvBounds.Bottom)))

Dim dgvScreen As Screen = Screen.FromPoint(extremeDGVPoint)

If Not targetScreen.Equals(dgvScreen) Then

If extremeDGVPoint.X < cms.Bounds.X Then

'targetScreen is to the right of dgvScreen

cms.Show(New Point(cms.Bounds.X - cms.Width, cms.Bounds.Y))

ElseIf extremeDGVPoint.Y > cms.Bounds.Y Then

'targetScreen is below dgvScreen

cms.Show(New Point(cms.Bounds.X, cms.Bounds.Y - cms.Height))

End If

End If

End Sub

You can tweak it if you want to handle things a bit differently, but this should give you a goodidea of how to do it.
JayStation3  Friday, April 04, 2008 7:58 PM
Thank you very much!

Juri G_  Monday, April 07, 2008 1:42 PM

You can use google to search for other answers

Custom Search

More Threads

• Bitmap Clone
• DataGridView leaking GDI objects
• Set cursor position (CaretIndex) in maskedTextBox
• Grid view - How to make a row expand to display all the data.
• Microsoft Report Viewer Export question...
• Separating values of a text box
• MDI child form inside panel?
• How can I stop opening context menu for a DataGrid when clicked on blank rows or on headers?
• Displaying a Collection
• Playing audio on Form