hi,

How do i delete the selected records at listview and textfile (which keep the record from listview) and at the same time show the total current balance at listview.
any guidance plz help.

Imports System.Data.OleDb.OleDbCommand
Imports System.Data.OleDb
Imports System.IO
Imports System
Imports System.Text
Imports System.Windows.Forms
Imports System.Console


Public Class Form1

    Public Class frmPaymentSummary
        Dim nTotlistrec As Integer
        Dim bNextPage As Boolean
        Dim bPrevPage As Boolean
        Private valRounded As String
        Private blnRoundedValue As Boolean = False
        Private YesPressed, NoPressed As Integer



        Private Sub frmPaymentSummary_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Visible = False
            lblServiceCharge.Text = ServiceC
            lblBillAmountValue.Text = dTotal
            dTotal = 0
            ServiceC = 0

            lblSummary.Font = myFont1
            Label5.Font = myFont
            Label6.Font = myFont
            lblPrevious.Font = myFont
            lblNext.Font = myFont
            ListView1.Font = myFont2

            lblPrevious.Text = GetButtonText("Previous")
            lblNext.Text = GetButtonText("Confirm")
            lblSummary.Text = GetButtonText("Summary")
            Label5.Text = GetButtonText("TotalPayable")

            ListView1.View = View.Details
            ListView1.CheckBoxes = False

            ListView1.Columns.Add(GetButtonText("Number"), 50, HorizontalAlignment.Center)
            ListView1.Columns.Add(GetButtonText("AgencyName"), 290, HorizontalAlignment.Center)
            ListView1.Columns.Add(GetButtonText("AccountNo"), 130, HorizontalAlignment.Center)
            ListView1.Columns.Add(GetButtonText("BillNo"), 130, HorizontalAlignment.Center)
            ListView1.Columns.Add(GetButtonText("BillAmount1"), 130, HorizontalAlignment.Center)
            ListView1.Columns.Add(GetButtonText("ServiceCharge1"), 130, HorizontalAlignment.Center)
            ListView1.Columns.Add(GetButtonText("Total1"), 130, HorizontalAlignment.Center)

            Application.DoEvents()

        End Sub

        Private Sub frmPaymentSummary_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
            Dim aStr(8) As String
            Try
                GetFileContents()

                lblBillAmountValue.Text = Format(dTotal, "#,###,##0.00")
                lblServiceCharge.Text = Format(ServiceC, "#,###,##0.00")

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

        Private Sub addListView(ByVal p1 As String, ByVal p2 As String, ByVal p3 As String, ByVal p4 As String, ByVal p5 As String, ByVal p6 As String)
            Dim str(8) As String
            Dim itm As ListViewItem

            str(0) = ListView1.Items.Count + 1
            str(1) = p1
            str(2) = p2
            str(3) = p3
            str(4) = p4
            str(5) = p5
            str(6) = p6
            'str(7) = p7
            '  str(5) = vbTrue
            itm = New ListViewItem(str)
            ListView1.Items.Add(itm)
        End Sub

        Public Sub GetFileContents()
            'Try
            Dim aStr(8) As String
            Dim FILE_NAME As String = "C:\Documents and SettingsDesktop\BillPaymentRecords.txt"

            If System.IO.File.Exists(FILE_NAME) = True Then
                Dim objReader As New System.IO.StreamReader(FILE_NAME)
                Do While objReader.Peek() <> -1
                    aStr = Split(objReader.ReadLine(), ";")

                    SESSION_NAME = aStr(0)
                    SESSION_ACCNO = aStr(1)
                    SESSION_BILLNO = aStr(2)
                    SESSION_BILLAMO = aStr(5)
                    SESSION_BILLINT = aStr(4)

                    dTotal += aStr(5) '.Replace("RM", "")
                    ServiceC += aStr(4) 'Replace("RM", "")

                    nTotlistrec = ListView1.Items.Count
                    If nTotlistrec < 10 Then
                        addListView(aStr(0), aStr(1), aStr(2), aStr(3), aStr(4), aStr(5))
                    End If

                Loop

            Else
                MsgBox("File Does Not Exist")
            End If

        End Sub

        Private Sub pnlNext_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlNext.MouseDown
            pnlNext.BackgroundImage = My.Resources.Next1_Down

        End Sub

        Private Sub pnlNext_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlNext.MouseUp
            pnlNext.BackgroundImage = My.Resources.Next1_Up

            frmMakePayment.Show()
            Me.Close()

        End Sub

        Private Sub pnlPre_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlPre.MouseDown
            pnlPre.BackgroundImage = My.Resources.Pre1_Down
        End Sub

        Private Sub pnlPre_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlPre.MouseUp
            pnlPre.BackgroundImage = My.Resources.Pre1_Up
            If ButtonClick = "POS" Then
                frmStep1_AgencyDirect.Show()
            ElseIf ButtonClick = "OTHER" Then
                frmStep2_AgencyDirect.Show()
            End If

            Me.Close()
        End Sub


        Private Sub PnlDelete_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PnlDelete.MouseDown
            PnlDelete.BackgroundImage = My.Resources.POS_Btn_Down
        End Sub

        Private Sub PnlDelete_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PnlDelete.MouseUp
            Try


                PnlDelete.BackgroundImage = My.Resources.POS_Btn_Up
                ListView1.SelectedItems(0).Remove()

                'File.Delete("C:\Documents and SettingsDesktop\\Bill Payment\BillPaymentRecords.txt")
                'File.DeleteListView(aStr(0), aStr(1), aStr(2), aStr(3), aStr(4), aStr(5)))

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

        Private Sub Label6_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label6.MouseUp
            ListView1.SelectedItems(0).Remove()
        End Sub

        Private Sub ListView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
            ListView1.Select()
        End Sub

    End Class
