I want to set the border color/style around the picturebox on and off according to different events.
Are there properties or functions that help me to achieve that aim?
This has always been what I use for that:
To change the border color, call this from the Paint
event handler of your Picturebox control:
private void pictureBox1_Paint_1(object sender, PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, pictureBox1.ClientRectangle, Color.Red, ButtonBorderStyle.Solid);
}
To change the border color dynamically, for instance from a mouseclick event, I use the Tag
property of the picturebox to store the color and adjust the Click
event of the picturebox to retrieve it from there. For example:
if (pictureBox1.Tag == null) { pictureBox1.Tag = Color.Red; } //Sets a default color
ControlPaint.DrawBorder(e.Graphics, pictureBox1.ClientRectangle, (Color)pictureBox1.Tag, ButtonBorderStyle.Solid);
The picturebox Click event, then, would go something like this:
private void pictureBox1_Click(object sender, EventArgs e)
{
if ((Color)pictureBox1.Tag == Color.Red) { pictureBox1.Tag = Color.Blue; }
else {pictureBox1.Tag = Color.Red; }
pictureBox1.Refresh();
}
You'll need using System.Drawing;
at the beginning and don't forget to call pictureBox1.Refresh()
at the end. Enjoy!