For Next Loop - Increase Variable - vb.net

I have a basic for next loop.
For x = 1 to 100
test = Test +110
if test > 500
counter = Counter +1
end if
MsgBox (thisisatest)
Next
What i'd like to do, increase thisisatest by 1 each time the counter increases. For example.
If counter = '1' it should be 'thisisatest1'
If Counter = '2' it should be 'thisisatest2'
If counter = '3' it should be 'thisisatest3'

For x = 1 to 100
test = Test +110
if test > 500
counter = Counter +1
end if
MsgBox (thisisatest+counter)
Next

you can increase your variable thisisatest by 1 each time the counter increases as below:
For x = 1 to 100
test = Test +110
if test > 500
counter = Counter +1
thisisatest &= x
end if
MsgBox (thisisatest)
Next

Related

I want to pop up a MsgBox when counter is 3, 6, 9, 12.......99

I want to pop up a MsgBox when counter is 3, 6, 9, 12.......99.
Following code need to be reparied.
Dim Counter As Integer
Do While Counter Is threefold
MsgBox("Hello")
Counter = Counter + 1
Loop
You have to use the Mod Operator.
I don't know what is threefold, but you can test this loop :
For i As Integer = 0 To 99
If i > 0 Then
If i Mod 3 = 0 Then
MsgBox(i)
End If
End if
Next
EDIT : As Rubens mentioned, this is possible too :
For i As Integer = 3 To 99 Step 3
If i Mod 3 = 0 Then
MsgBox(i)
End If
Next
assuming 99 is your limit, using mod like below will help.
Mod gives you the ability to find divisors of a number, as the remainder is always 0. (in your case, you're after 'Mod 3')
Dim counter As Integer = 1
While counter < 100
If (counter mod 3) = 0 Then
MsgBox("Hello")
End If
Counter += 1
End While
Fiddle here: https://dotnetfiddle.net/gvFjGV
An alternative way to do it is with a For loop - this will save the need of declaring counter & remembering to increment within the loop

taking average of 10 sample in vb2010

I have used below code to find average of 10 sample . But during first time it take sample and do the averaging . during next cycle counter not become Zero.and text box not updating
Static counter As Integer = 0
DIm average_sum As Double = 0
If counter < 10 Then
counter = counter + 1
Count_val.Text = counter
Dim array(10) As Double
For value As Integer = 0 To counter
array(counter) = k
average_sum = average_sum + array(counter)
Next
If counter = 10 Then
average_sum = average_sum / array.Count
System.Threading.Thread.Sleep(250)
Array_count.Text = average_sum
End If
If counter > 10 Then
average_sum = 0
counter = 0
End If
End If
If Avg_count < 10 Then
Dim array(10) As Double
For value As Double = 0 To Avg_count
array(Avg_count) = k
average_sum = average_sum + array(Avg_count)
Avg_count = Avg_count + 1
Next
If Avg_count = 10 Then
average_sum = average_sum / Avg_count
System.Threading.Thread.Sleep(250)
Average.Text = average_sum
Avg_count = 0
End If
End If
Here count value setting properly . But after 2 to3 cycle Average will done earlier itself same thing i writen in excel to compare averages but not matching with average and excel sheet data
Below is excel sheet code.Both code are in timer1 block.
If counter < 10 Then
'counter = 0
'average_sum = 0
Dim headerText = ""
Dim csvFile As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "Current.csv")
If Not IO.File.Exists((csvFile)) Then
headerText = "Date,TIME ,Current, "
End If
Using outFile = My.Computer.FileSystem.OpenTextFileWriter(csvFile, True)
If headerText.Length > 0 Then
outFile.WriteLine(headerText)
End If
Dim date1 As String = "25-10-2014"
Dim time1 As String = TimeOfDay()
Dim x As String = date1 + "," + time1 + "," + distance
outFile.Write(x)
End Using
End If
If counter > 10 Then
counter = 0
End If

Max Value in VB?

Here is the code I'm working on for a project.
What I can't figure out is the max value and how to get it?
Even if I change it, it seems not to affect the outcome of the counter?
Can anyone lead me in the right direction on what to do for this?
Thanks!
Module
Module1
Dim counter As Integer
Const Max_Value As Double = 22
Sub Main()
Console.WriteLine("Are you ready to see which letter is 22? Press Enter")
Console.ReadLine()
For counter As Integer = 0 To Max_Value Step 1
 
