I have a UserControl (Groupbox) with the following onPaint method:
protected override void OnPaint(PaintEventArgs e) {
Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
Rectangle borderRect = e.ClipRectangle;
borderRect.Y += tSize.Height / 2;
borderRect.Height -= tSize.Height / 2;
ControlPaint.DrawBorder(e.Graphics, borderRect, this.borderColor, ButtonBorderStyle.Solid);
Rectangle textRect = e.ClipRectangle;
textRect.X += 6;
textRect.Width = tSize.Width;
textRect.Height = tSize.Height;
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
}
When the user selects this control, I want to change the border to be darker, so I tried the following logic:
protected override void OnPaint(PaintEventArgs e) {
Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
Rectangle borderRect = e.ClipRectangle;
borderRect.Y += tSize.Height / 2;
borderRect.Height -= tSize.Height / 2;
if (ContainsFocus) {
ControlPaint.DrawBorder(e.Graphics, borderRect,
this.borderColor, 4, ButtonBorderStyle.Solid,
this.borderColor, 4, ButtonBorderStyle.Solid,
this.borderColor, 4, ButtonBorderStyle.Solid,
this.borderColor, 4, ButtonBorderStyle.Solid);
} else {
ControlPaint.DrawBorder(e.Graphics, borderRect, this.borderColor, ButtonBorderStyle.Solid);
}
Rectangle textRect = e.ClipRectangle;
textRect.X += 6;
textRect.Width = tSize.Width;
textRect.Height = tSize.Height;
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
}
My control seems to be continuously entering else even when I select my user control. Any help in this direction will be appreciated.