Stash_Man
Stash_Man

Reputation: 69

Keep button color unchanged when it is disabled

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

Upvotes: 2

Views: 4157

Answers (1)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

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.

[Note: Code tested with 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

Upvotes: 4

Related Questions