Console.Write("a")
Console.WriteLine("0")
counter = counter + 1
Console.Write("b")
Console.WriteLine("1")
counter = counter + 1
Console.Write("c")
Console.WriteLine("2")
counter = counter + 1
Console.Write("d")
Console.WriteLine("3")
counter = counter + 1
Console.Write("e")
Console.WriteLine("4")
counter = counter + 1
Console.Write("f")
Console.WriteLine("5")
counter = counter + 1
Console.Write("g")
Console.WriteLine("6")
counter = counter + 1
Console.Write("h")
Console.WriteLine("7")
counter = counter + 1
Console.Write("i")
Console.WriteLine("8")
counter = counter + 1
Console.Write("j")
Console.WriteLine("9")
counter = counter + 1
Console.Write("k")
Console.WriteLine("10")
counter = counter + 1
Console.Write("l")
Console.WriteLine("11")
counter = counter + 1
Console.Write("m")
Console.WriteLine("12")
counter = counter + 1
Console.Write("n")
Console.WriteLine("13")
counter = counter + 1
Console.Write("o")
Console.WriteLine("14")
counter = counter + 1
Console.Write("p")
Console.WriteLine("15")
counter = counter + 1
Console.Write("q")
Console.WriteLine("16")
counter = counter + 1
Console.Write("r")
Console.WriteLine("17")
counter = counter + 1
Console.Write("s")
Console.WriteLine("18")
counter = counter + 1
Console.Write("t")
Console.WriteLine("19")
counter = counter + 1
Console.Write("u")
Console.WriteLine("20")
counter = counter + 1
Console.Write("v")
Console.WriteLine("21")
counter = counter + 1
Console.Write("w")
Console.WriteLine("22")
Console.ReadLine()
Next
Console.WriteLine()
Console.WriteLine("Summary of the Count: {0} So we've counted to 22{0} The Winning letter is W{0} Which is great because that's the letter of my first name{0} W also stands for War Eagle!", _
Environment.NewLine)
  
Console.WriteLine("Press Enter to Exit")
Console.ReadLine()
End Sub
End
Module
I think what you are trying to accomplish is something like this:
Module Module1
Dim counter As Integer
Const Max_Value As Integer = 22
Sub Main()
Console.WriteLine("Are you ready to see which letter is 22? Press Enter")
Console.ReadLine()
For counter As Integer = 0 To Max_Value Step 1
Select Case counter
Case 0
Console.Write("a")
Case 1
Console.Write("b")
Case 2
Console.Write("c")
Case 3
Console.Write("d")
Case 4
Console.Write("e")
Case 5
Console.Write("f")
Case 6
Console.Write("g")
Case 7
Console.Write("h")
Case 8
Console.Write("i")
Case 9
Console.Write("j")
Case 10
Console.Write("k")
Case 11
Console.Write("l")
Case 12
Console.Write("m")
Case 13
Console.Write("n")
Case 14
Console.Write("o")
Case 15
Console.Write("p")
Case 16
Console.Write("q")
Case 17
Console.Write("r")
Case 18
Console.Write("s")
Case 19
Console.Write("t")
Case 20
Console.Write("u")
Case 21
Console.Write("v")
Case 22
Console.Write("w")
End Select
Console.WriteLine(counter)
Console.ReadLine()
Next
Console.WriteLine()
Console.WriteLine("Summary of the Count: {0} So we've counted to 22{0} The Winning letter is W{0} Which is great because that's the letter of my first name{0} W also stands for War Eagle!", Environment.NewLine)
Console.WriteLine("Press Enter to Exit")
Console.ReadLine()
End Sub
End Module
In your version of the code, it outputs all of the letters each time it loops. In my version of the code, it only outputs one letter each time it loops. The Select Case statement is basically a simpler way of writing a bunch of separate If statements.
However, it's silly to write a big Select Case like that. The best way to implement a loop is to have it iterate over some sort of indexed data-structure. In this case, all you need is a list of letters. The simplest way to do that is to just store all of the letters in a single string, like this:
Dim letters As String = "abcdefghijklmnopqrstuvw"
For counter As Integer = 0 To Max_Value Step 1
Console.Write(letters(counter))
Console.WriteLine(counter)
Console.ReadLine()
Next
Create seperate variable to be used in for loop. What is happening is that you are looping with variable counter and then incrementing it inside loop. This will always loop 22 times.
So declare seperate variable and loop on it.
Dim intC as integer
For intC = 0 to Max_Value step 1
counter = counter + 1
Next
You are declaring counter on Module level and as a private variable for your iteration.
Every time your for loop goes to the next step, the private counter is overwritten with a new value. You should rename it something else
For c As Integer = 0 To Max_Value Step 1
counter = counter + 1
Next
or
For c As Integer = 0 To Max_Value Step 1
counter = c
Next
you probably want this:
For c as Integer = 0 To Max_Value ' Step 1 is default so you can skip that
Console.Write(Convert.ToChar(c + 97)) ' 97 = a
Console.WriteLine(c)
Next
Update: This has not much to do with your original code but is an alternative approach
Dim maxvalue As Integer = 22
Dim counter As Integer = 0
For Each c As Char In "abcdefghijklmnopqrstuvw".ToCharArray()
counter += 1
If counter = maxvalue Then
Dim ordinal As String = "th"
Select Case counter
Case 1 : ordinal = "st"
Case 2 : ordinal = "nd"
Case 3 : ordinal = "rd"
End Select
Console.WriteLine("{0} is the {1}{2} letter in the alphabet", c, maxvalue, ordinal)
Exit For
End If
Next

