Click here to Skip to main content
15,917,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi i develop a winform application and in this application i use buttons Now i want to change Only the back color of Button Text not a whole Button Background.

What I have tried:

i dont have any idea how can i do this plese help me any one
Posted
Updated 25-Oct-19 11:44am

Use Graphics.DrawString() in combination with Graphics.FillRectangle()
See: Graphics.DrawString Method (System.Drawing) | Microsoft Docs[^]
This can be done in the OnPaint() event of a custom button (which inherits from Button), here is some untested code:
using System.Drawing;

protected override void OnPaint(PaintEventArgs pe)
{
        base.OnPaint(pe);
        Brush brush = new SolidColorBrush(Color.Red));
        pe.Graphics.FillRectangle (brush, ClientRectangle);
        brush.Dispose();
}

Also see: Control.ClientRectangle Property (System.Windows.Forms) | Microsoft Docs[^]

Another way would be to use TextRenderer, see example here: c# - Graphics.DrawString highlight specific words - Stack Overflow[^]
 
Share this answer
 
v7
As Rick has pointed out, you're going to have to draw the Button text yourself. Text on controls don't have a background color. The background is the control itself.

You should probably create a new class that inherits from Button and add the properties, and drawing code to the new class. Don't try to do this at the form level.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900