-
[2005] for loop problem
Code:
For i = 0 To 27
Dim value As Integer = CInt(Int((10 * Rnd()) + 1))
If i / 9 = 0 Then
'...
End If
Next i
If I havd the Labels, there are Label1,Label2,Label3,Label4...etc...Label27
If i / 9 = 0, then the i th of label text is "Hello", otherwise, it will show "Hi"
how can I do it?
-
Re: [2005] for loop problem
The only time the if block will be executed is when i = 0, so I don't see why a simply if/else will do. :confused:
-
Re: [2005] for loop problem
Quote:
Originally Posted by crptcblade
The only time the if block will be executed is when i = 0, so I don't see why a simply if/else will do. :confused:
but my if/else is doing in the for loop:o
-
Re: [2005] for loop problem
vb Code:
For i As Integer = 0 To 27
If i Mod 9 Then
MsgBox("Hello")
Else
MsgBox("Hi")
End If
Next
-
Re: [2005] for loop problem
Quote:
Originally Posted by shakti5385
vb Code:
For i As Integer = 0 To 27
If i Mod 9 Then
MsgBox("Hello")
Else
MsgBox("Hi")
End If
Next
but it is doing in Label, not msg box:confused:
in java, it may be done by:
Label+i=...
or
Label[i]=...
but I have created 27 labels:blush:
-
Re: [2005] for loop problem
What is the use of having ?
Won't the For Loop alone do?
-
Re: [2005] for loop problem
Quote:
Originally Posted by Hell-Lord
What is the use of having
?
Won't the For Loop alone do?
the for loop will add i from 1 to 27, then so
i/9=0:
label1's text= "HI"
label2's text="HI"
.
.
.
label9's text="Hello"
.
.
.
label18's text="Hello"
.
.
.
label27's text="Hello"
-
Re: [2005] for loop problem
If you put your 27 labels into an array then you can reference them by index
Code:
Dim Labels() As Label = {Label1, Label2, Label3, ..., Label27}
For i As Integer = 0 To 26
If (i Mod 9) = 0 Then
Labels(i).Text = "Hello"
Else
Labels(i).Text = "Hi"
End If
Next
-
Re: [2005] for loop problem
Quote:
Originally Posted by stanav
If you put your 27 labels into an array then you can reference them by index
Code:
Dim Labels() As Label = {Label1, Label2, Label3, ..., Label27}
For i As Integer = 0 To 26
If (i Mod 9) = 0 Then
Labels(i).Text = "Hello"
Else
Labels(i).Text = "Hi"
End If
Next
how can i modify their location? If I count the location, it seems quite trouble
And...Actually, I have 90 labels:blush: 27 is simple to say
-
Re: [2005] for loop problem
Quote:
Originally Posted by newpat
the for loop will add i from 1 to 27, then so
i/9=0:
label1's text= "HI"
label2's text="HI"
.
.
.
label9's text="Hello"
.
.
.
label18's text="Hello"
.
.
.
label27's text="Hello"
i/9 = 0 happens only when i = 0 where as i \ 9 = 0 happens when i < 9
The difference? The / operator is for floating point division and the \ operator is for integer division.
-
Re: [2005] for loop problem
vb Code:
'This code will not work when lable text is not label
For Each ctl As Control In Me.Controls
If TypeOf ctl Is Label Then
If (Mid(ctl.Text.ToString, 6)) Mod 9 Then
ctl.Text = "Hello"
Else
ctl.Text = "Hi"
End If
End If
Next
'This code work on the more then 27 label also so you can change it in your hand
-
Re: [2005] for loop problem
Quote:
Originally Posted by stanav
i/9 = 0 happens only when i = 0 where as i \ 9 = 0 happens when i < 9
The difference? The / operator is for floating point division and the \ operator is for integer division.
I see...After read yr atricle, now I think mod is equal to \
-
Re: [2005] for loop problem
-
Re: [2005] for loop problem
Quote:
Originally Posted by newpat
I see...After read yr atricle, now I think mod is equal to \
Nope... Mod is modulus division. That is, it'll return the remainder of an integer division
For example
5 \ 9 = 0 where as 5 Mod 9 = 5
9 \ 9 = 1 where as 9 Mod 9 = 0
-
Re: [2005] for loop problem
Quote:
I see...After read yr atricle, now I think mod is equal to \
No, Mod gives you the remainder.
-
Re: [2005] for loop problem
Quote:
Originally Posted by shakti5385
vb Code:
'This code will not work when lable text is not label
For Each ctl As Control In Me.Controls
If TypeOf ctl Is Label Then
If (Mid(ctl.Text.ToString, 6)) Mod 9 Then
ctl.Text = "Hello"
Else
ctl.Text = "Hi"
End If
End If
Next
'This code work on the more then 27 label also so you can change it in your hand
Ha, I test yr code, but it doesn't work:(
-
Re: [2005] for loop problem
Quote:
Originally Posted by newpat
Ha, I test yr code, but it doesn't work:(
What problems you get:confused:
-
Re: [2005] for loop problem
The label still "Label1", "Label2", "Label3"....
-
Re: [2005] for loop problem
Put the code on a button click it is working properly for me.
-
Re: [2005] for loop problem
Quote:
Originally Posted by shakti5385
Put the code on a button click it is working properly for me.
oh! I put it in Form_Load...I would like it when running the program, the labels text are changed
-
1 Attachment(s)
Re: [2005] for loop problem
check the file, working properly for me
-
Re: [2005] for loop problem
ya, yr program is my expectation!
Is that my amount of labels is the problem?
I have 90 of labels
-
Re: [2005] for loop problem
What is the function of this code?
(Mid(ctl.Text.ToString, 6)) Mod 9
-
Re: [2005] for loop problem
If you set a breakpoint on that line, you could then copy it, go to the Command Window (this is all in the debugger), type "?" and then paste it, hit enter, and it would display what that code evaluates to at that moment.
You could paste any part of it that you want, for instance:
"?(Mid(ctl.Text.ToString, 6)) Mod 9"
or
"?Mid(ctl.Text.ToString, 6)"
If you learn how to use breakpoints and other features, you can solve many of your problems.
-
Re: [2005] for loop problem
how can I make a break point?
re shakti5385:
I know what is the problem...because I put the labels in the groupbox, so vb may not detect it.
-
Re: [2005] for loop problem
You click to the left of the code. I have attached a pic. Now, when the program reaches that line of code, execution will come to a halt. Then, you can look at values of variables and such.
Use F11 to step through the code one line at a time, and F5 to resume execution. You can set as many breakpoints as you would like.
-
Re: [2005] for loop problem
This is a picture of the Command Window, commonly found in the bottom right corner of the IDE. You just type "?" and then whatever else afterwards and it will attempt to evaluate it.
Like:
"?5 + 3" will return 8
"?Me.Text" will return the title of your app
and so on and so on...
You can also use the Command Window when your app crashes in debug mode. It usually shows you the line that it errored on, and you can look at what could have done that (possibly an out of bounds exception on an array, thats fairly common).
-
Re: [2005] for loop problem
thx for 18experience!
now I meet a new troble by shakti5385's code
http://i128.photobucket.com/albums/p...belproblem.jpg
Code:
Dim myArr As String() = {"◕", "▲", "●", "◆", "★", "☆", "*", "♀", "↗", "→"}
Dim i As Integer = 0
Dim j As Integer = 9
Dim myRnd As New Random
Dim myVal As Integer = myRnd.Next(0, 9)
For Each ctl As Control In Me.Controls
Dim myVal1 As Integer = myRnd.Next(0, 9)
Console.WriteLine("value1: " & myVal)
Console.WriteLine("value: " & myVal1)
If TypeOf ctl Is Label Then
If (Mid(ctl.Text.ToString, 6)) Mod 9 Then
i += 1
ctl.Text = i & ": " & myArr(myVal1)
Else
ctl.Text = j & ": " & myArr(myVal)
j += 9
i += 1
End If
End If
http://i128.photobucket.com/albums/p...elproblem1.jpg
-
Re: [2005] for loop problem
You need to trace the code then you will get the solution, I think this problem is occurring due to the value of the I and J because , if IF part you are putting the value I on the label and In Else part you are putting the value of J on the Label
-
Re: [2005] for loop problem
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myArr As String() = {"◕", "▲", "●", "◆", "★", "☆", "*", "♀", "↗", "→"}
Dim i As Integer = 0
Dim myRnd As New Random
Dim myVal As Integer = myRnd.Next(0, 9)
For Each ctl As Control In Me.Controls
Dim myVal1 As Integer = myRnd.Next(0, 9)
Console.WriteLine("value1: " & myVal)
Console.WriteLine("value: " & myVal1)
If TypeOf ctl Is Label Then
If (Mid(ctl.Text.ToString, 6)) Mod 9 Then
i += 1
ctl.Text = i & ": " & myArr(myVal1)
Else
ctl.Text = i & ": " & myArr(myVal)
i += 1
End If
End If
Next
End Sub
If i dun use i and j, the num will not be repeated , but it is really confusing
http://i128.photobucket.com/albums/p...opproblem1.jpg
-
Re: [2005] for loop problem
vb Code:
If (Mid(ctl.Text.ToString, 6)) Mod 9 Then
ctl.Text = Mid(ctl.Text.ToString, 6) & ": " & myArr(myVal1)
Else
ctl.Text = Mid(ctl.Text.ToString, 6) & ": " & myArr(myVal)
End If
Use above code
-
Re: [2005] for loop problem
You are doing the old divide by 9 trick (think of a number, add the digits, subract it from the original).
What you need to do is re-sort the string order at the very beginning. Then loop through and take the mod 9. of all the numbers. If the remainder is 0 use the first char, 1 use the second char, etc...
I haven't looked at your code, but knowing what you want to do and why will help you create the program. Break it down into the steps and have fun. This is a interesting project to do.
-
Re: [2005] for loop problem
Quote:
Originally Posted by dwc_work
You are doing the old divide by 9 trick (think of a number, add the digits, subract it from the original).
What you need to do is re-sort the string order at the very beginning. Then loop through and take the mod 9. of all the numbers. If the remainder is 0 use the first char, 1 use the second char, etc...
I haven't looked at your code, but knowing what you want to do and why will help you create the program. Break it down into the steps and have fun. This is a interesting project to do.
:D U know what I am doing?
That's a really interesting program, when I finish it, I will post it to share with yours:o