Form2 form = new Form2();
Point leftBottom = this.PointToScreen(new Point(button1.Left, button1.Bottom));
int left = leftBottom.X;
int bottom = leftBottom.Y;
Point rightTop = this.PointToScreen(new Point(button1.Right, button1.Top));
int right = rightTop.X;
int top = rightTop.Y;
Point location = new Point();
if (left + form.Width > Screen.PrimaryScreen.Bounds.Width) {
location.X = right - form.Width;
} else {
location.X = left;
}
if (bottom + form.Height > Screen.PrimaryScreen.Bounds.Height) {
location.Y = top - form.Height;
} else {
location.Y = bottom;
}
form.Location = location;
form.Show();
}
private void button2_Click(object sender, EventArgs e) {
Point leftBottom = this.PointToScreen(new Point(button2.Left, button2.Bottom));
int left = leftBottom.X;
int bottom = leftBottom.Y;
Point rightTop = this.PointToScreen(new Point(button2.Right, button2.Top));
int right = rightTop.X;
int top = rightTop.Y;
Point location = new Point();
if (left + contextMenuStrip1.Width > Screen.PrimaryScreen.Bounds.Width) {
location.X = right - contextMenuStrip1.Width;
} else {
location.X = left;
}
if (bottom + contextMenuStrip1.Height > Screen.PrimaryScreen.Bounds.Height) {
location.Y = top - contextMenuStrip1.Height;
} else {
location.Y = bottom;
}
contextMenuStrip1.Show(location);
}