4

I have a winform application where I've written my own little color picker that will only display system colours. It's basically a TableLayoutPanel with a bunch of smaller panels added to it, to which I just set the background color.

Pretty simple:

enter image description here

Now I'm opening this form for with:

using (frmColourWindow colourPicker = new frmColourWindow (Cursor.Position.X, Cursor.Position.Y, findingPriority))
{
    colourPicker.ShowDialog();
    if (!colourPicker.SelectedColor.IsEmpty)
    {
        pnlColor.BackColor = colourPicker._SelectedColor;
    }                    
}

and closing it with by setting the DialogResult when the user has clicked on one of the color panels.

This all works pretty good, the only thing I can not manage to get right is by closing the form when it loses focus (E.g. when the user clicks somewhere else or starts typing). I've had a look at the Deactivate, LostFocus, and Leave events. Just can't seem to get those events to fire when I want them to. Maybe I'm missing something obvious?

Bojan B
  • 2,091
  • 4
  • 18
  • 26
Code Vader
  • 739
  • 3
  • 9
  • 26
  • When opening a Form with `ShowDialog()` you can only "use" that form until it has been closed. If I understand what you want correctly, use `Show()` instead of `ShowDialog()` then those events should fire. – Bojan B Dec 14 '16 at 13:35
  • You certainly do, select the panel and click ApplicationSettings. Press F1 if you need more help. – Hans Passant Dec 14 '16 at 13:35
  • @Bojan, how do I then get the selected color from my color picker if I use `Show()`, and then `Close()` the form programatically from inside the color picker? Because the little panel in the back must be set to that color once the form closes – Code Vader Dec 14 '16 at 13:39
  • 1
    @CodeVader there are various ways to achieve that, you can add an `OnClose` or `OnClosing` event listener on the color picker where you set the color (provided the selected color is a publicly accessible property), you could have a custom event that sends the color to the invoker form, you could pass the object for which you want to set the background as a parameter to the color picker... – Bojan B Dec 14 '16 at 13:43

2 Answers2

3

As I mentioned in the comments, when using the ShowDialog() you can only use the Dialog you have opened and thus it never looses focus, so event like Deactivate, LostFocus and Leave won't work.

You need to use the Show() command to use those event to close the opened Form.

As to addressing the issue you pointed out in the comments about assigning the color to the object. you can do the following:

Declare a public Property

Color SelectedColor {get; private set; }

In your color picker and change your using statement to this:

var colourPicker = new frmColourWindow (Cursor.Position.X, Cursor.Position.Y, findingPriority);
colourPicker.Closed += (o, args) => { pnlColor.BackColor = colourPicker.SelectedColor };
colourPicker.Show(); 

This is of course just one of many possible solutions for that.

Bojan B
  • 2,091
  • 4
  • 18
  • 26
0

You can achieve this by displaying the form with the Show() method and then using the Form.Deactivate event.

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64