在鼠标悬停时使标签下划线

Mar*_*isP 9 c# label underline winforms

当我用鼠标输入标签时,我需要使标签下划线.我怎样才能做到这一点?我尝试了几个选项,但它没有用.谁能告诉我怎么做?

Pie*_*ult 16

您可以使用标签的事件MouseEnterMouseLeave事件来修改Font使用的标签

private void OnMouseEnter(object sender, EventArgs e)
{
    label1.Font = new Font(label1.Font.Name, label1.Font.SizeInPoints, FontStyle.Underline); 
}

private void OnMouseLeave(object sender, EventArgs e)
{
    label1.Font = new Font(label1.Font.Name, label1.Font.SizeInPoints, FontStyle.Regular); 
}
Run Code Online (Sandbox Code Playgroud)

如果您不需要修改字体名称或大小,可以直接使用 new Font(label1.Font, FontStyle.Underline)

此外,如果您需要添加多个样式,您可以使用|运算符:

label1.Font = new Font(label1.Font.Name, label1.Font.SizeInPoints, FontStyle.Underline | FontStyle.Bold); 
Run Code Online (Sandbox Code Playgroud)