Click here to Skip to main content
13,048,406 members (142,571 online)
Rate this:
Please Sign up or sign in to vote.
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.


string func(string abcd)
string result;
result  = getvalue(abcd);

I will call it as


please help....
Posted 16-Dec-11 21:24pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:
Form4 f4a = new Form4();
Form4 f4b = new Form4();
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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:
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 17 Dec 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100