[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

Label problem

Hi all,

I have the following labels

Result1, Result2, Result3 ..... Result10

I have a loop that does some calculate based on the labels and I encountered some error

CurrentPlay is an integer

if cint(me.controls("Result" & CurrentPlay-2).text) > 2 then ....

Please help. Thanks!

1 Solution
Wayne Taylor (webtubbs)AstronautCommented:
What's the error?
If the CurrentPlay - 2 is less than 1 you will get an error as there is no label named "Result0"
You should check this integer prior to calling this line:

If CurrentPlay > 2  AndAlso Not CurrentPlay > 12 Then
    If cint(me.controls("Result" &  CurrentPlay-2).text) > 2 then ....
Mike TomlinsonMiddle School Assistant TeacherCommented:
You will also get an error if the current text in the label cannot be converted to an integer with CInt()...make sure it has a valid "input".
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

leechoonhweeAuthor Commented:
Currentplay had been checked before it enters the sub routine.

The error is raised on unable to convert string to integer which has nothing to do with the Cint but rather something to do with the syntax of

me.controls("Result" & CurrentPlay -2).text

It seems that there is something structually wrong with the syntax above.

I can however get around the prblem by using a label link instead as label links have index which I can directly reference with like


However, it will be helpful to know where is the root of the problem so that it might help others too.


Mike TomlinsonMiddle School Assistant TeacherCommented:
You could make the subtraction occur first by placing it in parenthesis:

    Me.Controls("Result" & (CurrentPlay - 2)).Text

Also, if this is WinForms and .Net 2.0 (or above) then you can make it search for a matching control so it will be found even if the control is nested several levels deep:

    Dim matches() As Control = Me.Controls.Find("Result" & (CurrentPlay - 2), True)
    If matches.Length > 0 AndAlso TypeOf matches(0) Is Label Then
        Dim lbl As Label = CType(matches(0), Label)

        ' ... do something with "lbl" ...

    End If
I think the problem is with the CINT

If the value of the label is am "empty string" it will raise the an exception.

Cannot convert string to int.

So, I think to avoid this problem you have to check whether the value of the label is convertible to Integer.

Try this code:
If Not Char.IsDigit(Me.Controls("Result" & CurrentPlay - 2).Text) Then
            MsgBox("The value of label is " & Me.Controls("Result" & CurrentPlay - 2).text & " Cannot convert to integer.)
            Exit Sub
        End If

        If CInt(Me.Controls("Result" & CurrentPlay - 2).Text) > 2 Then
            'Do your staff here...
        End If

Open in new window

I use this type of naming convention and it works fine. The only difference I see is I cast the label type.

CInt(CType(Me.Controls("Result" & (CurrentPlay - 2).ToString(), Label).Text)
This is also Option Strict On correct.

Also you have not shown us the actual error as it was asked above and this will help us understand the true issue.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now