Dobrý den. Obnovil jsem svůj stracený ListView,ale funkce SumBetweenDates nefunguje,tak jak má.Nevím si s tím dát rady.Pořád mi to vyhazuje chybu " Funkce SumBetweenDates nevrací hodnotu na všech cestách kódu.Nechybí příkaz Return?Ale já tam příkaz Return mám a stejně to nejde.Zasílám svůj kód a dopředu velice děkuji za radu. S pozdravem Luděk Public Class Form1 Function SumBetweenDates(ByVal lv As ListView, ByVal columnDatesIndex As Integer, ByVal columnSumIndex As Integer, ByVal fromDate As DateTime, ByVal toDate As DateTime) As Decimal For i As Integer = 0 To lv.Items.Count - 1 Dim d As Date If DateTime.TryParse(lv.Items(i).SubItems(columnDatesIndex).Text, d) Then If d >= fromDate And d <= toDate Then Dim n As Decimal If Decimal.TryParse(lv.Items(i).SubItems(columnSumIndex).Text, n) Then Return celkem celkem += n Return n End If End If End If Next End Function Private celkem As Decimal Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged End Sub Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged End Sub Private Sub TextBox9_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged End Sub Private Sub TextBox21_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox21.TextChanged End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 'uložit dat Dim soubor As New IO.StreamWriter("database1.txt") 'otevřít soubor For i As Object = 0 To ListView1.Items.Count - 1 'projít všechny položky seznamu soubor.WriteLine(ListView1.Items(i).SubItems(0).Text) 'zapsat datum soubor.WriteLine(ListView1.Items(i).SubItems(1).Text) 'zapsat pivo soubor.WriteLine(ListView1.Items(i).SubItems(2).Text) 'zapsat cigarety soubor.WriteLine(ListView1.Items(i).SubItems(3).Text) 'zapsat jiná událost Next soubor.Close() End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim soubor As New IO.StreamReader("database1.txt") 'načtení vstupu a otevření souboru While Not soubor.EndOfStream 'číst,dokuď nejsme na konci souboru Dim datum As DateTime = CDate(soubor.ReadLine()) 'načíst první řádek - datum Dim pivo As Single = CSng(soubor.ReadLine()) 'načíst druhý řádek - pivo Dim cigarety As Single = CSng(soubor.ReadLine()) 'načíst třetí řádek - cigarety Dim jinaUdalost As String = soubor.ReadLine() 'načíst čtvrtý řádek - jiná událost Dim polozka As New ListViewItem() 'přidat položku do seznamu If pivo > 0 Then polozka.ForeColor = Color.Blue If cigarety > 0 Then polozka.ForeColor = Color.Red polozka.Text = datum 'vypsat datum polozka.SubItems.Add(pivo) 'přidat druhý sloupec - pivo polozka.SubItems.Add(cigarety) 'přidat třetí sloupec - cigarety polozka.SubItems.Add(jinaUdalost) 'přidat čtvrtý sloupec - jiná událost ListView1.Items.Add(polozka) 'přidat položku do seznamu End While soubor.Close() Dim soucet1, soucet2, soucet3 As Single For i As Object = 0 To ListView1.Items.Count - 1 soucet1 = soucet1 + CSng(ListView1.Items(i).SubItems(1).Text) soucet2 = soucet2 + CSng(ListView1.Items(i).SubItems(2).Text) soucet3 = soucet1 + soucet2 Next TextBox1.Text = CStr(soucet1 & (" Kč ")) TextBox2.Text = CStr(soucet2 & (" Kč ")) TextBox3.Text = CStr(soucet1 + soucet2 & (" Kč ")) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim dlg As New Dialog1() If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then Dim datum As DateTime = dlg.DateTimePicker1.Value Dim pivo As Single = dlg.NumericUpDown1.Value Dim cigarety As Single = dlg.NumericUpDown2.Value Dim jinaUdalost As String = dlg.TextBox1.Text Dim polozka As New ListViewItem() If pivo > 0 Then polozka.ForeColor = Color.Blue If cigarety > 0 Then polozka.ForeColor = Color.Red polozka.Text = datum polozka.SubItems.Add(pivo) polozka.SubItems.Add(cigarety) polozka.SubItems.Add(jinaUdalost) ListView1.Items.Add(polozka) Dim soucet1, soucet2, soucet3 As Single For i As Integer = 0 To ListView1.Items.Count - 1 soucet1 = soucet1 + CSng(ListView1.Items(i).SubItems(1).Text) soucet2 = soucet2 + CSng(ListView1.Items(i).SubItems(2).Text) soucet3 = soucet1 + soucet2 Next TextBox1.Text = CStr(soucet1) TextBox2.Text = CStr(soucet2) TextBox3.Text = soucet1 + soucet2 End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If ListView1.SelectedItems.Count = 0 Then Exit Sub With ListView1.SelectedItems(0) Dim dlg As New Dialog1() dlg.DateTimePicker1.Value = .SubItems(0).Text dlg.NumericUpDown1.Value = .SubItems(1).Text dlg.NumericUpDown2.Value = .SubItems(2).Text dlg.TextBox1.Text = .SubItems(3).Text If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then Dim datum As DateTime = dlg.DateTimePicker1.Value Dim pivo As Single = dlg.NumericUpDown1.Value Dim cigarety As Single = dlg.NumericUpDown2.Value Dim jinaUdalost As String = dlg.TextBox1.Text .SubItems(0).Text = datum .SubItems(1).Text = pivo .SubItems(2).Text = cigarety .SubItems(3).Text = jinaUdalost Dim soucet1, soucet2, soucet3 As Single For i As Integer = 0 To ListView1.Items.Count - 1 soucet1 = soucet1 + CSng(ListView1.Items(i).SubItems(1).Text) soucet2 = soucet2 + CSng(ListView1.Items(i).SubItems(2).Text) soucet3 = soucet1 + soucet2 Next TextBox1.Text = CStr(soucet1) TextBox2.Text = CStr(soucet2) TextBox3.Text = soucet1 + soucet2 End If End With End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click If ListView1.Items.Count = 0 Then Exit Sub 'pokud není vybrána žádná položka,nic nemazat ListView1.Items.Remove(ListView1.SelectedItems(0)) Dim soucet1, soucet2, soucet3 As Single For i As Integer = 0 To ListView1.Items.Count - 1 soucet1 = soucet1 + CInt(ListView1.Items(i).SubItems(1).Text) soucet2 = soucet2 + CInt(ListView1.Items(i).SubItems(2).Text) soucet3 = soucet1 + soucet2 Next TextBox1.Text = CStr(soucet1) TextBox2.Text = CStr(soucet2) TextBox3.Text = soucet1 + soucet2 End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Close() End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click Dim celkem As Decimal = SumBetweenDates(ListView1, 0, 1, " 4.11.2016 ", " 30.11.2016 ") MsgBox("SumBetweenDates =" & celkem.ToString) TextBox4.Text = celkem.ToString() End Sub End Class
|