Case Count 0 Digit in VB.Net - 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

Related

VB.NET Count bits, 1's are counted as 1, Zero's are counted together in a sequence between 1's

Looking to count bits in a sequence.
1's are counted as 1, Zero's are counted together in a sequence between 1's
If you look at this picture
The output should be 2,1,5,1,1,3,2,1
Here is my code it works on 80% of numbers sometimes it messes up.
dim bitmask() as byte
redim bitmask(15)
bitmask(0) = 0
bitmask(1) = 0
bitmask(2) = 1
bitmask(3) = 0
bitmask(4) = 0
bitmask(5) = 0
bitmask(6) = 0
bitmask(7) = 0
bitmask(8) = 1
bitmask(9) = 1
bitmask(10) = 0
bitmask(11) = 0
bitmask(12) = 0
bitmask(13) = 1
bitmask(14) = 0
bitmask(15) = 1
Public Function GetBitCount() As Byte()
Dim count As Byte = 0
Dim bitcounts As New List(Of Byte)
Dim indexOfNext As Integer = 0
Dim totalCounted As Integer = 0
While True
indexOfNext = Array.IndexOf(bitmask, CByte(1), indexOfNext + 1)
If indexOfNext > 0 Then
If indexOfNext - totalCounted = 1 Then
bitcounts.Add(2)
bitcounts.Add(1)
ElseIf indexOfNext - totalCounted > 0 Then
bitcounts.Add(IIf(totalCounted > 0, indexOfNext - totalCounted, indexOfNext))
ElseIf indexOfNext - totalCounted > 1 Then
bitcounts.Add(2)
bitcounts.Add(1)
Else
bitcounts.Add(1)
bitcounts.Add(1)
End If
If totalCounted = 0 Then bitcounts.Add(1)
totalCounted = indexOfNext + 1
Else
Exit While
End If
End While
If totalCounted - 1 > 2 AndAlso totalCounted - 1 < bitmask.Length - 1 Then
bitcounts.Add(1)
bitcounts.Add((bitmask.Length - 1) - (totalCounted - 1))
ElseIf totalCounted - 1 <= 2 AndAlso totalCounted - 1 < bitmask.Length - 1 Then
bitcounts.Add((bitmask.Length - 1) - (totalCounted - 1))
Else
bitcounts.Add(1)
End If
If count > 0 Then bitcounts.Add(count)
Return bitcounts.ToArray()
End Function
Solution
Public Function GetBitCount() As Byte()
Dim i As Integer = 0
Dim count As Byte = 0
Dim bitcounts As New List(Of Byte)
For i = 0 To bitmask.Length - 1
If bitmask(i) = 1 Then
bitcounts.Add(count)
count = 0
End If
count += 1
Next
If count > 0 Then bitcounts.Add(count)
Return bitcounts.ToArray()
End Function

Count lines not 0 found Textboxes

I want to calculate the amount in a multiline Textbox where the value 0 is not found.
If TxtListScanValue.Text = ("2") Then
TxtDrawR2.Text &= Environment.NewLine & lastDraw2
Dim ListScan = TxtNumberListScan.Lines.ToList.Select(Function(o, i) New With {.scan = o, .Index = i})
Dim DrawR2 = TxtDrawR2.Lines.ToList.Select(Function(o, i) New With {.draw = o, .Index = i})
Dim list2 = From a In ListScan From b In DrawR2 Where a.Index = b.Index Select LstScan = a.scan, DrwR2 = ("00" & b.draw).Substring(("00" & b.draw).Length - 2) Order By DrwR2 Descending
TxtListScanTxt.Text = String.Join(vbCrLf, list2)
End If
If TxtdrawR5 =
2
4
0
0
1
3
5
In output I want to display: 5 because:
I want to calculate the count lines where the value 0 is not found. Count lines no have 0 value :D (2+4+1+3+5 = 5) (5 lines no have 0 value).
You create function like this:
'For Counting
Private Function CountNonZero(ByVal TheCtrl As TextBox) As Integer
Dim myCnt As Integer = 0
For Each Content In TheCtrl.Lines
Dim ContentVal As Integer = 0
Integer.TryParse(Content, ContentVal)
If ContentVal <> 0 Then myCnt += 1
Next
Return myCnt
End Function
'For Counting
Private Function SummingNonZero(ByVal TheCtrl As TextBox) As Integer
Dim mySum As Integer = 0
For Each Content In TheCtrl.Lines
Dim ContentVal As Integer = 0
Integer.TryParse(Content, ContentVal)
If ContentVal <> 0 Then mySum += ContentVal
Next
Return mySum
End Function
And you can count or sum now:
dim TxtdrawR5Count as integer = CountNonZero(TxtdrawR5)
dim TxtdrawR5Sum as integer = SummingNonZero(TxtdrawR5)

Count Analysis Number Step -1 to 0 (1/2, 2/3 and 3/3)

how can I make this code work? Let's say we have in Textbox1 - the following: (2,4,6) - the textbox that will be scanned to determine the analysis.
TxtBoxIntDraws.Lines(0) = 3,6,7,9
TxtBoxIntDraws.Lines(1) = 2,6,9,10
TxtBoxIntDraws.Lines(2) = 3,5,7,10
then it will result: >
and if exists count reset to 0, if not exists count +1. the analysis will begin from the last line to the first line. TxtBoxIntDraws.Lines(2) - and finish with TxtBoxIntDraws.Lines(0)
1/3 - 0 (because one value exists 2 or 4 or 6, in TxtBoxIntDraws.Lines(0),
2/3 - 1 (because not not 2/3 - 2,6 or 4,6) and +1 add to value.
3/3 - 3 (because not 3/3 - 2,4,6) and +1 add to value.
so briefly, an analysis, 1/3, 2/3 and 3/3, if the value exists then it will be 0, if the value does not exist, it will be added + 1. It has to start from Step -1 to Line 1. I'm trying to make this code, and I'm not doing worked it.
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim lastDraw1 As Integer = 0 '1/3
Dim lastDraw2 As Integer = 0 '2/3
Dim lastDraw3 As Integer = 0 '3/3
For i As Integer = Step to -1 To TxtBoxIntDraws.Lines.Count - 0
Dim lineVals As String() = TxtBoxIntDraws.Lines(i).Split(",")
Select Case lineVals.Count()
Case 1
lastDraw1 = 0
lastDraw2 += 1
lastDraw3 += 1
TextBox1.Text = lastDraw1
TextBox2.Text = lastDraw2
TextBox3.Text = lastDraw3
Case 2
lastDraw1 = 0
lastDraw2 = 0
lastDraw3 += 1
TextBox1.Text = lastDraw1
TextBox2.Text = lastDraw2
TextBox3.Text = lastDraw3
Case 3
lastDraw1 = 0
lastDraw2 = 0
lastDraw3 = 0
TextBox1.Text = lastDraw1
TextBox2.Text = lastDraw2
TextBox3.Text = lastDraw3
Case Else
'This should probably also be handled
End Select
Next
End Sub
Not sure what you are doing but to fix the For...Next
Dim StartIndex = TxtBoxIntDraws.Lines.Length - 1
For i = StartIndex To 0 Step -1
'Your code here
Next

For each Textbox

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

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.