Listview item or subitem onchange event

When I have a listview with like 46 items, where each item has 4 subitems, the values of the subitems are changing with time, I use the values of subitems to draw a pie chart when an item in the list view is selected using this code:
Chart1.Series("Series1").ChartType = SeriesChartType.Pie
Chart1.Series("Series1").Points.Clear()
If ListView3.FocusedItem.SubItems(1).Text > 0 Then
Chart1.Series("Series1").Points.AddY(ListView3.FocusedItem.SubItems(1).Text)
End If
If ListView3.FocusedItem.SubItems(2).Text > 0 Then
Chart1.Series("Series1").Points.AddY(ListView3.FocusedItem.SubItems(2).Text)
End If
If ListView3.FocusedItem.SubItems(3).Text > 0 Then
Chart1.Series("Series1").Points.AddY(ListView3.FocusedItem.SubItems(3).Text)
End If
If ListView3.FocusedItem.SubItems(4).Text > 0 Then
Chart1.Series("Series1").Points.AddY(ListView3.FocusedItem.SubItems(4).Text)
End If
Is there a way to detect a change of a subitem's value? Like onchange event in textbox, but for items or subitems, because I want the pie chart to update when a subitem value changes.
this code changes the sub items
For xx As Integer = 0 To ListView3.Items.Count - 1
If ListView3.Items(xx).SubItems(0).Text = googleXMLdocument...<s:name>(j).Value Then
If j + 1 = 1 Then
ListView3.Items(xx).SubItems(1).Text += 1
End If
If j + 1 = 2 Then
ListView3.Items(xx).SubItems(2).Text += 1
End If
If j + 1 = 3 Then
ListView3.Items(xx).SubItems(3).Text += 1
End If
If j + 1 > 4 Then
ListView3.Items(xx).SubItems(4).Text += 1
End If
End If
Next
Try this code. This way the chart will automatically get updated if there is any change in the subitems. You will not require a timer.
For xx As Integer = 0 To ListView3.Items.Count - 1
If ListView3.Items(xx).SubItems(0).Text = googleXMLdocument...<s:name>(j).Value Then
If j + 1 = 1 Then
ListView3.Items(xx).SubItems(1).Text += 1
End If
If j + 1 = 2 Then
ListView3.Items(xx).SubItems(2).Text += 1
End If
If j + 1 = 3 Then
ListView3.Items(xx).SubItems(3).Text += 1
End If
If j + 1 > 4 Then
ListView3.Items(xx).SubItems(4).Text += 1
End If
End If
Next
Put the below code after the above code in a loop. You can also do away with the FocusedItem
Chart1.Series("Series1").Points.Clear()
Chart1.Series("Series1").Points.AddY (ListView3.FocusedItem.SubItems(1).Text)
Chart1.Series("Series1").Points.AddY (ListView3.FocusedItem.SubItems(2).Text)
Chart1.Series("Series1").Points.AddY (ListView3.FocusedItem.SubItems(3).Text)
Chart1.Series("Series1").Points.AddY (ListView3.FocusedItem.SubItems(4).Text)

number variable won't addition by 1 and display in a label everytime the timer counts 1000ms. - vb.net

Here is my code:
number = 1
If chkFN.Enabled = True Then
If ProgressBar1.Value < 100 Then
number += 1
lblFN2.Text = number
Else
lblFN2.Text = "0"
End If
End If
i have a checkbox, progressbar and a label.
when the progress bar is lower than 100 i want the number variable to + 1 every time the timer counts 1. I've tried "X = X + 1" - it worked to a point but i need to reset the variable to "0" when the progressbar hits 100% and when i click the stop button.
while I've been typing this i've also tried:
X = X + 1
If chkFN.Enabled = True Then
If ProgressBar1.Value < 100 Then
lblFN2.Text = X + 1
ElseIf AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsMediaEnded
Then
X = 0
lblFN2.Text = "0"
Else
X = 0
lblFN2.Text = "0"
End If
End If
But when you start the timer again it just resumes from it's last number e.g. "13" and doesn't reset to "0".
Thanks for the help guys :)
Use your first code, but add in
number += 1
to the Else condition. You need to reset the variable or it just increments it again.