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. change text from form

change text from form

Scheduled Pinned Locked Moved C#
help
21 Posts 5 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.
  • M messages

    Thank for your reply OriginalGriff,but a question we didnt write any thing in the form2.in the form2 we have defined a property and insert close() method.but in the form1 we get the text from textbox and set the title of the form. is it possible to show two forms in the same time?

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #7

    You can, but you have to use events and properties to do it: think about the two as Parent and Child as I described, and look at these two tips: Transferring information between two forms, Part 1: Parent to Child[^] Transferring information between two forms, Part 2: Child to Parent[^] You will probably also want to include a Form1 event handler to handle the Form2.FormClosing Event so that you know when (if) the user closes it so you can either close Form1 as well, or open a new instance of Form2 when you need it.

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    M 1 Reply Last reply
    0
    • B BillWoodruff

      Unless there is a compelling reason to create new instances of the Forms with every click of the Button(s), then you want to create the two Forms once. There are many ways you could have the two Forms interact; the method shown here works by giving each Form a valid reference to the other Form. In the case of Form1, the "Main Form," which creates Form2: by definition, it "knows about" ... has a valid reference to ... Form2. Look at the Load EventHandler in Form1: notice that it sets the Public Property of Type Form1 in Form2 with a valid reference to Form1. Form1:

      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      }

      Form2 form2 = new Form2();
      
      private void Form1\_Load(object sender, EventArgs e)
      {
          // form2 gets a reference to this instance of Form1
          form2.referenceToForm1 = this;
      }
      
      private void button1\_Click(object sender, EventArgs e)
      {
          form2.Text = textBox1.Text;
          form2.Show();
          Hide();
      }
      

      }

      Form 2:

      public partial class Form2 : Form
      {
      public Form2()
      {
      InitializeComponent();
      }

      // holds the reference to Form1
      public Form1 referenceToForm1 { get; set; }
      
      private void button1\_Click(object sender, EventArgs e)
      {
          referenceToForm1.Text = textBox1.Text;
          referenceToForm1.Show();
          Hide();
      }
      

      }

      Note that you could get into an "undefined" state if you did this: 1. clicked the Button on form1 which would show form2, and hide form1 2. then, closed form2: at that point your Application is still running, and form1 is hidden ... so it won't show in the TaskBar, so: you are now running in "outer space," and HAL will not open the pod-bay doors. Finally, I am not sure why you are implementing this kind of functionality which I find rather strange: perhaps as a learning experiment ?

      "What Turing gave us for the first time (and without Turing you just couldn't do any of this) is he gave us a way of thinking about and taking seriously and thinking in a disciplined way about phenomena that have, as I like to say, trillions of moving parts. Until the late 20th century, nobody knew how to take seriously a machine with a trillion moving parts. It's just mind-boggling." Daniel C. Dennett

      M Offline
      M Offline
      messages
      wrote on last edited by
      #8

      well its very important to know and your code does not work when I click the button on the form2 it shows this error Object reference not set to an instance of an object

      B 1 Reply Last reply
      0
      • M messages

        I have tired as you said but it didnt work.could you write what you said?

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

        messages wrote:

        but it didnt work.

        Well that doesn't tell us anything about what you tried or what errors you got.

        messages wrote:

        could you write what you said?

        I could, but you will learn much more by trying it yourself. This is really not a difficult issue. All you are doing is adding a reference in each form to the other - approximately two or three lines of code.

        Veni, vidi, abiit domum

        M 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          You can, but you have to use events and properties to do it: think about the two as Parent and Child as I described, and look at these two tips: Transferring information between two forms, Part 1: Parent to Child[^] Transferring information between two forms, Part 2: Child to Parent[^] You will probably also want to include a Form1 event handler to handle the Form2.FormClosing Event so that you know when (if) the user closes it so you can either close Form1 as well, or open a new instance of Form2 when you need it.

          M Offline
          M Offline
          messages
          wrote on last edited by
          #10

          OriginalGriff your examples are my problem,show two forms and exchange the text,but you are solved a half of problem,is it possible to write a full code it?

          OriginalGriffO 1 Reply Last reply
          0
          • L Lost User

            messages wrote:

            but it didnt work.

            Well that doesn't tell us anything about what you tried or what errors you got.

            messages wrote:

            could you write what you said?

            I could, but you will learn much more by trying it yourself. This is really not a difficult issue. All you are doing is adding a reference in each form to the other - approximately two or three lines of code.

            Veni, vidi, abiit domum

            M Offline
            M Offline
            messages
            wrote on last edited by
            #11

            Its not possible I cant add two references in each form,compiler doesnt complie my code,I have tired. could you help richard please?

            L 1 Reply Last reply
            0
            • M messages

              OriginalGriff your examples are my problem,show two forms and exchange the text,but you are solved a half of problem,is it possible to write a full code it?

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #12

              I could - it's not exactly difficult and you have all the code you need - but how much would you learn if I did? :laugh: Seriously, it isn't difficult. You know what you need to do: Add the event code to Form2. Create an instance of Form2 in Form1, and attach a handler to the event. When you press the button, show the Form2 instance and set the text. When you press the button in Form2, raise the event. In the Form1 handler, set the Form1 Text to the Form2 property. The sample even does most of that already! So which bit is difficult for you?

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              M 1 Reply Last reply
              0
              • M messages

                Its not possible I cant add two references in each form,compiler doesnt complie my code,I have tired. could you help richard please?

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

                messages wrote:

                Its not possible

                Of course it's possible, you even have the code provided by OriginalGriff. If you really cannot understand what he has written for you then you may need to step back and work on something simpler.

                Veni, vidi, abiit domum

                1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  I could - it's not exactly difficult and you have all the code you need - but how much would you learn if I did? :laugh: Seriously, it isn't difficult. You know what you need to do: Add the event code to Form2. Create an instance of Form2 in Form1, and attach a handler to the event. When you press the button, show the Form2 instance and set the text. When you press the button in Form2, raise the event. In the Form1 handler, set the Form1 Text to the Form2 property. The sample even does most of that already! So which bit is difficult for you?

                  M Offline
                  M Offline
                  messages
                  wrote on last edited by
                  #14

                  event handler is problem,I cant use of it.is it possible without events?

                  OriginalGriffO 1 Reply Last reply
                  0
                  • M messages

                    event handler is problem,I cant use of it.is it possible without events?

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #15

                    Why can't you use it?

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    M 1 Reply Last reply
                    0
                    • M messages

                      well its very important to know and your code does not work when I click the button on the form2 it shows this error Object reference not set to an instance of an object

                      B Offline
                      B Offline
                      BillWoodruff
                      wrote on last edited by
                      #16

                      I am almost certain that you have not wired-up the Load Event of Form1, and that is why the reference to the instance of Form1, 'form1,' in the instance of Form2 is null, and you get this error message. good luck, Bill

                      "What Turing gave us for the first time (and without Turing you just couldn't do any of this) is he gave us a way of thinking about and taking seriously and thinking in a disciplined way about phenomena that have, as I like to say, trillions of moving parts. Until the late 20th century, nobody knew how to take seriously a machine with a trillion moving parts. It's just mind-boggling." Daniel C. Dennett

                      1 Reply Last reply
                      0
                      • M messages

                        Hi everyone Im going to change the text from two forms for this operation I have a textbox and button in the form1 and a textbox and a button on the form2 each time I click on the button1 it must change the title from the form2 and vice versa Im using of this code in the form1 private void button1_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); form2.Show(); new Form2().Text = textBoxform1.Text; Hide(); } in the form2 private void button1_Click(object sender, EventArgs e) { Form1 form1 = new Form1(); form1.Show(); form1.Text= textBoxform2.Text; Hide(); } but this code have a problem each time I want to change the text from forms I have to make a new instance of forms is it an another way to change

                        V Offline
                        V Offline
                        Vinh Nguyen
                        wrote on last edited by
                        #17

                        You can simply do following steps to get your code works: - In the definition of each form (Form1.cs and Form2.cs for example), declare a property of type Form and name it as TargetForm or so. - In the main function in program.cs, you initiate the two forms at once, then keep a reference between them, like:

                        var form1 = new Form1();
                        var form2 = new Form2();
                        form1.TargetForm = form2;
                        form2.TargetForm = form1;

                        then hide one of them if necessary. - Update your event handling function, for example for button1.click in the form1:

                        private void button1_Click(object sender, EventArgs e)
                        {
                        this.TargetForm.Show();
                        this.TargetForm.Text = textBoxform1.Text;
                        Hide();
                        }

                        1 Reply Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          Why can't you use it?

                          M Offline
                          M Offline
                          messages
                          wrote on last edited by
                          #18

                          Because I have to write it with properties.(btw can I declare a property of type? but how?)

                          OriginalGriffO 1 Reply Last reply
                          0
                          • M messages

                            Because I have to write it with properties.(btw can I declare a property of type? but how?)

                            OriginalGriffO Offline
                            OriginalGriffO Offline
                            OriginalGriff
                            wrote on last edited by
                            #19

                            You use a property to access the data, but you use an Event to tell the other form when you have new data for it. Think about it: the logic needs to be that the user types on one form, and presses a button. The text needs to be available via a property (or the other form can't access it) but the other form needs to know when the user has finished typing and pressed the button! So you have a property to get the data, and a event to tell you when to read it. A property is easy to define:

                            public string MyProperty
                            {
                            get { return myTextBox.Text; }
                            set { myTextBox.Text = value; }
                            }

                            Declares a property of type "string" called MyProperty, that Form1 can access:

                            Form2 f2 = new Form2();
                            f2.MyProperty = "hello!";
                            ...

                            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                            M 1 Reply Last reply
                            0
                            • OriginalGriffO OriginalGriff

                              You use a property to access the data, but you use an Event to tell the other form when you have new data for it. Think about it: the logic needs to be that the user types on one form, and presses a button. The text needs to be available via a property (or the other form can't access it) but the other form needs to know when the user has finished typing and pressed the button! So you have a property to get the data, and a event to tell you when to read it. A property is easy to define:

                              public string MyProperty
                              {
                              get { return myTextBox.Text; }
                              set { myTextBox.Text = value; }
                              }

                              Declares a property of type "string" called MyProperty, that Form1 can access:

                              Form2 f2 = new Form2();
                              f2.MyProperty = "hello!";
                              ...

                              M Offline
                              M Offline
                              messages
                              wrote on last edited by
                              #20

                              Hi OriginalGriff I exactly wrote what you said for form2 Form2 f2 = new Form2(); f2.MyProperty = "hello!"; ... but it works very well for form1 but doesnt work for form2. I dont know why C# doenst understand it.

                              OriginalGriffO 1 Reply Last reply
                              0
                              • M messages

                                Hi OriginalGriff I exactly wrote what you said for form2 Form2 f2 = new Form2(); f2.MyProperty = "hello!"; ... but it works very well for form1 but doesnt work for form2. I dont know why C# doenst understand it.

                                OriginalGriffO Offline
                                OriginalGriffO Offline
                                OriginalGriff
                                wrote on last edited by
                                #21

                                Why would you want to write that in Form2? Form2 has the property. Form1 access the property. Think of it as a car: Form1 is you, Form2 is a car, the glove box is a property of the car.

                                You can open the glove box of the car

                                In the same way

                                Form1 can access the property of Form2

                                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                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