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