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. Create 2 forms from button press

Create 2 forms from button press

Scheduled Pinned Locked Moved C#
7 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.
  • L Offline
    L Offline
    LiamD
    wrote on last edited by
    #1

    I want to be able to create 2 forms when I press a button. I am using the following code. { frmMy1 frm1 = new frmMy1(); frmMy2 frm2 = new frmMy2(); frm1.ShowDialog(this); frm2.ShowDialog(this); frm1.Dispose(); frm1.Dispose(); } When I run this I have to close the first form before the second form is displayed. Is there any way to get them both up at the same time. Thanks Liam

    Q D L 3 Replies Last reply
    0
    • L LiamD

      I want to be able to create 2 forms when I press a button. I am using the following code. { frmMy1 frm1 = new frmMy1(); frmMy2 frm2 = new frmMy2(); frm1.ShowDialog(this); frm2.ShowDialog(this); frm1.Dispose(); frm1.Dispose(); } When I run this I have to close the first form before the second form is displayed. Is there any way to get them both up at the same time. Thanks Liam

      Q Offline
      Q Offline
      QuietKnight
      wrote on last edited by
      #2

      There are two ways to show forms: modal and nonmodal. Modal means that the form being displayed must be closed before any further execution. This is like when you close Notepad before saving and it asks you if you would like to save. You MUST answer the question (and close the form) before you can click on the main notepad form or continue doing anything else in Notepad. This also means that your code blocks until that form is closed. In other words, frm2.ShowDialog(this); doesnt execute until frm1 is closed. The second, nonmodal, does not have these restrictions and is simply a normal form. In .NET, the Form class has two methods, Show() and ShowDialog(). Show corresponds to a nonmodal form, and ShowDialog() corresponds to a modal form. To fix your problem, simply use Show() instead of ShowDialog().

      1 Reply Last reply
      0
      • L LiamD

        I want to be able to create 2 forms when I press a button. I am using the following code. { frmMy1 frm1 = new frmMy1(); frmMy2 frm2 = new frmMy2(); frm1.ShowDialog(this); frm2.ShowDialog(this); frm1.Dispose(); frm1.Dispose(); } When I run this I have to close the first form before the second form is displayed. Is there any way to get them both up at the same time. Thanks Liam

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

        Since ShowDialog() is a blocking call that returns a value, you can only have one Dialog open at a time. If you want both forms to be open, and usable at the same time, you'll have to show both forms using their Show() method, not ShowDialog(). RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        1 Reply Last reply
        0
        • L LiamD

          I want to be able to create 2 forms when I press a button. I am using the following code. { frmMy1 frm1 = new frmMy1(); frmMy2 frm2 = new frmMy2(); frm1.ShowDialog(this); frm2.ShowDialog(this); frm1.Dispose(); frm1.Dispose(); } When I run this I have to close the first form before the second form is displayed. Is there any way to get them both up at the same time. Thanks Liam

          L Offline
          L Offline
          LiamD
          wrote on last edited by
          #4

          Ok I can see how Show instead of ShowDialog might work but I will add extra requirements. Perhaps my requirements are that I want to ensure that frm1 is always above the main form. And also frm2 is always above frm1. Even if the main display is selected I do not want frm1 and frm2 to be lost behind the main form. With a modal form this is the case but if it is modeless form then it looses that feature. How do I get around it. I know this sounds silly but can I have a modal form that itseld creates a modal form? i.e. frm1 is modal based on the main form, and also frm2 is modal based on frm2? Thanks, Liam

          L 1 Reply Last reply
          0
          • L LiamD

            Ok I can see how Show instead of ShowDialog might work but I will add extra requirements. Perhaps my requirements are that I want to ensure that frm1 is always above the main form. And also frm2 is always above frm1. Even if the main display is selected I do not want frm1 and frm2 to be lost behind the main form. With a modal form this is the case but if it is modeless form then it looses that feature. How do I get around it. I know this sounds silly but can I have a modal form that itseld creates a modal form? i.e. frm1 is modal based on the main form, and also frm2 is modal based on frm2? Thanks, Liam

            L Offline
            L Offline
            Large Data File
            wrote on last edited by
            #5

            Yes you can put a modal form on another (though in most cases it is not a correnct implementation) In any way, is it necessary that frm1 will pop up and immediatly frm2 will pop up above it? or is frm2 dependent on an action in frm1 (which is a more likely option)? If your case is the first case (i.e. frm1 should open an immediatly open frm2) - you should open frm2 from frm1's OnActivate method. just override it, call the base and open the frm2 using the ShowDialog method. However, i recommend not doing so since it does not seem the right thing to do. If you are using option 2 then there is no problem. When a certain event occurs in frm1 (such as button_click), simply open the frm2 and you will get a modal from on a modal form on your main form. Good luck, hope this answers your question.

            L 1 Reply Last reply
            0
            • L Large Data File

              Yes you can put a modal form on another (though in most cases it is not a correnct implementation) In any way, is it necessary that frm1 will pop up and immediatly frm2 will pop up above it? or is frm2 dependent on an action in frm1 (which is a more likely option)? If your case is the first case (i.e. frm1 should open an immediatly open frm2) - you should open frm2 from frm1's OnActivate method. just override it, call the base and open the frm2 using the ShowDialog method. However, i recommend not doing so since it does not seem the right thing to do. If you are using option 2 then there is no problem. When a certain event occurs in frm1 (such as button_click), simply open the frm2 and you will get a modal from on a modal form on your main form. Good luck, hope this answers your question.

              L Offline
              L Offline
              LiamD
              wrote on last edited by
              #6

              My exact situation is:- Form1 is created from a button press on the main form. Form2 is created on a timer event. When the timer elapes form1 may or may not be present. If not it should be created. If it is form2 is displayed. But Form2 must always be at the top and main form always at the bottom. If I use modeless formas and select the main form then form1 and form2 are hidden - not what I want. Thanks, Liam

              L 1 Reply Last reply
              0
              • L LiamD

                My exact situation is:- Form1 is created from a button press on the main form. Form2 is created on a timer event. When the timer elapes form1 may or may not be present. If not it should be created. If it is form2 is displayed. But Form2 must always be at the top and main form always at the bottom. If I use modeless formas and select the main form then form1 and form2 are hidden - not what I want. Thanks, Liam

                L Offline
                L Offline
                Large Data File
                wrote on last edited by
                #7

                I didn't quite follow what the requirement is, but did you consider placing frm2 together with the main form in an MDI frame? This way they can remain open while still being non-modal. You can use docking on the MDI frame to dock the frm2 to the bottom and the set the main form to fill the rest of the screen. Also if you can specify what are the roles of each form it might be easier to understand. Good Luck!

                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