user11251258
user11251258

Reputation:

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).

Upvotes: 0

Views: 85

Answers (1)

user11982798
user11982798

Reputation: 1908

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) 

Upvotes: 2

Related Questions