最近工作中遇到一个场景,对部分字段,在后面要放置一个问号图标,当鼠标悬停在该图标上时,要弹出解释性文字简要描述字段的功能。
新建一个自定义控件,里面包了一个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