0

Here is my code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim randVal As Integer
    Dim label As String
    Dim val As Integer
    Dim stringVal As String
    For i As Integer = 1 To 256 Step 1
        val = i
        stringVal = CStr(val)
        label = "Label" + stringVal
        randVal = CInt(Math.Floor((20 - 1 + 1) * Rnd())) + 1
        label.BackColor = Color.Green
    Next

End Sub

I get an error that string has no property BackColor.

How would I be able to edit all the strings without calling them individually?

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
Daniel Lyon
  • 1
  • 1
  • 3

4 Answers4

0

You are trying to access a property that does not exist.

The String type in Visual Basic has two properties. See here (https://msdn.microsoft.com/en-us/library/system.string_properties(v=vs.110).aspx)

If you are trying to change the background color of your label, then you need to reference your label name, which has that property.

For example: (I am assuming you want to change the text and color on your label)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  Dim randVal As Integer
  Dim labelText As String
  Dim val As Integer
  Dim stringVal As String
  For i As Integer = 1 To 256 Step 1
      val = i
      stringVal = CStr(val)
      labelText = "Label" + stringVal
      randVal = CInt(Math.Floor((20 - 1 + 1) * Rnd())) + 1
      labelName.BackColor = Color.Green
  Next
End Sub

Having said that, you can also use your label properties to change the text of your label. labelName.Text = "Label" + stringVal

Here are some references (How to change the text color. Simple?)

Also, here is a link to Label Properties (https://msdn.microsoft.com/en-us/library/system.windows.forms.label_properties(v=vs.110).aspx)

and your Buttons properties (https://msdn.microsoft.com/en-us/library/system.windows.forms.button(v=vs.110).aspx)

Community
  • 1
  • 1
mugabits
  • 1,015
  • 1
  • 12
  • 22
0

The error message is correct: there isn't a property called BackColor on a string.

There is a BackColor property on Button, however, and it looks as if you're perhaps trying to set the background color of the Button object when it's clicked. If so, then you need to get hold of the Button object before you can set the color. The event handler has made this (moderately) easy, by passing the object into your handler as the parameter "sender". The only problem is that it's sent it as an object, not as a Button, so you first have to cast it to the type you want, like this:

Dim button As Button
button = DirectCast(sender, Button)

Then later on you can set the color:

button.BackColor = Color.Green

Also, if you want to set the text of the button, you have to set it using the button.Text property:

button.Text = "What I want to see on the button"

However, you're program is hard to follow. I can't see clearly from the code why you're executing a loop, or why you're setting values like randVal that aren't being used, and so it's hard to give concrete advice.

Geoff
  • 1
  • 1
  • 2
0

You managed to set the variable label to the name of your Label control, but not to the object itself. Now you need to get that Control object from its name.

VB6:

 Me.Controls(label).BackColor = vbGreen

VB.Net:

Me.Controls(label).BackColor = Color.Green

However, you had an easier way to span you label controls and update their backgrounds:

   Dim c as Control
   For Each c In Me.Controls
       If (TypeOf c Is Label) Then c.BackColor = Color.Green
   Next c
A.S.H
  • 29,101
  • 5
  • 23
  • 50
0

If you want to reference Label1 thru Label256 in a loop, no matter which container they are in, then use Controls.Find():

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For i As Integer = 1 To 256
        Dim lbl As Label = Me.Controls.Find("Label" & i, True).FirstOrDefault
        If Not IsNothing(lbl) Then
            lbl.BackColor = Color.Green
        End If
    Next
End Sub
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40