InvalidCastException when getting Text from a Label referenced by dynamic string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Promedeus
    New Member
    • Mar 2010
    • 3

    #1

    InvalidCastException when getting Text from a Label referenced by dynamic string

    .NET Version: 3.5
    Ok, I recieve an error (System.Invalid CastException 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:
    Code:
    int c = 1;
    while (c < 251)
    {
    string k = "l" + c.ToString(); //dynamic name of Control(Label)
    object ka = Controls.Find(k, true); 
    string ct = ((Label)ka).Text;  //<<Error Occurs Here
    build = build + ct;
    c++;
    }
    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".
    Last edited by Promedeus; Jun 11 '10, 03:14 AM. Reason: Refining Detail
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    Originally posted by Promedeus
    .NET Version: 3.5
    Ok, I recieve an error (System.Invalid CastException 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:
    Code:
    int c = 1;
    while (c < 251)
    {
    string k = "l" + c.ToString(); //dynamic name of Control(Label)
    object ka = Controls.Find(k, true); 
    string ct = ((Label)ka).Text;  //<<Error Occurs Here
    build = build + ct;
    c++;
    }
    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".
    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
    Code:
    foreach(object o in Controls.Find(k, true))
    {
    st+=((Label)o).Text;
    }

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      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.

      Code:
      Label theLabel = this.Controls["myLabelName"] as Label;
      if (theLabel != null)
      {
        ...
      }
      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!

      Comment

      • ThatThatGuy
        Recognized Expert Contributor
        • Jul 2009
        • 453

        #4
        Originally posted by Promedeus
        .NET Version: 3.5
        Ok, I recieve an error (System.Invalid CastException 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:
        Code:
        int c = 1;
        while (c < 251)
        {
        string k = "l" + c.ToString(); //dynamic name of Control(Label)
        object ka = Controls.Find(k, true); 
        string ct = ((Label)ka).Text;  //<<Error Occurs Here
        build = build + ct;
        c++;
        }
        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".
        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

        Comment

        Working...