tomyforever tomyforever - 9 months ago 50
C# Question

Why my button is not showing the text?

I have a class CircularButton that extends of Button and a button, which is not showing the text, with or without a BackColor. Can't figure out what is happening:

public class CircularButton : Button {

public CircularButton(Color c, String text){
Font = new Font("Arial", 20f, FontStyle.Bold);
BackColor = c;
Text = text;
public Action<PaintEventArgs> DoPaint { get; set; }
protected override void OnPaint(PaintEventArgs e) {
if (DoPaint != null) { DoPaint(e); }

//make a new circular button, the text is just a letter.
CircularButton btn = new CircularButton(Color.Red,"A");
btn.Bounds = new Rectangle(0, 0, 50, 50);
btn.Enabled = false;
btn.DoPaint = delegate(PaintEventArgs p){
Graphics graphics = p.Graphics;
SolidBrush brush1 = new SolidBrush(SystemColors.ButtonFace);
graphics.FillRectangle(brush1, 0, 0, btn.Width, btn.Height);
SolidBrush brush2 = new SolidBrush(btn.BackColor);
graphics.FillEllipse(brush2, 0, 0, btn.Width, btn.Height);

Answer Source

You are overwriting OnPaint which would draw the text in the base button. So you have to write the text yourself:

graphics.DrawString(btn.Text, btn.Font, new SolidBrush(btn.ForeColor), x, y);