侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

C#自定义控件:鼠标悬停时触发的存放解释性文字信息的气泡

2024-05-13 星期一 / 0 评论 / 0 点赞 / 93 阅读 / 2774 字

最近工作中遇到一个场景,对部分字段,在后面要放置一个问号图标,当鼠标悬停在该图标上时,要弹出解释性文字简要描述字段的功能。 新建一个自定义控件,里面包了一个PictureBox: 该控件C#代码如

最近工作中遇到一个场景,对部分字段,在后面要放置一个问号图标,当鼠标悬停在该图标上时,要弹出解释性文字简要描述字段的功能。

新建一个自定义控件,里面包了一个PictureBox:

该控件C#代码如下:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace CustuomControl{    public partial class QuestionMark : UserControl    {        public QuestionMark()        {            InitializeComponent();            //显示气泡            toolTip.AutoPopDelay = 30000;             toolTip.IsBalloon = true;            //toolTip.UseFading = true;            string noticeInfo = NoticeDetail.GetNoticeInfo(NoticeCode);            toolTip.SetToolTip(this.pcbQuestionMark, noticeInfo);        }        ToolTip toolTip = new ToolTip();             /// <summary>        /// 提示信息代码        /// </summary>        private string _noticeCode;        /// <summary>        /// 提示信息代码        /// </summary>        public string NoticeCode        {            get            {                return _noticeCode;            }            set            {                _noticeCode = value;                string noticeInfo = NoticeDetail.GetNoticeInfo(NoticeCode);                toolTip.SetToolTip(this.pcbQuestionMark, noticeInfo);            }        }    }}

其中用到的NoticeDetail类,存储了提示信息编号与提示信息的关系。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CustuomControl{    public class NoticeDetail    {        public static string GetNoticeInfo(string noticeCode)        {            string result = "";            if (!string.IsNullOrWhiteSpace(noticeCode))            {                switch (noticeCode)                {                    /** 所有的说明信息存储于此 */                    case "100001": result = "英文名:Yue Buqun"; break;                }            }            return result;        }    }}

在使用控件时,可直接向NoticeCode属性赋值,也可以自己写代码手动赋值。

END

广告 广告

评论区