2

当我禁用它时,我想保持按钮颜色不会变为灰色。我使用图像作为背景颜色,并将其设置ForeColor为白色。当按钮被禁用时,我想保持原样,而不是将其更改为灰色。我的代码是:

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
4

1 回答 1

4

实际上,在其模式更改期间,我们必须手动redraw使用所需的 。试试下面的代码来满足你的需要。textbuttoncolorenable

[注意:代码测试用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
于 2013-03-25T11:03:35.670 回答