I want to close a System.Windows.Forms.Form if the user clicks anywhere outside it. I\'ve tried using IMessageFilter, but even then none of the messages are passed to PreFi
In your form's Deactivate event, put "this.Close()". Your form will close as soon as you click anywhere else in Windows.
Update: I think what you have right now is a Volume button, and inside the Click event you create an instance of your VolumeSlider form and make it appear by calling ShowDialog() which blocks until the user closes the popped-up form. In the next line you read the volume the user selected and use it in your program.
This is OK, but as you've noticed it forces the user to explicitly close the popup in order to get back to the main program. Show() is the method you really want to use here on your popup form, but Show() doesn't block which means the Click event back on your main form finishes without knowing what the new volume is supposed to be.
A simple solution is to create a public method on your main form like this:
public void SetVolume(int volume)
{
// do something with the volume - whatever you did before with it
}
Then, in your Volume button's Click event (also on the main form), you make the VolumeSlider appear like so:
VolumeSlider slider = new VolumeSlider();
slider.Show(this); // the "this" is needed for the next step
In the VolumeSlider form, as the user works the (I guess) scrollbar, you put this code in the scrollbar's ValueChanged event (I think that's what it is):
MainForm owner = (MainForm)this.Owner;
owner.SetVolume(scrollbar.Value);
And then in the VolumeSlider form's Deactivate event you would put this.Close() as mentioned above. Your form will then behave as expected.