Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# .NET C#4.0
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,
 
string func(string abcd)
 
{ 
 
string result;
 
result  = getvalue(abcd);
 
return(result);
 
}
 

 
I will call it as
 
func("Form4.Textbox2.Text")
 

 
please help....
Posted 16-Dec-11 22:24pm
Rate this: bad
good
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();
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.
  Permalink  
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 406
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 205
3 Raul Iloc 170
4 Maciej Los 164
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 17 Dec 2011
Copyright © CodeProject, 1999-2014
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