473,385 Members | 1,343 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,385 software developers and data experts.

InvalidCastException when getting Text from a Label referenced by dynamic string

.NET Version: 3.5
Ok, I recieve an error (System.InvalidCastException was unhandled. Message="Unable to cast object of type 'System.Windows.Forms.Control[]' to type 'System.Windows.Forms.Label'.") when trying to get Text from a Label referenced by a dynamicly built string.

Here's my situation; I have an array of 250 labels named l1 - l250. What I want to do is loop through them using this while statement:
Expand|Select|Wrap|Line Numbers
  1. int c = 1;
  2. while (c < 251)
  3. {
  4. string k = "l" + c.ToString(); //dynamic name of Control(Label)
  5. object ka = Controls.Find(k, true); 
  6. string ct = ((Label)ka).Text;  //<<Error Occurs Here
  7. build = build + ct;
  8. c++;
  9. }
and get the text value of each to build a string named build.

I don't get any build errors, just this while debuging.

While debuging I can go down to view my local variables. When looking through these, I can view the contents of object ka; it does contain the correct Text value of the correct Label I want to "access". I just don't understand how to get there. The text value is listed under "[0]" which is the only subcatagory for "ka".
Jun 11 '10 #1
3 2057
ThatThatGuy
449 Expert 256MB
@Promedeus
If it would have been Object to Label then its acceptable...
But an Object[] to a single Label is unacceptable...

Your Control.Find() method is getting moer then one label...

so you can loop in a collection like this
Expand|Select|Wrap|Line Numbers
  1. foreach(object o in Controls.Find(k, true))
  2. {
  3. st+=((Label)o).Text;
  4. }
  5.  
Jun 11 '10 #2
GaryTexmo
1,501 Expert 1GB
I believe you can also get the object directly by name. Additionally, you can cast it directly when you retrieve it and then perform a null check on it, as such.

Expand|Select|Wrap|Line Numbers
  1. Label theLabel = this.Controls["myLabelName"] as Label;
  2. if (theLabel != null)
  3. {
  4.   ...
  5. }
The "as" operator will attempt to cast the object to the desired type. If it can't, it will return null, otherwise it will return the properly cast object.

With the code you've got now, it's entirely possible that you may have an object named say, "l12" that happens to be a TextBox due to being misnamed. Therefore your current code would throw an exception.

Hope that helps!
Jun 11 '10 #3
ThatThatGuy
449 Expert 256MB
@Promedeus
Or Better you can give the labels a common Tag...

Under the Tag property of the Label...
And then retrieve all labels with the tag name..

That's a better option... by probably a LINQ query
Jun 14 '10 #4

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

Similar topics

2
by: FrodoBaggins | last post by:
Dear Team, I am trying to use stylesheets on a web form. Initially, I created a folder named 'styles' within the WebUI project. In this I created the following stylesheet named...
17
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart,...
11
by: jcrouse | last post by:
I’m using VB .Net 2003. I am reading a file into memory. Here is a sniplet of the file ” section. I have the code returning what character the string “” starts at but don’t know the...
24
by: Sillaba atona | last post by:
I use this code to read dynamic string: char *s1; ....... puts("Inserire una stringa: "); while((*s1++=getchar())!='\n'); *s1='\0'; The compilation (ANSI C) is OK but I receive an error...
5
by: Yohan Blurp | last post by:
Hi, Here is sample page to show you the problem : <html><body> <form action="/cgi-bin/test.cgi" method="post"> Data Path : <input type="text" size="50" value="C:\Test Data\May 2007.xls"...
3
by: bhanubalaji | last post by:
hi, I am unable to disable the text(label) in javascript..it's working fine with IE,but i am using MOZILLA.. can any one help regarding this.. What's the wrong with my code? I am...
2
damonreid
by: damonreid | last post by:
Is it possible to pass a field to a text label when a form loads? I have tried Private Sub Form_Load() Dim textName As String textName = "Text Edit Text" Me.Text29 =...
6
by: dgleeson3 | last post by:
Hello All I have VB code (.Net 2005) reading from an SQL server 2005 database. Im getting InvalidCastException when doing reader.GetInt32(0) Im simply reading an int from a simple database. It...
1
by: kenneth6 | last post by:
Windows form app: 1. how to get text label updated? 2. how to get text label assigned with a variable value (integer / string)?
4
by: Michael Munch | last post by:
Hi I want to read the value of af text-field, create dynamic, in a form. Se below a small test-site to do that (but readning fails): I use the function Test_Read for reading the value from the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.