conquistador conquistador - 27 days ago 9
Vb.net Question

Show Dialog without clipping outside a form

How can I show a dialog on mouse point without clipping the dialog itself outside?

For example, when I r-click a control that is near the right edge of the screen, the dialog will appear on the left side and if the control is near below the screen it will show the dialog up of the cursor.

This is my code:

If e.Button = MouseButtons.Right Then
Dim tool = New Form2 With {
.Location = New Point(Cursor.Position.X, Cursor.Position.Y),
.WhoSend = sender 'some property
}
tool.ShowDialog()
End If


This will always show the dialog on the bottom-right of the cursor even if the dialog is clipping outside the window.

Answer Source

You can use My.Computer.Screen.Bounds to get the area of the screen and set the location according to it, and then relocate your form position.

If e.Button = MouseButtons.Right Then
   Dim ScreenBounds = My.Computer.Screen.Bounds

   Dim tool = New Form2 With {
       .Location = New Point(if( Cursor.Position.X + .Width > ScreenBounds.Width,ScreenBounds.Width - .Width ,Cursor.Position.X ),
                             if( Cursor.Position.Y + .Height > ScreenBounds.Width,ScreenBounds.Height - .Height ,Cursor.Position.Y ),
       .WhoSend = sender 'some property
   }
   tool.ShowDialog()
End If