我的winform应用程序中有一个按钮。问题是,当我禁用按钮时,前景变为绿色 - 即使它已被禁用,还有一种方法可以保持前色黑色???我从过去的SO问题尝试了很多解决方案,现在用谷歌搜索安静。但解决方案是在c#(我转换它们但仍然没有运气)或创建自定义按钮控件。任何解决方案?
答案 0 :(得分:1)
我有一个类似的问题。按钮没有进行.ForColor
分配的原因(如果我没记错的话)与正在重绘和重绘它的背景事件有关。我还没有完全了解“坚韧不拔”,但是要领是(到目前为止,我发现)解决颜色的唯一方法是在绘画事件中。
基本上,您可以在扩展程序中处理颜色变化,而不必像这样使用Button.Enabled = False
来调用它:
btnNext.Enable
btnPrevious.Disable
您需要做的就是在项目中添加一个模块(如下),所有按钮的扩展名分别为.Enable
和.Disable
。
这里是模块:
Module Extensions
<System.Runtime.CompilerServices.Extension()>
Public Sub Enable(ByVal btn As Button)
If btn.Tag = Nothing Then ' << This ensures we only add the handler once.
AddHandler btn.EnabledChanged, AddressOf btn_EnabledChanged
AddHandler btn.Paint, AddressOf btn_Paint
End If
btn.Enabled = True
End Sub
<System.Runtime.CompilerServices.Extension()>
Public Sub Disable(ByVal btn As Button)
If btn.Tag = Nothing Then ' << This ensures we only add the handler once.
AddHandler btn.EnabledChanged, AddressOf btn_EnabledChanged
AddHandler btn.Paint, AddressOf btn_Paint
End If
btn.Enabled = False
End Sub
Private Sub btn_EnabledChanged(sender As Object, e As System.EventArgs)
Dim btn As Button = DirectCast(sender, Button)
If sender.enabled = False Then
btn.ForeColor = Color.Black
Else
btn.ForeColor = Color.Black
End If
End Sub
Private Sub btn_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs)
'Dim TextForBtn As String = "" ' << we don't use a variable because that would only be useful if we could reassign to the .Text property, but that just raises the Paint Event again causing a infinite loop.
Dim btn As Button = DirectCast(sender, Button)
If btn.Text > " " Then
btn.Tag = btn.Text ' << this allows us to redraw the text, the one issue however is the .Text property is empty from here on out.
'TextForBtn = btn.Text ' << this is part of the failed idea of using a variable and reassigning to the .Text property.
End If
btn.Text = String.Empty ' << make sure Text is not written on button as well as rendered below
Dim flags As TextFormatFlags = TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter Or TextFormatFlags.WordBreak 'center the text
TextRenderer.DrawText(e.Graphics, btn.Tag.ToString, btn.Font, e.ClipRectangle, btn.ForeColor, flags) ' << TextForBtn was replaced with btn.Tag.ToString in the second parameter
'btn.Text = TextForBtn ' << this caused an infinite loop necessitating the use of the .Tag property.
End Sub
End Module