i have a class type UserControl since the class is very big and long with more then 800 lines i will try to add only the important parts.
i created a public text property:
private string _buttonText = "Button"; // Default text
public string ButtonText
{
get { return _buttonText; }
set
{
_buttonText = value;
Invalidate(); // Redraw the button when text changes
}
}
then in the OnPaint event calling a method name DrawButtonText:
protected override void OnPaint(PaintEventArgs e)
{
DrawButton();
DrawButtonText(e.Graphics);
base.OnPaint(e);
}
and the DrawButtonText method
private void DrawButtonText(Graphics graphics)
{
using (Font font = new Font("Arial", 12, FontStyle.Bold))
{
SizeF textSize = graphics.MeasureString(_buttonText, font);
PointF locationToDraw = new PointF((this.Width / 2) - (textSize.Width / 2), (this.Height / 2) - (textSize.Height / 2));
using (SolidBrush brush = new SolidBrush(Color.White)) // Ensure text color contrasts well with button
{
graphics.DrawString(_buttonText, font, brush, locationToDraw);
}
}
}
and then example of using it in form1 constructor:
public frmMain()
{
InitializeComponent();
Image originalImage = Image.FromFile(@"D:Csharp ProjectsScreen RecorderResourcespngimg.com - buttons_PNG152.png");
Image resizedImage = ResizeImage(originalImage, new Size(100, 100));
GlowButton.GlowButton glowButton1 = new GlowButton.GlowButton
{
ButtonText = "REC", // Set the specific text for this button
Image = resizedImage,
Size = new Size(100, 100),
Location = new Point(10, (this.ClientSize.Height - 100) / 2),
ApplyDarkEffect = true // Assuming you have this property to toggle the dark effect
};
this.Controls.Add(glowButton1);
SetButtonBehavior(glowButton1, ButtonBehavior.Default);
}
the result and problem is after running the application first time there is a text “REC” as should be on the big red button. but the REC text is also added on the all other buttons on the bottom.
and another problem is when i click the button the text REC in the big red button is gone.
the big red button is glowButton1.
i want that the “REC” text will be only on the big red glowingButton1 and if not added and set text to any other buttons don’t write the text on them too. and when clicking the button that the text will remain.