0

I've multiples CheckBoxes named "CB_0", "CB_1", "CB_2" and so far... I'm trying to change their "Checked" state, but "Controls" doesn't recognize the "Checked" property.

My line of code is like this:

Dim i As Integer = 0
Controls($"CB_{i}").Checked = True

It doesn't works, but if I use this:

CB_0.Checked = True

It works, how can I fix that?

Note:

If I try to change it text, it works:

Dim i As Integer = 0
Controls($"CB_{i}").Text = "Hello"
4
  • 4
    You need to cast it. Ctype(Controls($"CB_{i}"), Checkbox).Checked Commented Jun 27, 2019 at 18:05
  • You could use an array and add the name of the controls to the array and then manipulate them from there. Commented Jun 27, 2019 at 18:07
  • 1
    If you used the extension method I gave you in your other question, you would just use the overload with the generic Me.ChildControls(Of CheckBox)().Single(Function(c) c.Name = $"CB_{i}")).Text = "Hello" Commented Jun 27, 2019 at 18:58
  • Yes! But I'm new in VB and in programming, I tried to understand this line of code but I couldn't Commented Jun 27, 2019 at 19:53

1 Answer 1

1

Is this what your looking for?

    Dim i As Integer = 0
    CType(Controls($"CB_{i}"), CheckBox).Checked = True
Sign up to request clarification or add additional context in comments.

2 Comments

Yes! Thanks a lot.
Or DirectCast

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.