0

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.

  • You need to override OnEnter() and OnLeave() and call this.Invalidate(). That gets the control to repaint itself when it gains and loses focus. Easy peasy. – Hans Passant Dec 10 '16 at 13:58
  • Hi @HansPassant, I am not sure how to do this. Could you please help me out. I am not able to get the e.Graphics to work inside onEnter or onLeave... – Ganesh Kamath - 'Code Frenzy' Dec 10 '16 at 14:22
  • I said "call this.Invalidate()", that does not sound much like "get the e.Graphics to work" does it? Sample code [is here](http://stackoverflow.com/a/3562449/17034). – Hans Passant Dec 10 '16 at 14:26
  • @HansPassant, this.Invalidate is not changing the borderthickeness for selection change event of group box. I might have got something wrong, could you please post an answer that i could try? – Ganesh Kamath - 'Code Frenzy' Dec 10 '16 at 18:27

1 Answers1

0

To solve this problem I used the onMouseClick event with a combination of declaring a constructor which set the initial borderColor to a different value. like so

    private Color borderColor;
    private int entered;

    public TUserControlGroupBox() {
        this.borderColor = SystemColors.ControlDark;
    }

    public Color BorderColor {
        get { return this.borderColor; }
        set { this.borderColor = value; }
    }

    public BorderState Border {
        set {
            switch (value) {
                case BorderState.Unselected:
                    this.BorderColor = Color.LightGray;
                    break;
                case BorderState.Selected:
                    this.BorderColor = Color.Black;
                    break;
                case BorderState.SelectedFirst:
                    this.BorderColor = SystemColors.ControlDark;
                    break;
            }

            this.Invalidate();
        }
    }

    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 ((this.BorderColor == Color.Black || this.borderColor == SystemColors.ControlDark) && this.entered == 1) {
            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);
    }

    protected override void OnMouseClick(MouseEventArgs e) {
        this.entered = 1;
        this.Invalidate();
        base.OnMouseClick(e);
    }

Hope this answer helps those looking for a similar solution in the future.