hi,

Below is my code for DELETE button. What i want is if the user select the transaction from the listview and press the DELETE button it must be able to delete the transaction from listview and text file(temporary log file) which i keep all the transaction record. At the same time the list view must able show the total current balance from the deleted trnsaction.
The problem is my code just do function to delete the transaction from list view but it does not delete the record form text file and the total amount till the same as it is....
Please guide me base on my code.
PLEASE HELP !!! TQ

Private Sub PnlDelete_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PnlDelete.MouseUp
        PnlDelete.BackgroundImage = My.Resources.BtnUp

        Try
            'Dim stringToFind As String = ListView1.SelectedItems(0)
            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

            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:\Program Files\POS\Autopay Terminal\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:\Program Files\POS\Autopay Terminal\BillPaymentRecords.txt") Then
                File.Delete("C:\Program Files\POS\Autopay Terminal\BillPaymentRecords.txt")
            End If

            Dim writer As New StreamWriter("C:\Program Files\POS\Autopay Terminal\BillPaymentRecords.txt", 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

Recommended Answers

All 11 Replies

How about we shorten this code a bit...

Private Sub PnlDelete_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PnlDelete.MouseUp
        PnlDelete.BackgroundImage = My.Resources.BtnUp

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

      If File.Exists("C:\Program Files\POS\Autopay Terminal\BillPaymentRecords.txt") Then
         File.Delete("C:\Program Files\POS\Autopay Terminal\BillPaymentRecords.txt")
      End If

      Dim writer As New StreamWriter("C:\Program Files\POS\Autopay Terminal\BillPaymentRecords.txt", True)
      For Each item As ListViewItem In ListView1.Items
         Dim line As String = item.SubItems(0).Text & ";" _
         & item.SubItems(1).Text & ";" & item.SubItems(2).Text & ";" _
         & item.SubItems(3).Text & ";" & item.SubItems(4).Text
         writer.WriteLine(line)
      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 Oxiegen,

Thank You very much for you kind response. Its really work. But i still facing 1 problem which is the total amount doesn't reduce and still remain the same.
Can you help me on this....PLEASE HELP PLEASE HELP!!!

THANK YOU

Well.
I don't know how the code for GetFileContents() looks like, so it's impossible for my to determine why it doesn't work.
Perhaps you should take a look and determine when and how the variables dTotal and ServiceC are assigned.

hi,

this is my code for GetFileContents(). I have no idea whr the codes goes wrong.....
PLEASE guide me.....

Public Sub GetFileContents()
        Try
            Dim aStr(8) As String
            Dim FILE_NAME As String = "C:\Program Files\Autopay Terminal\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_AGENCY_NAME = aStr(0)
                    SESSION_AGENCY_ACCNO = aStr(1)
                    SESSION_AGENCY_BILLNO = aStr(2)
                    SESSION_AGENCY_BILLAMO = aStr(5)
                    SESSION_AGENCY_BILLINT = aStr(4)
                    If Trim(aStr(5)) <> "" Then
                        dTotal += CDbl(aStr(5)) '.Replace("RM", "")
                    End If
                    If Trim(aStr(4)) <> "" Then
                        ServiceC += CInt(aStr(4)) 'Replace("RM", "")
                    End If
                    nTotlistrec = ListView1.Items.Count
                    If nTotlistrec < 10 Then
                        addListView(aStr(0), aStr(1), aStr(2), aStr(3), aStr(4), aStr(5))
                    End If
                Loop
                        End If
        Catch ex As Exception
            WriteToLogFile("GetFileContents() -> " & ex.Message)
        End Try
    End Sub

hi,

it hit error right after ========>

SESSION_AGENCY_BILLAMO = aStr(5)

GetFileContents() -> Index was outside the bounds of the array.

Please guide as i not sure where to fix it. TQ

Ok, I'm just assuming based on original question that the listview only contains 5 columns.
So, let's try this:

Public Sub GetFileContents()
   Try
      Dim aStr() As String = New String() {}
      Dim FILE_NAME As String = "C:\Program Files\Autopay Terminal\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_AGENCY_NAME = aStr(0)
            SESSION_AGENCY_ACCNO = aStr(1)
            SESSION_AGENCY_BILLNO = aStr(2)
            SESSION_AGENCY_BILLAMO = aStr(3) 'Change from 5 to 3
            SESSION_AGENCY_BILLINT = aStr(4)

            If aStr(3).Trim <> "" Then
               dTotal += CDbl(aStr(3).Trim) '.Replace("RM", "")
            End If
            If aStr(4).Trim <> "" Then
               ServiceC += CInt(aStr(4).Trim) 'Replace("RM", "")
            End If

            nTotlistrec = ListView1.Items.Count
            If nTotlistrec < 10 Then
               addListView(aStr(0), aStr(1), aStr(2), aStr(3), aStr(4))
            End If
         Loop
      End If
   Catch ex As Exception
      WriteToLogFile("GetFileContents() -> " & ex.Message)
   End Try
End Sub

hi,

actually i have 6 array and array (5) carries the value of total amount and this part i already fix it by changing the codes in delete function to save the records again using the same function i did before....
Now the problem is when i delete 1 record out of 5 records and i exit the application just to check whether it have the remaining 4 records. The text file is empty.
I am really need your guidance as i need to accomplish this project. Below is my code for DELETE BUTTON. Please guide me. Thank You

Private Sub PnlDelete_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PnlDelete.MouseUp
        PnlDelete.BackgroundImage = My.Resources.BtnUp

        Try
            ListView1.SelectedItems(0).Remove()
         
            If File.Exists("C:\Program Files\ Autopay Terminal\BillPaymentRecords.txt") Then
                File.Delete("C:\Program Files\Autopay Terminal\BillPaymentRecords.txt")
            End If

            Dim writer As New StreamWriter("C:\Program Files\ Autopay Terminal\BillPaymentRecords.txt", True)
          
      'For Each item As ListViewItem In ListView1.Items
            '    Dim line As String = item.SubItems(0).Text & ";" _
            '    & item.SubItems(1).Text & ";" & item.SubItems(2).Text & ";" _
            '    & item.SubItems(3).Text & ";" & item.SubItems(4).Text
            '    writer.WriteLine(line)
            'Next


            Dim filename As String = "C:\Program Files\Autopay Terminal\BillPaymentRecords.txt"
            Dim data(6) As String
            Dim lineoftext As String
            data(0) = SESSION_AGENCY_NAME

            Select Case SESSION_AGENCY_NAME
                Case AGENCYNAME_A
                    data(1) = SESSION_AGENCY_ACCNO
                    data(2) = SESSION_AGENCY_BILLNO
                    data(3) = SESSION_AGENCY_BILLAMO
                    data(4) = SESSION_AGENCY_BILLINT
                    data(5) = SESSION_AGENCY_BILLTOT
                Case AGENCYNAME_B
                    data(1) = SESSION_AGENCY_ACCNO
                    data(2) = SESSION_AGENCY_BILLNO
                    data(3) = SESSION_AGENCY_BILLAMO
                    data(4) = SESSION_AGENCY_BILLINT
                    data(5) = SESSION_AGENCY_BILLTOT
                Case AGENCYNAME_C
                    data(1) = SESSION_AGENCY_ACCNO
                    data(2) = SESSION_AGENCY_BILLNO
                    data(3) = SESSION_AGENCY_BILLAMO
                    data(4) = SESSION_AGENCY_BILLINT
                    data(5) = SESSION_AGENCY_BILLTOT
                Case AGENCYNAME_D
                    data(1) = SESSION_AGENCY_ACCNO
                    data(2) = SESSION_AGENCY_BILLNO
                    data(3) = SESSION_AGENCY_BILLAMO
                    data(4) = SESSION_AGENCY_BILLINT
                    data(5) = SESSION_AGENCY_BILLTOT
                Case AGENCYNAME_E
                    data(1) = SESSION_AGENCY_ACCNO
                    data(2) = SESSION_AGENCY_BILLNO
                    data(3) = SESSION_AGENCY_BILLAMO
                    data(4) = SESSION_AGENCY_BILLINT
                    data(5) = SESSION_AGENCY_BILLTOT
                Case AGENCYNAME_F
                    data(1) = SESSION_AGENCY_ACCNO
                    data(2) = SESSION_AGENCY_BILLNO
                    data(3) = SESSION_AGENCY_BILLAMO
                    data(4) = SESSION_AGENCY_BILLINT
                    data(5) = SESSION_AGENCY_BILLTOT
                Case AGENCYNAME_G
                    data(1) = SESSION_AGENCY_ACCNO
                    data(2) = SESSION_AGENCY_BILLNO
                    data(3) = SESSION_AGENCY_BILLAMO
                    data(4) = SESSION_AGENCY_BILLINT
                    data(5) = SESSION_AGENCY_BILLTOT
            End Select

            Dim objwriter As New System.IO.StreamWriter(filename, True)
            lineoftext = String.Join(";", data)
            objwriter.WriteLine(lineoftext)

          
              dTotal = 0
                      GetFileContents()

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

            objwriter.Flush()
            objwriter.Close()

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


    End Sub

So you decided to do this instead?
No wonder it's doesn't work and that the textfile is empty.

If what I gave you works, then use it.
In the method GetFileContents you already have the code in place to calculate the total.

There is no need to actually store the totals, just let the program calculate it.

HI,

the reason for remarks the code that you have given is because it gives error
GetFileContents() -> Index was outside the bounds of the array because your code creates another column for numbering which i didnt use it when the time coming to record saving(i use only 6 array). Do you think the modification i did is wrong. Morever the code you given delete the particular record and creates new textfile which with new record but it doesn't recalculate the new amount.When i check the text file it contains record like this =====> 1;AGENCY_A;15154545;F6664253;50.12
Actual record save function save to text file like this ======> AGENCY_A;15154545;F6664253;50.12;1.00;51.20;
what i should do now.
PLEASE PLEASE PLEASE GUIDE ME ON THIS. i have spend more than 2 day doing the same things.

hi,

i still havent got the reply. Please i realy need help to solve this
Please guide !!!!!1

I have now tested this code, and it works flawlessly.
And as a side-note. That Select..Case statement you've got going.
No matter what agency you choose, data(1) to data(5) will contain the exact same thing.
The only reason to use such a statement, is if you change the order or if one of the data(1) to data(5) will contain something else.
This is clearly not the case.

Public Sub GetFileContents()
	Try
		Dim aStr() As String = New String() {}
		Dim FILE_NAME As String = "C:\Program Files\Autopay Terminal\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_AGENCY_NAME = aStr(0)
				SESSION_AGENCY_ACCNO = aStr(1)
				SESSION_AGENCY_BILLNO = aStr(2)
				SESSION_AGENCY_BILLAMO = aStr(5)
				SESSION_AGENCY_BILLINT = aStr(4)

				If aStr(5).Trim <> "" Then
					dTotal += CDbl(aStr(5).Trim)
				End If
				If aStr(4).Trim <> "" Then
					ServiceC += CInt(aStr(4).Trim)
				End If

				nTotlistrec = ListView1.Items.Count
				If nTotlistrec < 10 Then
					addListView(aStr(0), aStr(1), aStr(2), aStr(3), aStr(4), aStr(5))
				End If
			Loop
		End If
	Catch ex As Exception
		WriteToLogFile("GetFileContents() -> " & ex.Message)
	End Try
End Sub
Private Sub PnlDelete_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PnlDelete.MouseUp
	pnlDelete.BackgroundImage = My.Resources.BtnUp

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

		If File.Exists("C:\Program Files\Autopay Terminal\BillPaymentRecords.txt") Then
			File.Delete("C:\Program Files\Autopay Terminal\BillPaymentRecords.txt")
		End If

		Dim writer As New StreamWriter(Application.StartupPath & "\BillPaymentRecords.txt", True)
		For Each item As ListViewItem In ListView1.Items
			Dim line As String = item.SubItems(0).Text & ";" _
			& item.SubItems(1).Text & ";" & item.SubItems(2).Text & ";" _
			& item.SubItems(3).Text & ";" & item.SubItems(4).Text & ";" & item.SubItems(5).Text
			writer.WriteLine(line)
		Next
		writer.Flush()
		writer.Close()

		dTotal = 0
		ServiceC = 0
		GetFileContents()

		lblBillAmountValue.Text = Format(dTotal, "#,###,##0.00")
		lblServiceCharge.Text = Format(ServiceC, "#,###,##0.00")
	Catch ex As Exception
		MsgBox(ex.Message)
	End Try
End Sub
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.