本文介绍了禁用按钮时,使其颜色保持不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使按钮颜色在禁用时不变为灰色.我正在使用图像作为背景色,并且已将 ForeColor
设置为白色.禁用按钮后,我想保持原样,不要将其更改为灰色.我的代码是:
I want to keep the button color from changing to grey when I disable it. I'm using an image for the background color and I've set the ForeColor
to white. When the button is disabled I want to keep it as it is, not having it changed to grey. My code is:
Private Sub btnItemNonTaxable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnItemNonTaxable.Click
If Shift = 0 Then
MessageBox2("Please Begin the Shift before you start the transaction.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
txtNonInventoryQuantity.Text = "1"
pnlOpenItem.Visible = True
LabelNonInventory.Text = "Non-Inventory Non-Taxable"
isOpenItem = True
chkTax1.Visible = False
chkTax1.Checked = False
txtPrice.Focus()
btnCashDrop.Enabled = False
If Not btnCashDrop.Enabled Then
btnCashDrop.Image = My.Resources.small_green
btnCash.ForeColor = Color.White
End If
推荐答案
实际上,我们必须手动使用 button
的 text
redraw
在其 enable
模式期间,所需的 color
会更改.尝试下面的代码来满足您的需求.
Actually we have to manually redraw
the text
of the button
with the needed color
, during its enable
mode gets changed. Try this following code to achieve your need.
[注意:代码已通过 IDE
]
Private Sub Button1_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.EnabledChanged
Button1.ForeColor = If(sender.enabled = False, Color.Blue, Color.Red)
End Sub
Private Sub Button1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Button1.Paint
Dim btn = DirectCast(sender, Button)
Dim drawBrush = New SolidBrush(btn.ForeColor)
Dim sf = New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
Button1.Text = String.Empty
e.Graphics.DrawString("Button1", btn.Font, drawBrush, e.ClipRectangle, sf)
drawBrush.Dispose()
sf.Dispose()
End Sub
这篇关于禁用按钮时,使其颜色保持不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!