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. Creating child window

Creating child window

Scheduled Pinned Locked Moved C#
question
6 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.
  • S Offline
    S Offline
    S O S
    wrote on last edited by
    #1

    How can I create a form as a child window of another form?

    F 1 Reply Last reply
    0
    • S S O S

      How can I create a form as a child window of another form?

      F Offline
      F Offline
      Furty
      wrote on last edited by
      #2

      Assuming your child form is called Form2, and this code is being called from another form: Form2 form2 = new Form2(); form2.Parent = this; // form2 is now a child of this form // show form 2 Or, if Form2 is to be displayed as a dialog box: Form2 form2 = new Form2(this) // show form 2

      S 1 Reply Last reply
      0
      • F Furty

        Assuming your child form is called Form2, and this code is being called from another form: Form2 form2 = new Form2(); form2.Parent = this; // form2 is now a child of this form // show form 2 Or, if Form2 is to be displayed as a dialog box: Form2 form2 = new Form2(this) // show form 2

        S Offline
        S Offline
        S O S
        wrote on last edited by
        #3

        Well, method 1 gives me:

        An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll

        Additional information: Cannot add a top level control to a control.

        And neither my own form nor the System.Windows.Forms.Form class has a (Form) constructor...

        A 1 Reply Last reply
        0
        • S S O S

          Well, method 1 gives me:

          An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll

          Additional information: Cannot add a top level control to a control.

          And neither my own form nor the System.Windows.Forms.Form class has a (Form) constructor...

          A Offline
          A Offline
          Arun Bhalla
          wrote on last edited by
          #4

          Set the new Form's TopLevel property to false before you make it Visible or Show() it.

          S 1 Reply Last reply
          0
          • A Arun Bhalla

            Set the new Form's TopLevel property to false before you make it Visible or Show() it.

            S Offline
            S Offline
            S O S
            wrote on last edited by
            #5

            Thanks! That worked. However, the window is created with the WS_CHILD style, which makes it be "inside" the main window. This I do not want. How can I make it not use the WS_CHILD style, but still have the main window as its parent? I don't see any place to specify the window styles manually...

            A 1 Reply Last reply
            0
            • S S O S

              Thanks! That worked. However, the window is created with the WS_CHILD style, which makes it be "inside" the main window. This I do not want. How can I make it not use the WS_CHILD style, but still have the main window as its parent? I don't see any place to specify the window styles manually...

              A Offline
              A Offline
              Arun Bhalla
              wrote on last edited by
              #6

              Hmm, also try TopMost = true; in addition to TopLevel = false;

              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