【winform中的提示框】 使用Help.ShowPopup()做信息的提示框

做winform开发时,常用MessageBox.Show("Test");做提示框,用于验证信息提示时用诸多不便。
可以试试Help.ShowPopup()做提示框 模仿web中的提示框
效果如图
在这里插入图片描述
先看看方法

//
// 摘要:
//     显示帮助的弹出窗口。
//
// 参数:
//   parent:
//     一个 System.Windows.Forms.Control 标识帮助对话框中的父级。
//
//   caption:
//     要在弹出窗口中显示的消息。
//
//   location:
//     一个值,指定要显示弹出窗口中的,相对于屏幕左上角的水平和垂直坐标。
public static void ShowPopup(Control parent, string caption, Point location);

很简单,一个父Control,一个string信息,一个Point也就是坐标
前面两个就不说了,重点在于坐标的获取,上面location面的注释中我们知道,这个Point是相对屏幕的,所以我们获取parent的相对于容器坐标,
在这里插入图片描述
然后将其转换为屏幕坐标就行了

//上面视图中,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("连接成功");
}

上述得到的提示应该是这样的
在这里插入图片描述
所以这个位置我们要根据控件的Size调整一下
这里我们分成9个位置,如下图
在这里插入图片描述
位置枚举类

/// <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
}

写一个扩展方法方便使用

/// <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));
}

使用方法

this.Hint(txt_IP, "请检查IP地址格式", ShowPosition.Mid_Right);

需要注意的是,ShowPopup只能同时显示一个,请考虑使用环境

发布了62 篇原创文章 · 获赞 68 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/ZUFE_ZXh/article/details/104668775