Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have this program, that is part of the actual program, but i cant find a solution for it, i have 2 textboxes, in one i have the total, in the second i have to insert the % i want to give a discount to the total, i set the % by using buttons.
This is a Program where i use button1 to send to the listbox, save it to a textfile using the save button and when giving the bill i press the the button conta.
What i want is to press the button desconto and when it opens the panel give 10% discont from the Total textBox
This is the the link for a print of my forms:

https://ibb.co/wNKcdyt

This is my code:
VB.NET
Imports System.IO
Public Class Form1
    Dim Total
    Public Sub BtnConta_Click(sender As Object, e As EventArgs) Handles BtnConta.Click

        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 BtnSaveLstBox_Click(sender As Object, e As EventArgs) Handles BtnSaveLstBox.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.")

    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

    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        Dim loc As Integer
        loc = TextBoxTeclado1.Text.Length
        TextBoxTeclado1.Text = TextBoxTeclado1.Text.Remove(loc - 1, 1)
    End Sub

    Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click

        Dim percentagem As Double
        TextBoxTeclado1.Text = percentagem

        If IsNumeric(TextBoxTeclado1.Text) Then
            percentagem = CDbl(TextBoxTeclado1.Text)
        Else
            percentagem = 0 ' No discount
        End If

        If percentagem < 100 Then
            Total = Total * (100 - percentagem)
        Else
            ' Item is free
            Total = 0
        End If

        MsgBox(Total)

    End Sub

    Private Sub BtnSair_Click(sender As Object, e As EventArgs) Handles BtnSair.Click
        PanelDesconto.Hide()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        TextBoxTeclado1.Text = 1
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        TextBoxTeclado1.Text = 2
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        TextBoxTeclado1.Text = 3
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        TextBoxTeclado1.Text = 4
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        TextBoxTeclado1.Text = 5
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        TextBoxTeclado1.Text = 6
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        TextBoxTeclado1.Text = 7
    End Sub

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        TextBoxTeclado1.Text = 8
    End Sub

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        TextBoxTeclado1.Text = 9
    End Sub

    Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
        TextBoxTeclado1.Text = 0
    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(filePath)

            ' 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
End Class


What I have tried:

I tried
Dim percentagem As Double

If IsNumeric(TextBoxTeclado1.Text) Then
    percentagem = CDbl(TextBoxTeclado1.Text)
Else
    percentagem = 0 ' No discount
End If

If percentagem < 100 Then
    Total = Total * (100 - percentagem)
Else
    ' Item is free
    Total = 0
End If

MsgBox(Total)
Posted
Updated 14-Jul-23 21:45pm
v2

How do you calculate a percentage? Here is an example:

1. If you want to give a 10% discount from $100.00 the calculation is:
discount amount = value * (discount / 100)
   = 100.00 * (10 / 100)
   = 100.00 * 0.10
   = 10.00

2. To calculate the net amount, ie, the discounted amount, you can do it 2 ways:
discounted amount = value - (value * (discount / 100))
   = 100.00 - (100.00 * (10 / 100))
   = 100.00 - (100.00 * 0.10)
   = 100.00 - 10.00
   = 90.00
or ...
discounted amount = value * (ABS(100 - discount) / 100)
   = 100.00 * (ABS(100 - 10) / 100)
   = 100.00 * (90 / 100)
   = 100.00 * 0.90
   = 90.00
where ABS[^] is the positive of a value.

This should make it easy to modify your code.

UPDATE

As Dave pointed out in the comments below, you need a valid number in the TextBox, then you need to convert it to a number value. I'm going to use a Double to support decimals:
VB
Dim testText1 = "10.5"
Dim testText2 = "bad_10.5"

Dim value1 As Double
Dim value2 As Double

If Double.TryParse(testText1, value1) Then
    'value1 contains the number
    MsgBox(value1)
Else
    MsgBox("bad text")
End If

If Double.TryParse(testText2, value2) Then
    'value2 will not contains the invalid text
    MsgBox(value2)
Else
    MsgBox("bad text")
End If

The above example shows how to parse and test text to a number. I will leave it to you to update your code.
 
Share this answer
 
v3
Comments
Airton Aguiar 14-Jul-23 19:53pm    
Sorry if I didnt clarify, i want to give any discount possible, either 1 or 100, this value is inserted in a textbox
Graeme_Grant 14-Jul-23 20:01pm    
Yes, and I have given you a step-by-step how to calculate it. You put the gross amount as value position and the discount (1 to 100) in the discount position.

So, your VB code would be:
Dim calculatedTotal = Total * (Math.Abs(100 - percentagem) / 100)
Graeme_Grant 14-Jul-23 20:43pm    
How did you go?
Dave Kreskowiak 14-Jul-23 22:33pm    
I think part of the problem is he doesn't understand that the value coming back from the TextBox isn't a numerical value you can do calculations on but a string.
Graeme_Grant 14-Jul-23 22:43pm    
Yep, there is that ... I only address the calc
To add to what Graeme has said, if you want the user to enter numbers, then a textbox is probably not the way to go - the NumericUpDown Control[^] prevents the user entering invalid data at all, and provides a number directly from it's Value property.
 
Share this answer
 
You should not use Double types in financial calculations, as it is inherently inaccurate. Use the Decimal type as described in Numeric Data Types - Visual Basic | Microsoft Learn[^].
 
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