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. Forms

Forms

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

    I have two forms. With a button on each. The click event of the button on the first form is as follows

    Form2 form2;
    private void button1_Click(object sender, EventArgs e)
    {
    if (form2 == null)
    {
    form2 = new Form2();
    form2.Show();
    }
    }

    I want the button on the second form to bring back the first form on top with the BringToFront() method. How do I access the first form as there is no variable for it.

    N L H N 4 Replies Last reply
    0
    • H humayunlalzad

      I have two forms. With a button on each. The click event of the button on the first form is as follows

      Form2 form2;
      private void button1_Click(object sender, EventArgs e)
      {
      if (form2 == null)
      {
      form2 = new Form2();
      form2.Show();
      }
      }

      I want the button on the second form to bring back the first form on top with the BringToFront() method. How do I access the first form as there is no variable for it.

      N Offline
      N Offline
      netJP12L
      wrote on last edited by
      #2

      try Form2.TopMost

      H 1 Reply Last reply
      0
      • H humayunlalzad

        I have two forms. With a button on each. The click event of the button on the first form is as follows

        Form2 form2;
        private void button1_Click(object sender, EventArgs e)
        {
        if (form2 == null)
        {
        form2 = new Form2();
        form2.Show();
        }
        }

        I want the button on the second form to bring back the first form on top with the BringToFront() method. How do I access the first form as there is no variable for it.

        L Offline
        L Offline
        Le centriste
        wrote on last edited by
        #3

        If you modify the form2 constructor to take a Form instance that would be form1 and keep it in your form2 instance, for further use.

        H 1 Reply Last reply
        0
        • N netJP12L

          try Form2.TopMost

          H Offline
          H Offline
          humayunlalzad
          wrote on last edited by
          #4

          thanx this solves the particular problem. but I would like to know if there is anyway to get a hold of form1 from form2.

          1 Reply Last reply
          0
          • L Le centriste

            If you modify the form2 constructor to take a Form instance that would be form1 and keep it in your form2 instance, for further use.

            H Offline
            H Offline
            humayunlalzad
            wrote on last edited by
            #5

            How would I pass an instance of form1 as there is no variable for it. It is being initialized in the Program.cs

            Application.Run(new form1())

            method.

            E L 2 Replies Last reply
            0
            • H humayunlalzad

              How would I pass an instance of form1 as there is no variable for it. It is being initialized in the Program.cs

              Application.Run(new form1())

              method.

              E Offline
              E Offline
              Ennis Ray Lynch Jr
              wrote on last edited by
              #6

              Look up this

              Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
              Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
              Most of this sig is for Google, not ego.

              1 Reply Last reply
              0
              • H humayunlalzad

                How would I pass an instance of form1 as there is no variable for it. It is being initialized in the Program.cs

                Application.Run(new form1())

                method.

                L Offline
                L Offline
                Le centriste
                wrote on last edited by
                #7
                public Form2(Form parentForm)
                {
                }
                
                
                Form2 form2;
                private void button1_Click(object sender, EventArgs e)
                {           
                    if (form2 == null)            
                    {
                        form2 = new Form2(this);                
                        form2.Show();            
                    }        
                
                }
                
                H 1 Reply Last reply
                0
                • L Le centriste
                  public Form2(Form parentForm)
                  {
                  }
                  
                  
                  Form2 form2;
                  private void button1_Click(object sender, EventArgs e)
                  {           
                      if (form2 == null)            
                      {
                          form2 = new Form2(this);                
                          form2.Show();            
                      }        
                  
                  }
                  
                  H Offline
                  H Offline
                  humayunlalzad
                  wrote on last edited by
                  #8

                  Thanx that helped

                  L 1 Reply Last reply
                  0
                  • H humayunlalzad

                    Thanx that helped

                    L Offline
                    L Offline
                    Le centriste
                    wrote on last edited by
                    #9

                    Anytime :)

                    1 Reply Last reply
                    0
                    • H humayunlalzad

                      I have two forms. With a button on each. The click event of the button on the first form is as follows

                      Form2 form2;
                      private void button1_Click(object sender, EventArgs e)
                      {
                      if (form2 == null)
                      {
                      form2 = new Form2();
                      form2.Show();
                      }
                      }

                      I want the button on the second form to bring back the first form on top with the BringToFront() method. How do I access the first form as there is no variable for it.

                      H Offline
                      H Offline
                      Hum Dum
                      wrote on last edited by
                      #10

                      humayunlalzad wrote:

                      Form2 form2; private void button1_Click(object sender, EventArgs e) { if (form2 == null) { form2 = new Form2(); form2.Show(); } }

                      this code creates a new instance if form2 is not already opened, But what to do if form2 is opened and then closed by user(by X button of form2). i tried in else part of your block like this else { form2.BringToFront(); form2.Activate(); } but its not working. I think when user close the form2 it got disposed, but form2 instance not set to null. How to solve this? thanks and regards.

                      H 1 Reply Last reply
                      0
                      • H humayunlalzad

                        I have two forms. With a button on each. The click event of the button on the first form is as follows

                        Form2 form2;
                        private void button1_Click(object sender, EventArgs e)
                        {
                        if (form2 == null)
                        {
                        form2 = new Form2();
                        form2.Show();
                        }
                        }

                        I want the button on the second form to bring back the first form on top with the BringToFront() method. How do I access the first form as there is no variable for it.

                        N Offline
                        N Offline
                        nelsonpaixao
                        wrote on last edited by
                        #11

                        listen, i dont know what you are trying to achieve but use the mdi forms parent/child method instead in the future. i dont see the purpose of using many forms (or use usercontrols like pages). :)

                        nelsonpaixao@yahoo.com.br trying to help & get help

                        H 1 Reply Last reply
                        0
                        • H Hum Dum

                          humayunlalzad wrote:

                          Form2 form2; private void button1_Click(object sender, EventArgs e) { if (form2 == null) { form2 = new Form2(); form2.Show(); } }

                          this code creates a new instance if form2 is not already opened, But what to do if form2 is opened and then closed by user(by X button of form2). i tried in else part of your block like this else { form2.BringToFront(); form2.Activate(); } but its not working. I think when user close the form2 it got disposed, but form2 instance not set to null. How to solve this? thanks and regards.

                          H Offline
                          H Offline
                          humayunlalzad
                          wrote on last edited by
                          #12

                          Yes you are rt. Thanx for pointing out the prob. I tried to set form2 to null in the form2 closing event, but I dont why it's not working. but this works

                          private void button1_Click(object sender, EventArgs e)
                          {
                          if (form2 == null)
                          {
                          form2 = new Form2(this);
                          form2.Show();
                          }
                          else if (form2.IsDisposed)
                          {
                          form2 = new Form2(this);
                          form2.Show();
                          }
                          else
                          {
                          form2.Focus();
                          }
                          }

                          The only problem is that if I want to work on form2 and close form1, form2 also closes since form2.Show() had been called. Is there a way to keep form2 running even after form1 has been closed Guys Help

                          1 Reply Last reply
                          0
                          • N nelsonpaixao

                            listen, i dont know what you are trying to achieve but use the mdi forms parent/child method instead in the future. i dont see the purpose of using many forms (or use usercontrols like pages). :)

                            nelsonpaixao@yahoo.com.br trying to help & get help

                            H Offline
                            H Offline
                            humayunlalzad
                            wrote on last edited by
                            #13

                            Its just about understanding the mechanism. But thanx anyways.

                            N 1 Reply Last reply
                            0
                            • H humayunlalzad

                              Its just about understanding the mechanism. But thanx anyways.

                              N Offline
                              N Offline
                              nelsonpaixao
                              wrote on last edited by
                              #14

                              listen, if you are studying that method ok, go ahead, but like i said if you are trying to build some kind of application and you are thinking on using that method...drop it :laugh: i create applications using a single form!!! :) then a fill the form with lots of usercontrols that are pages. Or you can use mdi parent/child form method.

                              nelsonpaixao@yahoo.com.br trying to help & get help

                              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