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. Newbie: How to use a variable from a different form?

Newbie: How to use a variable from a different form?

Scheduled Pinned Locked Moved C#
tutorialquestion
6 Posts 3 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
    hristo1977
    wrote on last edited by
    #1

    Hi! I have a textbox where I write my namn and click OK. Then a another form loads up. Then my name should appear in a label. But I cant seem to reach my variable (name) from form1 to use it in form2. Any suggestions?

    Newbie untill I die! :-)

    A 1 Reply Last reply
    0
    • H hristo1977

      Hi! I have a textbox where I write my namn and click OK. Then a another form loads up. Then my name should appear in a label. But I cant seem to reach my variable (name) from form1 to use it in form2. Any suggestions?

      Newbie untill I die! :-)

      A Offline
      A Offline
      Andrei Ungureanu
      wrote on last edited by
      #2

      Hy, The easiest way to do what you need is to create a public static variable that contains the TextBox's text. I'll give you an example code. Suppose you have Form1 with TextBox1 and Form2 with Label2. When you click Button1 from Form1 you show Form2 with the text on the Label2.

      //in the first form
      public static string textBoxText = "";

      private void Button1_Click(object sender, EventArgs e)
      {
      textBoxText = TextBox1.Text;
      Form2 f = new Form2();
      f.ShowDialog();
      }

      //in the second form
      private void Form_Load(object sender, EventArgs e)
      {
      Label2.Text = Form1.textBoxText;
      }

      Hope it helps you.

      Do your best to be the best

      H 1 Reply Last reply
      0
      • A Andrei Ungureanu

        Hy, The easiest way to do what you need is to create a public static variable that contains the TextBox's text. I'll give you an example code. Suppose you have Form1 with TextBox1 and Form2 with Label2. When you click Button1 from Form1 you show Form2 with the text on the Label2.

        //in the first form
        public static string textBoxText = "";

        private void Button1_Click(object sender, EventArgs e)
        {
        textBoxText = TextBox1.Text;
        Form2 f = new Form2();
        f.ShowDialog();
        }

        //in the second form
        private void Form_Load(object sender, EventArgs e)
        {
        Label2.Text = Form1.textBoxText;
        }

        Hope it helps you.

        Do your best to be the best

        H Offline
        H Offline
        hristo1977
        wrote on last edited by
        #3

        public static string textBoxText = ""; that one was missing, well it was not decleared where you had put it, thanks!

        Newbie untill I die! :-)

        N 1 Reply Last reply
        0
        • H hristo1977

          public static string textBoxText = ""; that one was missing, well it was not decleared where you had put it, thanks!

          Newbie untill I die! :-)

          N Offline
          N Offline
          Nadia Monalisa
          wrote on last edited by
          #4

          Why dont you make a seperate class file of the same namespace, then declare all your global variables in that class and then access those variables from any Form of your application... i.e. class myGlobalVars { public static string textBoxContent; .......... .......... } Now access these variables from any Form like this, string getTExtBoxContent = myGlobalVars.textBoxContent;

          H 1 Reply Last reply
          0
          • N Nadia Monalisa

            Why dont you make a seperate class file of the same namespace, then declare all your global variables in that class and then access those variables from any Form of your application... i.e. class myGlobalVars { public static string textBoxContent; .......... .......... } Now access these variables from any Form like this, string getTExtBoxContent = myGlobalVars.textBoxContent;

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

            Yes, I have thougth about that...but I only have one variable...so I think its a little to much work for only one.

            Newbie untill I die! :-)

            N 1 Reply Last reply
            0
            • H hristo1977

              Yes, I have thougth about that...but I only have one variable...so I think its a little to much work for only one.

              Newbie untill I die! :-)

              N Offline
              N Offline
              Nadia Monalisa
              wrote on last edited by
              #6

              :) still better than spending hours to find out more efficient ways. :):) isn't it ? When I started learning C#, I used to use File.WriteAllText(), File.ReadAllText to communicate between forms and that was the easiest solution for me that time and my application survived without any problem. Anyway, best of luck.

              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