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

    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