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. help needed for passing data between forms ?

help needed for passing data between forms ?

Scheduled Pinned Locked Moved C#
helpquestion
6 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    hi all, firstly: >i defined a textbox1 on form1 passed on form2 and created form2 like this: Form2 mehmet = new Form2(textBox1.Text); mehmet.show(); >and form2's contructor : public Form2(string s) { InitializeComponent(); lblUserName.Text = s; lblLoginTime.Text = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString()).ToString("HH:mm:ss"); } till now everything is fine ;) what if i want to use that 'text' in another function or class ? do i have to assign them to different variables in constructor first ??? and what if i have to pass not just one value but many values between forms ??? > is there a more sophisticated way ? or this is the method everyone uses ? secondly: i defined that textbox1 (on form1) as public and in form2 defined something like this : Form1 x = new Form1(); lblUserName.Text = x.textBox1.Text; > gives nothing ;( lblUserName.Text is an empty string ??? cant i get the string value of textbox1 from form1 to form2 by setting it to public ??? where am i doing wrong ??? thanks in advance, bye.

    M G S U 5 Replies Last reply
    0
    • L Lost User

      hi all, firstly: >i defined a textbox1 on form1 passed on form2 and created form2 like this: Form2 mehmet = new Form2(textBox1.Text); mehmet.show(); >and form2's contructor : public Form2(string s) { InitializeComponent(); lblUserName.Text = s; lblLoginTime.Text = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString()).ToString("HH:mm:ss"); } till now everything is fine ;) what if i want to use that 'text' in another function or class ? do i have to assign them to different variables in constructor first ??? and what if i have to pass not just one value but many values between forms ??? > is there a more sophisticated way ? or this is the method everyone uses ? secondly: i defined that textbox1 (on form1) as public and in form2 defined something like this : Form1 x = new Form1(); lblUserName.Text = x.textBox1.Text; > gives nothing ;( lblUserName.Text is an empty string ??? cant i get the string value of textbox1 from form1 to form2 by setting it to public ??? where am i doing wrong ??? thanks in advance, bye.

      M Offline
      M Offline
      maryamf
      wrote on last edited by
      #2

      you must define it "Public Static" in form1 then u can use it without new form.

      1 Reply Last reply
      0
      • L Lost User

        hi all, firstly: >i defined a textbox1 on form1 passed on form2 and created form2 like this: Form2 mehmet = new Form2(textBox1.Text); mehmet.show(); >and form2's contructor : public Form2(string s) { InitializeComponent(); lblUserName.Text = s; lblLoginTime.Text = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString()).ToString("HH:mm:ss"); } till now everything is fine ;) what if i want to use that 'text' in another function or class ? do i have to assign them to different variables in constructor first ??? and what if i have to pass not just one value but many values between forms ??? > is there a more sophisticated way ? or this is the method everyone uses ? secondly: i defined that textbox1 (on form1) as public and in form2 defined something like this : Form1 x = new Form1(); lblUserName.Text = x.textBox1.Text; > gives nothing ;( lblUserName.Text is an empty string ??? cant i get the string value of textbox1 from form1 to form2 by setting it to public ??? where am i doing wrong ??? thanks in advance, bye.

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Mehmet Fatih Akbulut wrote:

        what if i want to use that 'text' in another function or class ?

        Then you see to it that the value gets there. There is nothing magical going on, the values are where you put them.

        Mehmet Fatih Akbulut wrote:

        and what if i have to pass not just one value but many values between forms ???

        Then you use more than one parameter or put the values together in a class or struct and pass that along.

        Mehmet Fatih Akbulut wrote:

        secondly: i defined that textbox1 (on form1) as public and in form2 defined something like this : Form1 x = new Form1(); lblUserName.Text = x.textBox1.Text; > gives nothing ;( lblUserName.Text is an empty string ??? cant i get the string value of textbox1 from form1 to form2 by setting it to public ??? where am i doing wrong ???

        You are creating a completely new instance of Form1. That instance will not have the data that you put in the instance of Form1 that already exists. To access the data in the existing instance, you need a reference to it. --- b { font-weight: normal; }

        1 Reply Last reply
        0
        • L Lost User

          hi all, firstly: >i defined a textbox1 on form1 passed on form2 and created form2 like this: Form2 mehmet = new Form2(textBox1.Text); mehmet.show(); >and form2's contructor : public Form2(string s) { InitializeComponent(); lblUserName.Text = s; lblLoginTime.Text = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString()).ToString("HH:mm:ss"); } till now everything is fine ;) what if i want to use that 'text' in another function or class ? do i have to assign them to different variables in constructor first ??? and what if i have to pass not just one value but many values between forms ??? > is there a more sophisticated way ? or this is the method everyone uses ? secondly: i defined that textbox1 (on form1) as public and in form2 defined something like this : Form1 x = new Form1(); lblUserName.Text = x.textBox1.Text; > gives nothing ;( lblUserName.Text is an empty string ??? cant i get the string value of textbox1 from form1 to form2 by setting it to public ??? where am i doing wrong ??? thanks in advance, bye.

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          Have a look at this[^] article.

          Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          1 Reply Last reply
          0
          • L Lost User

            hi all, firstly: >i defined a textbox1 on form1 passed on form2 and created form2 like this: Form2 mehmet = new Form2(textBox1.Text); mehmet.show(); >and form2's contructor : public Form2(string s) { InitializeComponent(); lblUserName.Text = s; lblLoginTime.Text = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString()).ToString("HH:mm:ss"); } till now everything is fine ;) what if i want to use that 'text' in another function or class ? do i have to assign them to different variables in constructor first ??? and what if i have to pass not just one value but many values between forms ??? > is there a more sophisticated way ? or this is the method everyone uses ? secondly: i defined that textbox1 (on form1) as public and in form2 defined something like this : Form1 x = new Form1(); lblUserName.Text = x.textBox1.Text; > gives nothing ;( lblUserName.Text is an empty string ??? cant i get the string value of textbox1 from form1 to form2 by setting it to public ??? where am i doing wrong ??? thanks in advance, bye.

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            Mehmet Fatih Akbulut wrote:

            > To access the data in the existing instance, you need a reference to it. how can i do this ? would you please explain a bit more ? or give a simple example ¿

            You can send the reference along when you create the form: Form2 mehmet = new Form2(this); And recieve it in the constructor: public Form2(Form1 form1) --- b { font-weight: normal; }

            1 Reply Last reply
            0
            • L Lost User

              hi all, firstly: >i defined a textbox1 on form1 passed on form2 and created form2 like this: Form2 mehmet = new Form2(textBox1.Text); mehmet.show(); >and form2's contructor : public Form2(string s) { InitializeComponent(); lblUserName.Text = s; lblLoginTime.Text = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString()).ToString("HH:mm:ss"); } till now everything is fine ;) what if i want to use that 'text' in another function or class ? do i have to assign them to different variables in constructor first ??? and what if i have to pass not just one value but many values between forms ??? > is there a more sophisticated way ? or this is the method everyone uses ? secondly: i defined that textbox1 (on form1) as public and in form2 defined something like this : Form1 x = new Form1(); lblUserName.Text = x.textBox1.Text; > gives nothing ;( lblUserName.Text is an empty string ??? cant i get the string value of textbox1 from form1 to form2 by setting it to public ??? where am i doing wrong ??? thanks in advance, bye.

              U Offline
              U Offline
              User 3370889
              wrote on last edited by
              #6

              You can use your own custom event and Delegates to achieve the same

              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