Close Form when not focused
-
I have a main form with a button, when pressed opens a second form. I want to make this form close when it is out of focused. I tried with a timer checking
private void IsFocused_Tick (object sender, EventArgs e)
{
if(!this.Focused)
{
this.DialogResult = DialogResult.Cancel;
}
}But Nothing happens Can anyone tell me what i am doing wrong here?
-
I have a main form with a button, when pressed opens a second form. I want to make this form close when it is out of focused. I tried with a timer checking
private void IsFocused_Tick (object sender, EventArgs e)
{
if(!this.Focused)
{
this.DialogResult = DialogResult.Cancel;
}
}But Nothing happens Can anyone tell me what i am doing wrong here?
there is no need to use a timer. there are events raised when a form loses focus - you probably want to handle the Deactivate event and close the form in that handler.
MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
-
there is no need to use a timer. there are events raised when a form loses focus - you probably want to handle the Deactivate event and close the form in that handler.
MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
Wow... That worked like a charm... Thanks alot... M sorry but, i hadn't mentioned before, but there are two buttons on this second form, Which when clicked One opens the openfiledialog and the other folderbrowser. The problem is when i click any of the buttons it will bring up the Dialogs and so the form gets deactivated, as a result the dialog boxes also gets closed. :( Any way to work around this???
-
Wow... That worked like a charm... Thanks alot... M sorry but, i hadn't mentioned before, but there are two buttons on this second form, Which when clicked One opens the openfiledialog and the other folderbrowser. The problem is when i click any of the buttons it will bring up the Dialogs and so the form gets deactivated, as a result the dialog boxes also gets closed. :( Any way to work around this???
Create a flag that you set right before the dialog is shown and cleared when the dialog is dismissed. Check tge value of the flag in your event handler.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Create a flag that you set right before the dialog is shown and cleared when the dialog is dismissed. Check tge value of the flag in your event handler.
A guide to posting questions on CodeProject[^]
Dave KreskowiakThanks alot... It works...
-
I have a main form with a button, when pressed opens a second form. I want to make this form close when it is out of focused. I tried with a timer checking
private void IsFocused_Tick (object sender, EventArgs e)
{
if(!this.Focused)
{
this.DialogResult = DialogResult.Cancel;
}
}But Nothing happens Can anyone tell me what i am doing wrong here?