End Class

hi,

its not the same as what i meant for. Can you give the solution from my source code which i have sent earlier.
i delete the selected records from listview and textfile (which saves the record from listview) and at the same time show the total current balance at listview. It means once the record deleted the amount from total must able to deducted and show the current balance.
any guidance plz help.

Here it is.
Almost an exact copy of my previous solution, with the alterations to match your project.

Private Sub PnlDelete_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PnlDelete.MouseUp
   Try
      Dim stringToFind As String = ListView1.SelectedItems(0).Text

      PnlDelete.BackgroundImage = My.Resources.POS_Btn_Up
      ListView1.SelectedItems(0).Remove()

      Dim line As String
      Dim input As StreamReader
      Dim strFile As New ArrayList
         
      input = File.OpenText("C:\Documents and Settings\Desktop\BillPaymentRecords.txt")

      While input.Peek <> -1
         line = input.ReadLine

         If Not line.Contains(stringToFind) Then
            strFile.Add(line)
         End If
      End While

      input.Close()

      If File.Exists("C:\Documents and Settings\Desktop\BillPaymentRecords.txt") Then
         File.Delete("C:\Documents and Settings\Desktop\BillPaymentRecords.txt")
      End If

      Dim writer As New StreamWriter("", True)
      For Each item As String In strFile
         writer.WriteLine(item)
      Next
      writer.Flush()
      writer.Close()

      GetFileContents()

      lblBillAmountValue.Text = Format(dTotal, "#,###,##0.00")
      lblServiceCharge.Text = Format(ServiceC, "#,###,##0.00")
   Catch ex As Exception
      MsgBox(ex.Message)
   End Try
End Sub

hi,
it gives error

Value of type 'System.Windows.Forms.ListViewItem' cannot be converted to 'String'.

any suggestion

Yeah. Replace the line
Dim stringToFind As String = ListView1.SelectedItems(0).Text
with this:
Dim stringToFind As String = ListView1.SelectedItems(0).SubItems(0).Text & ";" _
ListView1.SelectedItems(0).SubItems(1).Text & ";" _
ListView1.SelectedItems(0).SubItems(2).Text & ";" _
ListView1.SelectedItems(0).SubItems(3).Text & ";" _
ListView1.SelectedItems(0).SubItems(4).Text

hi,

it still gives error. anything that i have missed out.

Dim stringToFind As String = ListView1.SelectedItems(0).SubItems(0).Text & ";" _
ListView1.SelectedItems(0).SubItems(1).Text & ";" _
ListView1.SelectedItems(0).SubItems(2).Text & ";" _
ListView1.SelectedItems(0).SubItems(3).Text & ";" _
ListView1.SelectedItems(0).SubItems(4).Text

End of statement expected.

Ah yes! I'm so very sorry. I missed that one.
On the 4 lines below "Dim stringToFind......", add an ampersand (&) in the beginning, separating the ampersand and "Listview1.SelectedItems(0)..." with a blank space.

hi,

The code is deleting the .txt file and not the records inside the .txt. Actually my question was to delete the contents record from .txt when it is selected from listview and at the same time show the total current balance at listview..
.txt file is where i keep all the records entered into listview. so when user select a particular row from listview to delete it has to be removed from listview and also inside the .txt and produce the current balance after the deletion of the particular record.

guidance please.

What the code does is this.
First it reads the entire file, line by line.
And while it reads, each line is compared to the semi-colon separated string of the record from the ListView.
And if the string does NOT match the record, it's inserted into the ArrayList.

Once finished, the file is deleted, and recreated using the strings in the ArrayList, except for the deleted record.

Which reminds me, do this replacement:

'Replace this line
Dim writer As New StreamWriter("", True)

'With this line
Dim writer As New StreamWriter("C:\Documents and Settings\Desktop\BillPaymentRecords.txt", True)

thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.