473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How can I convert String to Controls?

For example...
Textbox Name="txt1"

Code:
String str="txt1";
I want str to become a controls/Textbox.
like this "str.text"..
Aug 31 '11 #1
2 8671
GaryTexmo
1,501 Expert 1GB
You can't just cast it to a control, a string object doesn't have enough information to become a control. That said, you can use that name to find the control within a list of controls. In the case of a form, the form's Controls property allows you to look something up by name.

http://msdn.microsoft.com/en-us/libr....controls.aspx
http://msdn.microsoft.com/en-us/libr...ollection.aspx

(Specifically Item[String])

Give that a try. Make sure you check that the ContainsKey method using your control name as a parameter returns true before trying to access.
Aug 31 '11 #2
arie
64
Lets assume you have a string:
Expand|Select|Wrap|Line Numbers
  1. string str = "Some Text";
And you have a control, for example:
Expand|Select|Wrap|Line Numbers
  1. TextBox textBox1 = new TextBox();
What do you want to do, exactly?
Do you want to name your control using your string? If so, first make sure it doesn't contain any invalid characters. Which are those? I'm not sure at the moment, but the space is certainly not allowed. SO you'll have to remove it, and then assign it to your control's name, for example like that:
Expand|Select|Wrap|Line Numbers
  1. textBox1.Name = str.Replace(" ",""); 
Then, as GaryTexmo said, you can look it up using, e.g. Find, or IndexOfKey method

Find:
Expand|Select|Wrap|Line Numbers
  1. Controls [] ctab = yourContainer.Controls.Find(str.Replace(" ",""));
  2. if(ctab.Count!=0) 
  3.   Control found = ctad[0];
where yourContainer is your form or panel that the textBox1 is child of.
http://msdn.microsoft.com/en-us/libr...tion.find.aspx

Using IndexOfKey:
Expand|Select|Wrap|Line Numbers
  1. int idx = yourContainer.Controls.IndexOfKey(str.Replace(" ",""));
  2. if(idx!=-1)
  3.    Control found = yourContainer.Controls[idx];
http://msdn.microsoft.com/en-us/libr...ndexofkey.aspx

If you just want to show your string in a TextBox, assign it to your TextBox Text field:
Expand|Select|Wrap|Line Numbers
  1. textBox1.Text = str
Sep 22 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Nathan | last post by:
Is there a way to convert a string to a CipherMessage? I am calling a function that decrypts a CipherMessage and returns the value. The only problem is when I want to use an encrypted value stored...
3
by: news.hku.hk | last post by:
could you tell me how can i convet a string to integer?? #include <iostream> #include <string> using namespace std; int main(){ int integer; string buffer("123456789");
5
by: sarab | last post by:
Hi, I have a managed C++ app in which i need to convert String * to char *. How can i achive this?. regards -sarab
3
by: Petr Jakes | last post by:
Hi, I am trying to convert string to the "escaped string". example: from "0xf" I need "\0xf" I am able to do it like: a="0xf" escaped_a=("\%s" % a ).decode("string_escape") But it looks a...
5
by: XML newbie: Urgent pls help! | last post by:
function to convert string to 1 dimensional array of long in VB.Net
3
by: Ursula | last post by:
Is it possible to convert a string in a file. The problem is this: I have an object string that is a file xml and I want to pass to Deserialize function, but Deserialize function expect an object...
3
by: Shawn Ferguson | last post by:
Hello All, I'm trying to do what I would think would be simple and straightforward, but it is not. I have a 2 textbox on a form, a label, and a button, when I click the botton I want to add the...
4
by: Man4ish | last post by:
HI , I am trying to convert string into char array of characters.but facing problem. #include <iostream> #include <string> using namespace std; int main() { string t="1,2,3,4,5,6";
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
9
by: engteng | last post by:
How do I convert string to numeric in VB.NET 2003 ? Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle then not convert. Regards, Tee
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.