Michal Hromas Michal Hromas - 2 months ago 20
C# Question

C# WindowsForms - Hide control after clicking outside of it

C# WindowsForms - Hide control after clicking outside of it



I have a picturebox (f.e. picturebox1) which is not visible as default. When I click a button (let's say button1) the picturebox1 will show up. Now -> I need the picturebox1 to become hidden again when I click outside of it (on form itself or any other control). It works the same as a contextmenu would work.

I have no idea how to do it since any "Click_Outside" event doesn't exist. Is there any simple way to do this? Thanks.

Answer

Use the LostFocus event of the control (in your case, a PictureBox control)