Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Close Form when not focused

Close Form when not focused

Scheduled Pinned Locked Moved C#
question
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alfie max15
    wrote on last edited by
    #1

    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?

    L Q 2 Replies Last reply
    0
    • A alfie max15

      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?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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')

      A 1 Reply Last reply
      0
      • L Lost User

        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')

        A Offline
        A Offline
        alfie max15
        wrote on last edited by
        #3

        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???

        D 1 Reply Last reply
        0
        • A alfie max15

          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???

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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

          A 1 Reply Last reply
          0
          • D 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 Kreskowiak

            A Offline
            A Offline
            alfie max15
            wrote on last edited by
            #5

            Thanks alot... It works...

            1 Reply Last reply
            0
            • A alfie max15

              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?

              Q Offline
              Q Offline
              qbeenslee
              wrote on last edited by
              #6

              I think the codes should like this:

              private void Form1_Deactivate(object sender, EventArgs e)
              {
              this.Hide();
              }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups