Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have string "Form4.Textbox2.Text" and i want to do something that reads this strings and gets value from specified location.

So my code will read string abcd which contains "Form4.Textbox2.Text" and will try to fetch value of Form4.Textbox2.Text.

suppose,

C#
string func(string abcd)

{ 

string result;

result  = getvalue(abcd);

return(result);

}




I will call it as

C#
func("Form4.Textbox2.Text")




please help....
Posted

That is'nt going to work: the TextBox2 reference can't be static, so you would have to specify which instance of Form4 you are talking about. Since they are all created dynamically in memory you can't easily search for them, particularly as Windows itself does not know the instances name - it works with the Window Handle - which you can get from the Window Title, but not from the instance name.
The instance name I am talking about here is:
C#
Form4 f4a = new Form4();
f4a.Show();
Form4 f4b = new Form4();
f5b.Show();
You can't even access they instances via the names f4a and f4b outside the method that created them unless they are class level variables, and even then you need you know which instance of the parent class you are referring to.

What are you trying to do, that you think this is the best way to proceed? There may be a better way.
 
Share this answer
 
First of all, where are you trying to read the value. If the code is located on the same form as the text box you can simply refer to the instance. For example something like:
C#
string val = this.Textbox2.Text;

It looks a bit that you're trying to refer to the class Form4, not the instance. If that's correct and the code you're running is outside the Form4 class, store the instance somewhere (or pass it as a parameter to proper method) and using that instance get the value of the text box.

However, note that referring to form's objects (TextBox2) outside the form is a bad habit, so consider for example creating a property to the form which returns the value when needed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900