Question Changing multiple labels

Tiaan

Member
Joined
Mar 25, 2015
Messages
15
Programming Experience
1-3
I have 10 labels, with a dropdown box, the user selects 10-20, 20-30 etc, according to that choice i need to change the labels.
This is what i have, but it does not work right, any help please?

VB.NET:
 Dim selectedIndex As Integer
        selectedIndex = SelectionBox.SelectedIndex
        Dim selectedItem As Object
        selectedItem = SelectionBox.SelectedItem

        Dim st1 As String = "Num"   ' This is the Label name (Num1,Num2,etc) 
        If selectedItem = "10-20" Then
            For y As Integer = 1 To 10
                Me.Controls(st1 & y.ToString).Text = y
            Next

        ElseIf selectedItem = "20-30" Then
            For y As Integer = 1 To 10  
                 For z As Integer = 20 to 30 
                Me.Controls(st1 & y.ToString).Text = Z   
            Next
The First one works, but when i select 20-30 it makes everything 30.

Thank you
 
Your code doesn't really make sense. This is what you get when you write code without having a clear idea of what it's actually supposed to do. It's a common problem when starting out. Why would you have two loops in the second case? What are you looping through? The list of Labels, right? That's one loop. What's the second loop for?

Maybe you should explain what you actually want to happen. You have said:
...according to that choice i need to change the labels.
That's a very vague description and that's exactly why your code doesn't work. You have no clear ideas of what it's supposed do so you just pluck it out of the air. My guess is that you all you actually need to do is take the code for the first case:
            For y As Integer = 1 To 10
                Me.Controls(st1 & y.ToString).Text = y
            Next
and simply add 10 to the number you're displaying:
            For y As Integer = 1 To 10
                Me.Controls(st1 & y.ToString).Text = y + 10
            Next
That's based on my guess about what you actually want to achieve based on your vague description. If I've guessed wrongly then please provide a FULL and CLEAR description.
 
Im sorry for the confusion. That is exactly what i wanted to do, i was over complicating the problem it seems, the first loop was to gather all the Label numbers, then i created a second loop to change those numbers, didnt even think of just adding 10.

Thank you i apreciate the help.
 
Back
Top