I have 2 Buttons in my form. Both have background image and Flat style is "Flat".
On clicking TAB key, when the focus is on button, I see solid color rectangle inner, instead of solid border I want to have dotted border in the button.
When the button is clicked, some activity is going on and the button is disabled. While disabled, the button text color changes to grey. I don't want it to change the text color. I believe this is windows standard behavior, but how do I not change the text color when the button is selected?
I can't find any properties to set these points. What should I do to achieve the goal. Any help is highly appreciated.
I also created a custom button, but can't get rid of the above issues.
My custom button's paint method :
public partial class MyButton : Button {
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
// Set custom fore color
base.ForeColor = Color.FromArgb(0, 255, 254, 255);
// set the same forecolor even if the button is disabled
if (base.Enabled == false)
{
base.ForeColor = Color.FromArgb(0, 255, 254, 255);
}
} //OnPaint method ends
} // class ends
UPDATED Soluiton for (1) & (2) ADDED the following in onpaint() :
private void init()
{
base.ForeColor = Color.FromArgb(0, 255, 254, 255);
base.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 254); // Transparent border
}
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
if (base.ContainsFocus)
{
// Draw inner dotted rectangle when button is on focus
Pen pen = new Pen(Color.Gray, 3);
Point p = base.Location;
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
Rectangle rectangle = new Rectangle(4, 4, Size.Width - 8, Size.Height - 8);
ControlPaint.DrawFocusRectangle(pevent.Graphics, rectangle);
}
// Draw the string to screen
SizeF sf = pevent.Graphics.MeasureString(displayText, this.Font, this.Width);
Point ThePoint = new Point();
ThePoint.X = (int)((this.Width / 2) - (sf.Width / 2));
ThePoint.Y = (int)((this.Height / 2) - (sf.Height / 2));
//pevent.Graphics.DrawString(displayText, Font, new SolidBrush(this.ForeColor), ThePoint);
//pevent.Graphics.DrawString(displayText, Font, new SolidBrush(Color.FromArgb(0, 255, 254, 255)), ThePoint);
pevent.Graphics.DrawString(displayText, Font, new SolidBrush(Color.Black), ThePoint);
//this.Text = "";
}//OnPaint ends
// Avoids the inner solid rectangle shown on focus
protected override bool ShowFocusCues
{
get
{
return false;
}
}
This successfully draws a dotted line in my button indicating for focus. To get rid of the default solid line I overridded ShowFocusCues().
This has helped me solve 1 completely. But with point 2, not getting 100% results. I created a property "displayText" in my MyButton class. In OnPaint(), if I give ForeColor, Color.FromARGB to SolidBrush, it doesn't show up any text. But if I pass Color.Black, then it accepts it and shows the text in Black color. Why is it not accepting the ForeColor, or custom color and only accepting default Colors'? Where am I still going wrong for this simple task. I tried the same by implementing button_paint in the form, but I see same results.
Any idea where am I still going wrong ???
Focus
is the keyword you're looking for. – nothrow