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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to check in form2 if a button on form1 has been clicked

How to check in form2 if a button on form1 has been clicked

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

    I have two forms. Form1 has a textbox and a button. Form2 has a label. When the button on form1 is clicked, form2 is opened and the text in the textbox in form1 is shown in the label of form2. To get the text in the textbox to appear in the label, the code I have for this is in the Form load event. In this event I want to check if the button on form 1 has been clicked. How do I do this? I can access the button (located on form1) from form 2 by firstly making this public and then using this code: ((Form1)frm1).button1. I do not know how to finish this line of code while it is in an IF statement. Lucy

    B M X A P 5 Replies Last reply
    0
    • L Lucy

      I have two forms. Form1 has a textbox and a button. Form2 has a label. When the button on form1 is clicked, form2 is opened and the text in the textbox in form1 is shown in the label of form2. To get the text in the textbox to appear in the label, the code I have for this is in the Form load event. In this event I want to check if the button on form 1 has been clicked. How do I do this? I can access the button (located on form1) from form 2 by firstly making this public and then using this code: ((Form1)frm1).button1. I do not know how to finish this line of code while it is in an IF statement. Lucy

      B Offline
      B Offline
      blackjack2150
      wrote on last edited by
      #2

      You can create a bool variable in Form1 and set it to true in the click event handler of your button. Then check it's value from your method in Form2.

      L 1 Reply Last reply
      0
      • B blackjack2150

        You can create a bool variable in Form1 and set it to true in the click event handler of your button. Then check it's value from your method in Form2.

        L Offline
        L Offline
        Lucy
        wrote on last edited by
        #3

        Sorry I'm not sure how to do that. Can you explain your answer please? Lucy

        1 Reply Last reply
        0
        • L Lucy

          I have two forms. Form1 has a textbox and a button. Form2 has a label. When the button on form1 is clicked, form2 is opened and the text in the textbox in form1 is shown in the label of form2. To get the text in the textbox to appear in the label, the code I have for this is in the Form load event. In this event I want to check if the button on form 1 has been clicked. How do I do this? I can access the button (located on form1) from form 2 by firstly making this public and then using this code: ((Form1)frm1).button1. I do not know how to finish this line of code while it is in an IF statement. Lucy

          M Offline
          M Offline
          Malcolm Smart
          wrote on last edited by
          #4

          Events and delegates. Either get Form2 to subscribe to Form1.Button's onclick event or capture the onclick event on form1 and tell Form2 it has been clicked using a delegate :- //in form1 public Event Eventhandler OnButton1Click; void form1_buttonClicked( objects sender , EventArgs args) { //handler tied to the actual button click on form 1 //do other stuff //tell any subscribers we've been clicked if (OnButton1Click != null) OnButton1Click( sender , args ); } ///form2 public void MyHandler( object sender , EventArgs args) { ///button on form1 has been clicked } //in ctor or some initialiser of Form2 form1.OnButton1Click += MyHandler;

          "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

          "I haven't spoken to my wife now for 48 hours. I don't like to interrupt her.

          1 Reply Last reply
          0
          • L Lucy

            I have two forms. Form1 has a textbox and a button. Form2 has a label. When the button on form1 is clicked, form2 is opened and the text in the textbox in form1 is shown in the label of form2. To get the text in the textbox to appear in the label, the code I have for this is in the Form load event. In this event I want to check if the button on form 1 has been clicked. How do I do this? I can access the button (located on form1) from form 2 by firstly making this public and then using this code: ((Form1)frm1).button1. I do not know how to finish this line of code while it is in an IF statement. Lucy

            X Offline
            X Offline
            Xmen Real
            wrote on last edited by
            #5

            set modifiers of your form2`s label to "public" in properties then in button`s click event ,writes these codes

            form2 frm = new form2();
            frm.label2.Text = textbox1.Text;
            frm.Show();
            //or ShowDialog

            hope it will help...

            Becoming Programmer...

            L 1 Reply Last reply
            0
            • X Xmen Real

              set modifiers of your form2`s label to "public" in properties then in button`s click event ,writes these codes

              form2 frm = new form2();
              frm.label2.Text = textbox1.Text;
              frm.Show();
              //or ShowDialog

              hope it will help...

              Becoming Programmer...

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

              I have tried that but it comes up with this error when you click the button: Additional information: Object reference not set to an instance of an object. I do not understand the other comments people have made as I am still very new to c#. Lucy

              X 1 Reply Last reply
              0
              • L Lucy

                I have tried that but it comes up with this error when you click the button: Additional information: Object reference not set to an instance of an object. I do not understand the other comments people have made as I am still very new to c#. Lucy

                X Offline
                X Offline
                Xmen Real
                wrote on last edited by
                #7

                i think you are clicking button when your textbox value is null

                Becoming Programmer...

                L 1 Reply Last reply
                0
                • X Xmen Real

                  i think you are clicking button when your textbox value is null

                  Becoming Programmer...

                  L Offline
                  L Offline
                  Lucy
                  wrote on last edited by
                  #8

                  No the error still occurs when the textbox has text in it.

                  X 1 Reply Last reply
                  0
                  • L Lucy

                    No the error still occurs when the textbox has text in it.

                    X Offline
                    X Offline
                    Xmen Real
                    wrote on last edited by
                    #9

                    you are doing some mistakes

                    Becoming Programmer...

                    P 1 Reply Last reply
                    0
                    • X Xmen Real

                      you are doing some mistakes

                      Becoming Programmer...

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

                      Yup - one of the first mistakes is making a control public. You really shouldn't make controls public because you have opened up the whole scope of what the control can do to another unrelated instance.

                      Deja View - the feeling that you've seen this post before.

                      1 Reply Last reply
                      0
                      • L Lucy

                        I have two forms. Form1 has a textbox and a button. Form2 has a label. When the button on form1 is clicked, form2 is opened and the text in the textbox in form1 is shown in the label of form2. To get the text in the textbox to appear in the label, the code I have for this is in the Form load event. In this event I want to check if the button on form 1 has been clicked. How do I do this? I can access the button (located on form1) from form 2 by firstly making this public and then using this code: ((Form1)frm1).button1. I do not know how to finish this line of code while it is in an IF statement. Lucy

                        A Offline
                        A Offline
                        Anthony Mushrow
                        wrote on last edited by
                        #11

                        Another way is like this: Add another Label to your class variables: private System.Windows.Forms.Label Form2Label; Create a method in form one like this: public void getLabel(Label label) { Form2Label = label; } Then, when you click the button in Form1 just add a bit of extra code. Form2Label.Text = this.TextBox.Text; Now when you start Form2, run the method, and pass it the label. Form1.getLabel(this.label); That should work. You'd have to try it out.

                        1 Reply Last reply
                        0
                        • L Lucy

                          I have two forms. Form1 has a textbox and a button. Form2 has a label. When the button on form1 is clicked, form2 is opened and the text in the textbox in form1 is shown in the label of form2. To get the text in the textbox to appear in the label, the code I have for this is in the Form load event. In this event I want to check if the button on form 1 has been clicked. How do I do this? I can access the button (located on form1) from form 2 by firstly making this public and then using this code: ((Form1)frm1).button1. I do not know how to finish this line of code while it is in an IF statement. Lucy

                          P Offline
                          P Offline
                          Paras Kaneriya
                          wrote on last edited by
                          #12

                          Try using this way declare a bool variable in form2 with public accessiblity like public bool clicked; and in the form load event you can check like if (clicked) { //do whatever you want to do when button1 on form1 is clicked } in form1's button1's click event write private void button1_Click(object sender, EventArgs e) { Form2 frm = new Form2(); frm.clicked = true; frm.Show(); }

                          Paras Kaneriya
                          The difference between genius and stupidity is that genius has its limits.

                          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