3

I have a WindowForm and some controls on it.

My point is that when I click button "?" on top-right of the datagridview, it will show a picture box and when I click outside the pictureBox, it must invisible.

My MainForm MainForm

MyPictureBox

enter image description here

I have searched some topics on this site, but some dont work, some work partly. Like this.

I also tried:

void pictureBox1_LostFocus(object sender, EventArgs e)
    {
        if (pictureBox1.Visible)
            pictureBox1.Visible = false;
    }

But when I click on button2, button3, ... The pictureBox wasn't invisible.

Any solution will be highly appreciated.

Community
  • 1
  • 1
anhtv13
  • 1,636
  • 4
  • 30
  • 51
  • With which events did you try it (LostFocus,Leave)? If it doesnt work I would just call on every Control that is not part of the "MyPictureBox"-Control `pictureBox1.Visible = false;`. This is a fast solution but not the best... – C0d1ngJammer Jun 05 '15 at 05:42
  • Keep this in your form load `pictureBox1.MouseLeave += (s, e) => { pictureBox1.Visible = false; }` and give a try. – Hari Prasad Jun 05 '15 at 05:45
  • Did you put break point and check out if the code is actually getting fired? Obviously if you can debug the `pictureBox1_LostFocus` the function is actually linked to the intended control. – fujiFX Jun 05 '15 at 05:47
  • Add `LostFocus` events to __DataGrid__ is also. – Behzad Jun 05 '15 at 05:57

3 Answers3

0

Add this method in designer.cs:

pictureBoxEvent this.MouseLeave += new EventHandler(pictureBox_MouseLeave);

Add this code in cs file:

private void pictureBox_MouseLeave(object sender, EventArgs e)
{
    pictureBox1.Visible = false;
}
OhBeWise
  • 5,350
  • 3
  • 32
  • 60
Kirti
  • 51
  • 2
0

I think your pictureBox1 isn't losing focus, cause it never actually GOT focused. Set it to be focused after making it visible.

0

Oh, I have encountered this before...

I was making a Label that you could double click and it would allow you to edit the Label.Text, like a TextBox. However, I was having problems hooking into the events to know when the user had clicked off the Control and wished to stop editing. I tried Control.LostFocus, and Control.Leave, but nothing. I even got frustrated/desperate and tried some silly ones like Control.Invalidated.

What I ended up having to do was subscribe to the Click event of the Form/Container/Control behind it.

However, putting the responsibility of wiring up this event into the Form that wants to use it is poor design. What you can do, however is to make the constructor to Control class require a reference to the owner/parent/container as a parameter. That way, the requirements are not hidden, they must be satisfied before you can get a object instance, and the control can wired up to the Form.Click within itself, where that logic belongs.

    private Form owner;

    public EditLabel(Form Owner)
    {
         this.owner = Owner;
         owner.Click += EndEditing;
    }
Adam White
  • 3,180
  • 1
  • 21
  • 18