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. how to get textbox value from one class to another

how to get textbox value from one class to another

Scheduled Pinned Locked Moved C#
tutorial
3 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.
  • S Offline
    S Offline
    Sr Frank
    wrote on last edited by
    #1

    public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { } } public class test { string strID; public string hello() { strID= this.txtTextBox1.Text; //here i am confused with my basic concept (can i assign value to strID from TextBox and if yes then how to get TextBox Value from form2 because I am making a new class here) return strID; } } }

    R D 2 Replies Last reply
    0
    • S Sr Frank

      public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { } } public class test { string strID; public string hello() { strID= this.txtTextBox1.Text; //here i am confused with my basic concept (can i assign value to strID from TextBox and if yes then how to get TextBox Value from form2 because I am making a new class here) return strID; } } }

      R Offline
      R Offline
      ramzg
      wrote on last edited by
      #2

      Hi, You can use C# Properties for this... Here's what u want...

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

          private void Form2\_Load(object sender, EventArgs e)
          {
              test \_test = new test();
              string strId = \_test.StrID;
          }
      }
      
      public class test
      {
          string strID = string.Empty;
      
          //This is the property that you can refer from any other class in which u have instantiated this class (test)
          public string StrID
          {
              get { return strID; }
              set { strID = value; }
          }
      
          public string hello()
          {
              strID = this.txtTextBox1.Text; //here i am confused with my basic concept (can i assign value to strID from TextBox and if yes then how to get TextBox Value from form2 because I am making a new class here)
              return strID;
          }
      }
      

      Thanks, Ram

      1 Reply Last reply
      0
      • S Sr Frank

        public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { } } public class test { string strID; public string hello() { strID= this.txtTextBox1.Text; //here i am confused with my basic concept (can i assign value to strID from TextBox and if yes then how to get TextBox Value from form2 because I am making a new class here) return strID; } } }

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        Properties work for getting information from an object instance you hold a reference to. To get the object to tell the 'parent' without it being requested, use events - see here[^].

        Dave

        If this helped, please vote & accept answer!

        Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        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