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; } } }
How this is related with ASP.NET ? This is your Windows Application ..
Cheers ! Abhijit Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.
-
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; } } }
Create a static class with global var, in you form 2, asign the value of text box to the var, you can use the value anywhere you want by refrencing the class and the varibale. hope this helps.
-
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; } } }
What do you intend to do with the test class? (choose better names like MyTestClass so you don't accidentally use reserved words) You should create a public property in your MyTestClass for the strID. (Tip: type prop and hit tab twice). Create an instance of your MyTestClass in the form load of Form2 and then you can set the TextChanged event of txtTextBox1 to set the value of that property. You might want to do some reading on object oriented programming. From what you have written it looks like you are used to procedural programming. Try this one C# Tutorial - An Object Oriented Approach to Programming[^]
-
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) { test t1 = new test(); //Create an instance of test class. t1.hello(textbox1.text); } } public class test { string strID; public string hello(string message) { strID= message; //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; } } }
jaypatel512
-
public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { test t1 = new test(); //Create an instance of test class. t1.hello(textbox1.text); } } public class test { string strID; public string hello(string message) { strID= message; //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; } } }
jaypatel512
the question is not clear what exacly do u want to do, do you have two forms, or are u requesting the valu from the same form. you can use a command button and put your code in the click event of the button. hope this helps.