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 form

show form

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

    What's the different between 2 code below? 1-Form2 obj1=new Form2(); obj1.show(); 2-Form2 obj2=new Form2(); obj2.showDialog(); :doh:

    P P F _ D 6 Replies Last reply
    0
    • E Elham M

      What's the different between 2 code below? 1-Form2 obj1=new Form2(); obj1.show(); 2-Form2 obj2=new Form2(); obj2.showDialog(); :doh:

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      One shows a dialog (i.e. a modal window), the other shows a modeless window.

      I'm not a stalker, I just know things. Oh by the way, you're out of milk.

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Onyx

      1 Reply Last reply
      0
      • E Elham M

        What's the different between 2 code below? 1-Form2 obj1=new Form2(); obj1.show(); 2-Form2 obj2=new Form2(); obj2.showDialog(); :doh:

        P Offline
        P Offline
        Paladin2000
        wrote on last edited by
        #3

        Well, showdialog() causes the form to be show as a "dialog" (hence the name). That means that it will retain focus in your application: you will notice that you can't place the focus on the calling form while the dialog remains active/visible. Make sense?

        E 1 Reply Last reply
        0
        • E Elham M

          What's the different between 2 code below? 1-Form2 obj1=new Form2(); obj1.show(); 2-Form2 obj2=new Form2(); obj2.showDialog(); :doh:

          F Offline
          F Offline
          freshonlineMax
          wrote on last edited by
          #4

          In obj2.show you can move form everywhere but in another case it can move in the parent form area. I guess.. :|

          P 1 Reply Last reply
          0
          • F freshonlineMax

            In obj2.show you can move form everywhere but in another case it can move in the parent form area. I guess.. :|

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            freshonlineMax wrote:

            n obj2.show you can move form everywhere but in another case it can move in the parent form area. I guess.. :|

            You guess wrong. The second example just shows a dialog box. There's no constraint in where it can be moved - the constraint is in how you can interact with the hosting application. As it's modal, processing on the primary thread halts in the calling code until it is dismissed.

            I'm not a stalker, I just know things. Oh by the way, you're out of milk.

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Onyx

            1 Reply Last reply
            0
            • E Elham M

              What's the different between 2 code below? 1-Form2 obj1=new Form2(); obj1.show(); 2-Form2 obj2=new Form2(); obj2.showDialog(); :doh:

              _ Offline
              _ Offline
              _Erik_
              wrote on last edited by
              #6

              As an addition to what others have told you, the Form object must be disposed later if you show it with ShowDialog method, so in this case it is much better to do this:

              using (Form2 obj2 = new Form2())
              {
              obj2.ShowDialog();
              }

              1 Reply Last reply
              0
              • E Elham M

                What's the different between 2 code below? 1-Form2 obj1=new Form2(); obj1.show(); 2-Form2 obj2=new Form2(); obj2.showDialog(); :doh:

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                An additional addition to the previous good answers... ShowDialog returns a value of type DialogResult that enables you to determine the result of whatever that form was shown for - useful for custom message box type dialogs etc.

                Dave
                Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                1 Reply Last reply
                0
                • E Elham M

                  What's the different between 2 code below? 1-Form2 obj1=new Form2(); obj1.show(); 2-Form2 obj2=new Form2(); obj2.showDialog(); :doh:

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

                  Yes Both are using for showing Form. But Firt one is used for showing form as a normal form. But Second One Is Used for Showing Form as a Dialog. You can not access Normally from Mouse or Keyboard. First Case shows Non Immediate Value Required. Second Shows Must Value Required. Best Regard

                  If you can think then I Can.

                  1 Reply Last reply
                  0
                  • P Paladin2000

                    Well, showdialog() causes the form to be show as a "dialog" (hence the name). That means that it will retain focus in your application: you will notice that you can't place the focus on the calling form while the dialog remains active/visible. Make sense?

                    E Offline
                    E Offline
                    Elham M
                    wrote on last edited by
                    #9

                    so you mean that with showdialog I can access the component in form2 like textbox? what can I do with dialog?

                    P 1 Reply Last reply
                    0
                    • E Elham M

                      so you mean that with showdialog I can access the component in form2 like textbox? what can I do with dialog?

                      P Offline
                      P Offline
                      Paladin2000
                      wrote on last edited by
                      #10

                      You need to make an accessor to expose the items you need from the form. Here is an example: I created a project with two forms. Form1 (parent/main) contains a textbox and a button. Form2 (dialog) has a textbox and two buttons (OK and Cancel). In the properties of the buttons, I set the "DialogResult" to "OK" and "Cancel" respectively, then in Form2's properties, set the AcceptButton to the OK button, and the CancelButton to the Cancel button. From there, the code is simple:

                      public partial class Form1 : Form
                      {
                          public Form1()
                          {
                              InitializeComponent();
                          }
                      
                          private void button1\_Click(object sender, EventArgs e)
                          {
                              Form2 frm = new Form2();
                              if (frm.ShowDialog() == DialogResult.Cancel) return;  //Cancel was chosen, so do nothing else
                              textBox1.Text = frm.Value;
                          }
                      }
                      
                      public partial class Form2 : Form
                      {
                          public Form2()
                          {
                              InitializeComponent();
                          }
                      
                          /// <summary>
                          /// Makes the value of the textbox accessible.
                          /// </summary>
                          public string Value
                          {
                              get { return textBox1.Text; }
                          }
                      }
                      
                      E 1 Reply Last reply
                      0
                      • P Paladin2000

                        You need to make an accessor to expose the items you need from the form. Here is an example: I created a project with two forms. Form1 (parent/main) contains a textbox and a button. Form2 (dialog) has a textbox and two buttons (OK and Cancel). In the properties of the buttons, I set the "DialogResult" to "OK" and "Cancel" respectively, then in Form2's properties, set the AcceptButton to the OK button, and the CancelButton to the Cancel button. From there, the code is simple:

                        public partial class Form1 : Form
                        {
                            public Form1()
                            {
                                InitializeComponent();
                            }
                        
                            private void button1\_Click(object sender, EventArgs e)
                            {
                                Form2 frm = new Form2();
                                if (frm.ShowDialog() == DialogResult.Cancel) return;  //Cancel was chosen, so do nothing else
                                textBox1.Text = frm.Value;
                            }
                        }
                        
                        public partial class Form2 : Form
                        {
                            public Form2()
                            {
                                InitializeComponent();
                            }
                        
                            /// <summary>
                            /// Makes the value of the textbox accessible.
                            /// </summary>
                            public string Value
                            {
                                get { return textBox1.Text; }
                            }
                        }
                        
                        E Offline
                        E Offline
                        Elham M
                        wrote on last edited by
                        #11

                        Thanks you realy help me.:rose::thumbsup:

                        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