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. Show hidden forms

Show hidden forms

Scheduled Pinned Locked Moved C#
question
8 Posts 3 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.
  • M Offline
    M Offline
    methodincharge
    wrote on last edited by
    #1

    I have a main form, call it A, which spawns another form B. When B spawns I want it brought to the front (easy), but if another program gets the focus, then the user switches back to A, I want B to be brought to the top if it was focused when the user switched to another program. Any easy way to do this? I have tried using the Enter/Leave/Activated/Deactivated events but it doesn't seem to work the way I think it should. If anything is unclear let me know. Thanks.

    R 1 Reply Last reply
    0
    • M methodincharge

      I have a main form, call it A, which spawns another form B. When B spawns I want it brought to the front (easy), but if another program gets the focus, then the user switches back to A, I want B to be brought to the top if it was focused when the user switched to another program. Any easy way to do this? I have tried using the Enter/Leave/Activated/Deactivated events but it doesn't seem to work the way I think it should. If anything is unclear let me know. Thanks.

      R Offline
      R Offline
      Rob Tomson
      wrote on last edited by
      #2

      can't you just use ShowDialog? that should make B be active even if you select A. Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

      M 1 Reply Last reply
      0
      • R Rob Tomson

        can't you just use ShowDialog? that should make B be active even if you select A. Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

        M Offline
        M Offline
        methodincharge
        wrote on last edited by
        #3

        Rob Tomson wrote: can't you just use ShowDialog? I need to have access to A while B is open, otherwise I would.

        S 1 Reply Last reply
        0
        • M methodincharge

          Rob Tomson wrote: can't you just use ShowDialog? I need to have access to A while B is open, otherwise I would.

          S Offline
          S Offline
          Skynyrd
          wrote on last edited by
          #4

          Its a tricky problem because u dont want the form snapping back to B when ur only activating A and not an external app. Still, i think u can do it this way (havent tried it but it looks pretty sound): Create a timer in the B Form with a low interval. 1-10 miliseconds will do. When B looses focus (deactivates), start timer and in the Tick handler check for the static member Form.ActiveForm and stop ur timer. If ActiveForm is FormA then dont do anything. If ActiveForm gives u Null then the active window is no longer part of ur app and u should set a property in ur A Form, for example SnapBackToB to true. Whenever A handles and Activate event, check for _snapBackToB and if true, activate Form B. That should do it. Oh, one other thing, when Form B activates, it should always check for SnapBackToB in form A and set it to False. GL, and if things dont work at all as I/u expect please let me know :p P.D. Its important u stop the timer after first tick because, even if it doesnt harm the algorithm, a timer running with that small interval may not let windows detect correctly ur idle app and u might have issues with power management, screensavers, etc.

          M 1 Reply Last reply
          0
          • S Skynyrd

            Its a tricky problem because u dont want the form snapping back to B when ur only activating A and not an external app. Still, i think u can do it this way (havent tried it but it looks pretty sound): Create a timer in the B Form with a low interval. 1-10 miliseconds will do. When B looses focus (deactivates), start timer and in the Tick handler check for the static member Form.ActiveForm and stop ur timer. If ActiveForm is FormA then dont do anything. If ActiveForm gives u Null then the active window is no longer part of ur app and u should set a property in ur A Form, for example SnapBackToB to true. Whenever A handles and Activate event, check for _snapBackToB and if true, activate Form B. That should do it. Oh, one other thing, when Form B activates, it should always check for SnapBackToB in form A and set it to False. GL, and if things dont work at all as I/u expect please let me know :p P.D. Its important u stop the timer after first tick because, even if it doesnt harm the algorithm, a timer running with that small interval may not let windows detect correctly ur idle app and u might have issues with power management, screensavers, etc.

            M Offline
            M Offline
            methodincharge
            wrote on last edited by
            #5

            Could you elaborate on the ActiveForm static member? Should this be in A or B, and how/where should it be set/unset? Or are you referring to ActiveControl? Thanks.

            S 1 Reply Last reply
            0
            • M methodincharge

              Could you elaborate on the ActiveForm static member? Should this be in A or B, and how/where should it be set/unset? Or are you referring to ActiveControl? Thanks.

              S Offline
              S Offline
              Skynyrd
              wrote on last edited by
              #6

              U dont set Form.ActiveForm to anything. Its a read only property so u can't set it anyways. U read its value to know which form in ur application is the Operating System's active window. If the value u read is Null that means the active window is an external window to ur application or it could be that there really is no active window. System.Windows.Forms.Form.ActiveForm[^] When ur timer in Form B (which has started when Form B has been deactivated) triggers the Tick event for the first time, the event Handler which u should include in Form B should check for the static memeber Form.ActiveForm as I posted before. After that its just a question of style. U can either make a public get/set property in either FormA (SnapBackToB) or in FormB (SnapBackToMe) or whatever. In the first case, when A activates it would: 1) Check if Form B is open 2)check its own property and if true it would give focus to Form B. On the other hand if property was in FormB, FormA would check when it activates: 1) If Form B is open 2) If FormB's SnapBackToMe is true, it would give focus to B. As I said before, this is a question of style and there are many many ways to implement this. Of course there r some details i havent touched like handling correctly what happens when Form B is closed and not only loosing focus (remember that the Deactivate event triggers when closing a form too) and u dont want ur SnapBackToX property to remain true when ur form B is closing....but u should be able to sort that out without too many problems.

              M 2 Replies Last reply
              0
              • S Skynyrd

                U dont set Form.ActiveForm to anything. Its a read only property so u can't set it anyways. U read its value to know which form in ur application is the Operating System's active window. If the value u read is Null that means the active window is an external window to ur application or it could be that there really is no active window. System.Windows.Forms.Form.ActiveForm[^] When ur timer in Form B (which has started when Form B has been deactivated) triggers the Tick event for the first time, the event Handler which u should include in Form B should check for the static memeber Form.ActiveForm as I posted before. After that its just a question of style. U can either make a public get/set property in either FormA (SnapBackToB) or in FormB (SnapBackToMe) or whatever. In the first case, when A activates it would: 1) Check if Form B is open 2)check its own property and if true it would give focus to Form B. On the other hand if property was in FormB, FormA would check when it activates: 1) If Form B is open 2) If FormB's SnapBackToMe is true, it would give focus to B. As I said before, this is a question of style and there are many many ways to implement this. Of course there r some details i havent touched like handling correctly what happens when Form B is closed and not only loosing focus (remember that the Deactivate event triggers when closing a form too) and u dont want ur SnapBackToX property to remain true when ur form B is closing....but u should be able to sort that out without too many problems.

                M Offline
                M Offline
                methodincharge
                wrote on last edited by
                #7

                Haha, for some reason when you said "static variable" I didn't put 2 and 2 together and was looking for an instance variable, which is why I couldn't find it. Thanks for the reply, I'll go about implmenting it today and I'll let you know it turns out.

                1 Reply Last reply
                0
                • S Skynyrd

                  U dont set Form.ActiveForm to anything. Its a read only property so u can't set it anyways. U read its value to know which form in ur application is the Operating System's active window. If the value u read is Null that means the active window is an external window to ur application or it could be that there really is no active window. System.Windows.Forms.Form.ActiveForm[^] When ur timer in Form B (which has started when Form B has been deactivated) triggers the Tick event for the first time, the event Handler which u should include in Form B should check for the static memeber Form.ActiveForm as I posted before. After that its just a question of style. U can either make a public get/set property in either FormA (SnapBackToB) or in FormB (SnapBackToMe) or whatever. In the first case, when A activates it would: 1) Check if Form B is open 2)check its own property and if true it would give focus to Form B. On the other hand if property was in FormB, FormA would check when it activates: 1) If Form B is open 2) If FormB's SnapBackToMe is true, it would give focus to B. As I said before, this is a question of style and there are many many ways to implement this. Of course there r some details i havent touched like handling correctly what happens when Form B is closed and not only loosing focus (remember that the Deactivate event triggers when closing a form too) and u dont want ur SnapBackToX property to remain true when ur form B is closing....but u should be able to sort that out without too many problems.

                  M Offline
                  M Offline
                  methodincharge
                  wrote on last edited by
                  #8

                  Thanks for the code. It all works ALMOST the exact way I wanted it to. For some reason if I tried to save the last active window (IE on return to the app, either make the main window the active one or the message window the active one depending on which was activated last) it would go back and forth between the two and then settle on the main window. So instead I just brought the messagewindow in front upon returning to the app (although I might change it around). Thanks again for the Time suggestion, I doubt I would have thought of it.

                  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