For each Textbox - vb.net

I have a code in VB6 which i need to convert to VB.NET, in VB6 I had 5 textboxes named t(0),t(1),t(2), t(3) and t(4) for which this code worked:
Suma = 0
For i = 1 To 4
Suma = Suma + t(i).Text
Next
If CDbl(Suma) > Label13.Caption Then
t(Index).Text = 0
Suma = 0
t(Index).SelStart = 0
t(Index).SelLength = 1
For i = 1 To 4
Suma = Suma + t(i).Text
Next
End If
t(0).Text = Format(Label13.Caption - CDbl(Suma), "#,##0.00")
Else
Suma = 0
For i = 0 To 3
Suma = Suma + t(i).Text
Next
If CDbl(Suma) > Label13.Caption Then
t(Index).Text = 0
Suma = 0
t(Index).SelStart = 0
t(Index).SelLength = 1
For i = 0 To 3
Suma = Suma + t(i).Text
Next
End If
t(4).Text = Format(Label13.Caption - CDbl(Suma), "#,##0.00")
Now, in VB.net i have textboxes named: t0,t1,t2,t3,t4
How would i loop through these textboxes in VB.net like i did in VB6?

You can use this concept :
For c As Integer = 0 To 4
CType(Me.Controls("t" & c.ToString()), TextBox).Text = "t" & c.ToString()
Next
In your case, it would be something like this :
Suma = 0
For i = 1 To 4
Suma = Suma + CInt(CType(Me.Controls("t" & i.ToString()), TextBox).Text)
Next
'Rest of your code following the above concept

Related

Case Count 0 Digit in VB.Net

Why Don't work Case 0 in my code? In this example, if there is nothing in that line (nothing), it should be +1, but it shows me the result 0.
Dim StartIndex = TextBoz1.Lines.Length - 1
For i = StartIndex To 0 Step -1
Dim lineVals As String() = TextBoz1.Lines(i).Split(",")
Select Case lineVals.Count()
Case 0
lastDraw1 += 1
lastDraw2 += 1
lastDraw3 += 1
Case 1
lastDraw1 = 0
lastDraw2 += 1
lastDraw3 += 1
Case 2
lastDraw1 = 0
lastDraw2 = 0
lastDraw3 += 1
Case 3
lastDraw1 = 0
lastDraw2 = 0
lastDraw3 = 0
End Select
Next
TextBox8.Text = lastDraw1
TextBox9.Text = lastDraw2
TextBox10.Text = lastDraw3
End Sub
It does not equal to zero. Try a small sample.
Dim s As String = ""
Dim v As String() = s.Split(","c)
Console.WriteLine(v.Count) ' Display 1
In your case, you would need to check if the string is empty first.
If String.IsNullOrEmpty(TextBoz1.Lines(i)) Then
lastDraw1 += 1
lastDraw2 += 1
lastDraw3 += 1
Else
Dim lineVals As String() = TextBoz1.Lines(i).Split(",")
Select Case lineVals.Count()
Case 1
lastDraw1 = 0
lastDraw2 += 1
lastDraw3 += 1
Case 2
lastDraw1 = 0
lastDraw2 = 0
lastDraw3 += 1
Case 3
lastDraw1 = 0
lastDraw2 = 0
lastDraw3 = 0
End Select
End If

For Next VBA skips half

I am trying to make a simple VBA in Excel, to copy some data I am trying to regroup. I seems to work good for some part, but it skips a row and column everytime! The problem must be somewhere in the double For..Next.. I am using, but I can't find it:
The result I get:
For i = 1 To AantalPag 'HIERRR
GezSite = BeginCel.Offset(i + 1) 'HIERRR
For iweek = 1 To AantalWeek
GezWeek = BeginCel.Offset(0, iweek)
For i2 = 1 To AantalWeekData
If BeginCelData.Offset(i2 - 1) = GezWeek Then
For i3 = 1 To AantalSitesData
If BeginCelData.Offset(0, i3) = GezSite Then
Sommetje = Sommetje + BeginCelData.Offset(i2 - 1, i3 - 1)
Else
i3 = i3 + 1
End If
Next i3
'BeginCel.Offset(i, iweek) = Sommetje
'Sommetje = 0
Else
i2 = i2 + 1
End If
Next i2
BeginCel.Offset(i, iweek) = Sommetje
Sommetje = 0
iweek = iweek + 1
Next iweek
i = i + 1
Next i
The code below will print the numbers from 1 to 100 in the Immediate window.
For n = 1 to 100
Debug.Print n
Next n
The code below will print every other number in the Immediate window. This is because n is incremented both by n = n + 1 and then again by Next n.
For n = 1 to 100
Debug.Print n
n = n + 1
Next n

Nested FOR loops in VBA

I am attempting to impliment a nested FOR loop in excel. Then interior loop does not seem to be executing in the code. Is the error syntatical? What's going on here?
Sub Statistics()
Dim cc As Integer
Dim i As Integer
i = 4
cc = 0
For cc = 0 To 4
For i = 4 To -4
If Sheets("Significance").Cells(4 + cc, 13 - i) = 1 Then Sheets("Output Database").Cells(8 + currevent, 7 + cc) = i
Next i
Next cc
'Rates
i = 4
cc = 0
For cc = 0 To 4
For i = 4 To -4
If Sheets("Significance").Cells(14 + cc, 13 - i) = 1 Then Sheets("Output Database").Cells(8 + currevent, 23 + cc) = i
Next i
Next cc
End Sub
The loop referring to the i variable needs to specify that i is decreasing:
For i = 4 To -4 Step -1

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

InvalidArgument=Value of '2' is not valid for 'index'

Dim group11_0_count = 0
Dim group11_1_count = 0
Dim group11_2_count = 0
Dim m As Integer = 0
Dim n As Integer = 0
Dim increment2 As Integer
For m = 0 To machings2.Items.Count - 1
For n = 0 To 3
If machings2.Items(m).ToString.Chars(n) = "1" Then
increment2 = increment2 + 1
End If
Next
If (increment2 = 0) Then
group11_0_count = group11_0_count + 1
group11_1_0.Items.Add(machings2.Items(m))
End If
If (increment2 = 1) Then
group11_1_count = group1_1_count + 1
group11_1_1.Items.Add(machings2.Items(m))
End If
If (increment2 = 2) Then
group11_2_count = group1_2_count + 1
group11_1_2.Items.Add(machings2.Items(m))
End If
increment2 = 0
Next
If (group11_0_count > 0 AndAlso group11_1_count > 0) Then
Dim result = ""
Dim index As Integer = 0
Dim gg As Integer = 0
Dim hh As Integer = 0
Dim i As Integer = 0
For hh = 0 To group11_1_count - 1
For gg = 0 To group11_0_count - 1
result = ""
index = 0
For i = 0 To 3
If group11_1_0.Items(gg).ToString.Chars(i) <> group11_1_1.Items(hh).ToString.Chars(i) Then
result &= "-"
index = index + 1
Else
result &= group11_1_0.Items(gg).ToString.Chars(i)
End If
Next
If (index = 1) Then
machings3.Items.Add(result)
End If
Next
Next
End If
I am comparing the items of two combobox items like that
combobox1 items
0000
combobox items
0001
0010
the result will be like that in machings3 combobox
000-
00-0
Here the differnce between two items indicated by - sign
But i am getting InvalidArgument=Value of '2' is not valid for 'index'.
I Can't make sense out of your source and where the IndexOutOfRangeException occurs. But you know that you need 3 Items in a Combobox to access Item with Index 2?! Every collection starts with 0.