how to change the color of the picture box
-
in my app i've apicture box and when clicked on that i want to show that this picturebox is clicked by changing the color of the border, how can i do it?
-
in my app i've apicture box and when clicked on that i want to show that this picturebox is clicked by changing the color of the border, how can i do it?
Sadly enough, this control does not expose a border color, even for classes inheriting from it. Your only choice is to draw it yourself, you can for example create a class that inherits from the PictureBox and override the OnClick (or just handle it) to paint the border, it can be something like that: public class PictureBoxEx : PictureBox { protected override void OnClick(EventArgs e) { base.OnClick(e); Pen pen = new Pen(Color.Red); this.CreateGraphics().DrawRectangle(pen, base.ClientRectangle); } } This code is not entirely correct, the base.ClientRectangle rectangle is inside the real border but I'll leave it to you to compute the correct coordinates. The disadvantage of this approach is that it is not done in the "Paint event" of the control, so any redraw (caused if you ALT-TAB or just move something over it, like another window) will cause the control to redraw itself and your border will disappear. I don't know what you really want, if the idea is that it should STAY colored after the click, then override the OnClick and trigger the OnPaint after having set a boolean or so to indicate the clicked state; in the OnPaint, check this boolean and draw the border if the "clicked" state check is positive. Another disadvantage, but this was really quick code to show the idea, is that I am using a Graphics object that I create myself. In the OnPaint handler, you should use the provided Graphics object from the eventArgs, otherwise you loose Double-Buffering.
Jean-Christophe Grégoire