I can’t seem to make DrawBorder to work when passing a new rectangle object to it:
private void answered_choice_1_paint(object sender, PaintEventArgs e)
{
Size s = new Size(Math.Max(answered_choice_1.Height, icon_correct.Height) + 4, answered_choice_1.Width + 22 + this.default_margin + 4);
Point p = new Point(answered_choice_1.Location.X - 22 - this.default_margin - 2, answered_choice_1.Location.Y - 2);
Rectangle r = new Rectangle(p, s);
if (icon_correct.Location.Y == answered_choice_1.Location.Y)
{
ControlPaint.DrawBorder(e.Graphics, r, Color.Green, ButtonBorderStyle.Solid);
}
}
However, passing a label’s rectangle works:
private void answered_choice_1_paint(object sender, PaintEventArgs e)
{
if (icon_correct.Location.Y == answered_choice_1.Location.Y)
{
ControlPaint.DrawBorder(e.Graphics, answered_choice_1.DisplayRectangle, Color.Green, ButtonBorderStyle.Solid);
}
}
As you can see from the code, my intent is to draw a rectangular border around the answered_choice_1 label and icon_correct pictureBox, so the second code excerpt does draw a rectangle but I want to draw the rectangle from the first excerpt.
Edit:
I’ve narrowed it down to this:
int x,y;
x = answered_choice_1.Location.X - 22 - this.default_margin - 2;
y = answered_choice_1.Location.Y - 2;
Point p = new Point(x, y);
Using the debugger I’ve found out that answered_choice_1.Location.Y - 2 evaluates to 210 buy y gets the value 0; This is very strange but consistent: if I call a different constructor for the Rectangle r, I get the same outcome.
Any further help would be appreciated.
Second Edit The edit before was wrong, although that’s the data that I saw in the Visual Studio IDE. Humberto’s comment gave me the final clue to what was going on, and I’ve approved his answer.
I think you’re trying to paint a border around a pair of controls: an icon aligned to the left of a label. Is this the case?
If so, your painting code has a problem. It’s trying to use the “surface” (Graphics instance) of
answered_choice_1to paint outside its area. It won’t work.Instead, you can place the icon and the label inside a Panel, then paint the panel’s border whenever you need. Somewhat like you already did, but referring to
panel_1instead ofanswered_choice_1:Alternatively, you can assign a FixedSingle border style to the panel, but AFAIK the border color will be system defined.