禁用时保持按钮颜色不变

时间:2013-03-25 08:26:05

标签: vb.net winforms button

我想在禁用它时将按钮颜色保持变为灰色。我正在使用图像作为背景颜色,并将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

1 个答案:

答案 0 :(得分:3)

实际上,在redraw模式被更改期间,我们必须手动text button color enable所需的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 进行代码测试]

{{1}}