When do winform development, commonly used MessageBox.Show("Test");
to make prompt box, with a lot of inconvenience for authentication information when prompted.
Try Help.ShowPopup()
to make a prompt box prompt box in imitation web
effect is shown
first look at methods
//
// 摘要:
// 显示帮助的弹出窗口。
//
// 参数:
// parent:
// 一个 System.Windows.Forms.Control 标识帮助对话框中的父级。
//
// caption:
// 要在弹出窗口中显示的消息。
//
// location:
// 一个值,指定要显示弹出窗口中的,相对于屏幕左上角的水平和垂直坐标。
public static void ShowPopup(Control parent, string caption, Point location);
Very simple, a parent Control
, a string
message, a Point
is the coordinate of
the first two did not say, focus on the coordinates of the acquisition, the above location
comments face, we know that this Point
is a relative of the screen, so we get parent
the coordinates relative to the container,
then converted into screen coordinates on the line
//上面视图中,TextBox为txt_IP,Button为btn_Connect
//实现上面的效果具体如下
/// <summary>
/// 连接
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Btn_Connect_Click(object sender, EventArgs e)
{
if (!IPAddress.TryParse(txt_IP.Text.Trim(), out IPAddress iP))
{
Help.ShowPopup(txt_IP, "请检查IP地址格式", PointToScreen(txt_IP.Location));
return;
}
MessageBox.Show("连接成功");
}
Get tips above should be like this
so we have to control this position based on Size
adjustments to
where we split into 9 position, as shown
position enum class
/// <summary>
/// 显示位置
/// </summary>
public enum ShowPosition
{
/// <summary>
/// 上左
/// </summary>
Top_Left,
/// <summary>
/// 上中
/// </summary>
Top_Mid,
/// <summary>
/// 上右
/// </summary>
Top_Right,
/// <summary>
/// 中左
/// </summary>
Mid_Left,
/// <summary>
/// 中中
/// </summary>
Mid_Mid,
/// <summary>
/// 中右
/// </summary>
Mid_Right,
/// <summary>
/// 底左
/// </summary>
Bottom_Left,
/// <summary>
/// 底中
/// </summary>
Bottom_Mid,
/// <summary>
/// 底右
/// </summary>
Bottom_Right
}
Write an extension method easy to use
/// <summary>
/// 提示框
/// </summary>
/// <param name="form"></param>
/// <param name="hint"></param>
public static void Hint(this Form form, Control control, string hint, ShowPosition position = ShowPosition.Bottom_Right)
{
Point point;
switch (position)
{
case ShowPosition.Top_Left:
point = new Point
{
X = control.Location.X,
Y = control.Location.Y
};
break;
case ShowPosition.Top_Mid:
point = new Point
{
X = control.Location.X + control.Size.Width / 2,
Y = control.Location.Y
};
break;
case ShowPosition.Top_Right:
point = new Point
{
X = control.Location.X + control.Size.Width,
Y = control.Location.Y
};
break;
case ShowPosition.Mid_Left:
point = new Point
{
X = control.Location.X,
Y = control.Location.Y + control.Size.Height / 2
};
break;
case ShowPosition.Mid_Mid:
point = new Point
{
X = control.Location.X + control.Size.Width / 2,
Y = control.Location.Y + control.Size.Height / 2
};
break;
case ShowPosition.Mid_Right:
point = new Point
{
X = control.Location.X + control.Size.Width,
Y = control.Location.Y + control.Size.Height / 2
};
break;
case ShowPosition.Bottom_Left:
point = new Point
{
X = control.Location.X,
Y = control.Location.Y + control.Size.Height
};
break;
case ShowPosition.Bottom_Mid:
point = new Point
{
X = control.Location.X + control.Size.Width / 2,
Y = control.Location.Y + control.Size.Height
};
break;
case ShowPosition.Bottom_Right:
point = new Point
{
X = control.Location.X + control.Size.Width,
Y = control.Location.Y + control.Size.Height
};
break;
default:
point = new Point
{
X = control.Location.X + control.Size.Width,
Y = control.Location.Y + control.Size.Height
};
break;
}
//提示框
Help.ShowPopup(control, hint, form.PointToScreen(point));
}
Instructions
this.Hint(txt_IP, "请检查IP地址格式", ShowPosition.Mid_Right);
Note that, ShowPopup only a simultaneous display, consider using environment