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?
Set your image to the BackgroundImage property. Set the BackgroundImageLayout to Center . Change the BackColor property to the color you want the border to be. Now resize the PictureBox enough to show the back color, which will now visually act like a border.
Typically the PictureBox is used to display graphics from a bitmap, metafile, icon, JPEG, GIF, or PNG file. Set the Image property to the Image you want to display, either at design time or at run time.
To move the image, click the move button, click on the image, keep hold the clicked mouse button and drag.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With