Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi guys,im new to programming I have to deliver this project where I have a lisbox that loads a textfile, with the price of the Items
Dim parts As String() = Strings.Split(Item, "€__")

here is the image for easier understanding (https://i.stack.imgur.com/IVXeG.png)
When i press the button discount, it opens a panel with a listbox and loads the same textfile as the listbox with the total above. I want to select the items from the listbox discount to send to the listbox total as a free item so (**-**Item, "€__") as a negative value so it can subtract from the 7,25 bill

From here you can see all my code

VB.NET
<pre>
Imports System.IO
Public Class Form1
    Dim Total
    Public Sub BtnConta_Click(sender As Object, e As EventArgs) Handles BtnConta.Click
        ' Specify the file path where you want to save the ListBox items
        Dim filePath As String = "C:\Mesa1.txt"

        ' Open the file for writing
        Using writer As New System.IO.StreamWriter(filePath)
            ' Loop through each item in the ListBox
            For Each item As Object In ListBoxConsumo.Items
                ' Write the item to the file
                writer.WriteLine(item.ToString())
            Next
        End Using

        MessageBox.Show("ListBox items saved to file.")

        Dim quoteArray As String() = File.ReadAllLines("C:\Mesa1.txt")
        For Each Item As String In quoteArray

            Dim parts As String() = Strings.Split(Item, "€__")
            Dim number As Decimal
            If Decimal.TryParse(parts(0), number) Then

            End If



            Total = (Total + number)
        Next
        TxtBoxTotal.Text = Total




    End Sub

    Private Sub BtnDesconto_Click(sender As Object, e As EventArgs) Handles BtnDesconto.Click
        PanelDesconto.Show()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ListBoxConsumo.Items.Add("1,45€__Pão")
    End Sub



   

    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Specify the file path from where you want to load the contents
        Dim filePath As String = "C:\Mesa1.txt"

        ' Check if the file exists
        If File.Exists(filePath) Then
            ' Read all lines from the file
            Dim lines() As String = File.ReadAllLines("C:\Mesa1.txt")

            ' Clear the existing items in the ListBox
            ListBoxConsumo.Items.Clear()

            ' Add each line to the ListBox
            For Each line As String In lines
                ListBoxConsumo.Items.Add(line)
            Next
        End If
    End Sub

    Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
        PanelDescontar.Hide()
    End Sub



    Private Sub BtnDescontar_Click(sender As Object, e As EventArgs) Handles BtnDescontar.Click

        ' Specify the file path from where you want to load the contents
        Dim filePath As String = "C:\Mesa1.txt"

        ' Check if the file exists
        If File.Exists(filePath) Then
            ' Read all lines from the file
            Dim lines() As String = File.ReadAllLines("C:\Mesa1.txt")

            ' Clear the existing items in the ListBox
            ListBoxFinal.Items.Clear()

            ' Add each line to the ListBox
            For Each line As String In lines
                ListBoxFinal.Items.Add(line)
            Next
        End If
        PanelDescontar.Show()
    End Sub

    Private Sub BtnApagar_Click(sender As Object, e As EventArgs) Handles BtnApagar.Click
        ' Move selected items from ListBoxFinal to ListBoxConsumo
        For Each selectedItem As Object In ListBoxFinal.SelectedItems
            ListBoxConsumo.Items.Add(selectedItem)
        Next

        '' Remove the selected items from ListBoxFinal
        'While ListBoxFinal.SelectedItems.Count > 0
        '    ListBoxFinal.Items.Remove(ListBoxFinal.SelectedItems(0))
        'End While
    End Sub
End Class


What I have tried:

I tried another way of giving a discount using a % to discount to the total, didnt go very well.
Posted
Updated 15-Jul-23 10:18am
Comments
Member 15627495 15-Jul-23 15:53pm    
in VB, Form are big Objects.
Form1 is your startup default loading Form.

Try to [ do it ] :
design your code with more Class
- one for IO_DISK ( read and write operations )

you can use Module / CLass to gather codes with same usability.

Really with the aim to have less code in Form1, several events for sure , but all of them will call one module, and from the module you build CLass using and functions. ( a "WORK_" module could be fine )

that one way to design and structure your code, it's part of optimising your code and all purposes.

Without doing this, you'll see your FOrm1 as overweighted, and out of memory, that is the danger you're approaching doing your way.

.Net is very compliant with OOP ( class , structure ). Make .Net grooving for you :)

I saw you use a messagebox in the middle of 2 IO disk operations, mind it twice. that is not a place for the messagebox, because of 'blocking/stoping' the computing operations. you can process the writing and the reading ,and use your msgbox at end it will be same for the User.

It's better as practices for a good use of .Net, by a more structured code.

1 solution

You were provided with the explanation for calculating discounts in your previous question at How do I calculate the percentage from a value in a textbox in visual basic[^]. Also, if you want help then you need to provide full details of what part of your code does not work. Statements such as, "didnt go very well.", do not tell us anything useful.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900