I'm trying to draw a border around some controls in order to highlight them, but I'm getting unexpected behaviour, in that nothing is drawn.
Here is a current event method. I've tried a few variations.

        private void checkBox1_Paint(object sender, PaintEventArgs e)
        {
            if (statechanged)
            {
                //Debug.WriteLine(checkBox1.Checked.ToString());
                statechanged = false;
                ControlPaint.DrawBorder(
                    e.Graphics,
                    RectangleToClient( new System.Drawing.Rectangle(checkBox1.Left -3,
                    checkBox1.Top -3,
                    checkBox1.ClientRectangle.Width + 6,
                    checkBox1.ClientRectangle.Height + 6)),
                    System.Drawing.Color.Red,
                    ButtonBorderStyle.Solid);
            }
        }

I'd appreciate any advice regarding what I might be doing wrong or missing.
Thank you for reading.

Recommended Answers

All 4 Replies

I have limited results with this, where a border is drawn red at left and top, but black at right and bottom.
But it disappears anyway as soon as I move mouse from control.

        private void checkBox1_Paint(object sender, PaintEventArgs e)
        {
            if (statechanged)
            {
                //Debug.WriteLine(checkBox1.Checked.ToString());
                statechanged = false;
                ControlPaint.DrawBorder(
                    e.Graphics,
                    new System.Drawing.Rectangle(
                        new System.Drawing.Point(checkBox1.ClientRectangle.Left, 
                        checkBox1.ClientRectangle.Top),
                        checkBox1.ClientRectangle.Size),
                    System.Drawing.Color.Red,
                    ButtonBorderStyle.Outset);
            }
        }

Some time ago I modified a checkbox. Here is the code snippet.
I derived a class from checkbox, perhaps you should try the same?
Let me know if it helps. :)

commented: This will be really handy +8

It does help, I was unaware you could even create a custom control, it will be much easier to have my code default in a custon control rather than each individual checkbox event.

Thank you very kindly.

Oh, and the original problem is solved, by (basically) removing the changed state test condition.

        private void checkBox1_Paint(object sender, PaintEventArgs e)
        {
                ControlPaint.DrawBorder(
                e.Graphics,
                new System.Drawing.Rectangle(
                    new System.Drawing.Point(
                        checkBox1.ClientRectangle.Left,
                        checkBox1.ClientRectangle.Top),
                    checkBox1.ClientRectangle.Size),
                checkBox1.Checked ? System.Drawing.Color.Green : System.Drawing.Color.Red,
                ButtonBorderStyle.Solid);
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.