Results 1 to 11 of 11

Thread: Visual Studio- Windows Form- VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    8

    Cool Visual Studio- Windows Form- VB

    Hello Everyone,

    I am trying to create a simple form on visual studio where I have 10 buttons with prices associated with each button I also have a multi line box where when a button is clicked the item will appear in this box with said price at the same time the associated prices will add together and display in another textbox at the bottom as well as a running total for the shift in another text box . Moreorless this is for a simplistic pos system. no data base will be needed as this will be used just for a shift by shift basis. Mainly having issues with how to get each item button with price to talley up.

    Thanks in advance

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: Visual Studio- Windows Form- VB

    Please post the code you have so far, using the "Wrap [Code] Tags..." button (with a # symbol).

    What you are describing should be very straightforward, so it might be something as simple as your running total variable not being declared with proper scope.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    8

    Re: Visual Studio- Windows Form- VB

    Apologies I do see that now, Will I need to re post or can someone move it?

    This is what I have now. Sloppy to say the least. I really wasnt sure how to go about it. set variables or what. each button has its own price. Label1 is the running price total of current order and textbox1 is the running look at the current order i.e. item name and price. Label3 is the running total until it is cleared, like at the end of a shift

    Code:
    Public Class Form1
    
        Dim a As Integer
        Dim b As Integer
        Dim c As Integer
        Dim d As Integer
        Dim e As Integer
        Dim f As Integer
        Dim g As Integer
        Dim h As Integer
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
    
    
    
    
    
        Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs)
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            TextBox1.Text &= Environment.NewLine & "Item 1 $2.50"
    
    
            
    
    
        End Sub
    
    
    
    
    
        Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
            TextBox1.Text = Nothing
    
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            TextBox1.Text &= Environment.NewLine & "Item 2 $2.50"
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            TextBox1.Text &= Environment.NewLine & "Item 3 $2.50"
        End Sub

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Visual Studio- Windows Form- VB

    VB.Net code.

    To do a tally you would want to have a var that holds the current total and add to it in each button click event as needed and display the value of that var

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    8

    Re: Visual Studio- Windows Form- VB

    Code:
    Public Class frmregister
    
    
        Dim Total As Decimal
        Dim Subtract As Decimal
        Dim Balance As Decimal
    
    
        Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnadd.Click
            Balance += Decimal.Parse(txtamount.Text)
            txtbalance.Text = FormatCurrency(Balance)
            txtamount.Text = ""
        End Sub
    
    
        Private Sub btnSubtract_Click(sender As System.Object, e As System.EventArgs) Handles btnSubtract.Click
            Balance -= Decimal.Parse(txtamount.Text)
            txtbalance.Text = FormatCurrency(Balance)
            txtamount.Text = ""
        End Sub
    
        Private Sub txtAmount_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtamount.KeyPress
            If Asc(e.KeyChar) <> 8 AndAlso Asc(e.KeyChar) <> &H2E Then
                If Asc(e.KeyChar) = &H2B OrElse Asc(e.KeyChar) = &HD Then
                    e.Handled = True
                    btnadd.PerformClick()
                ElseIf Asc(e.KeyChar) = &H2D Then
                    e.Handled = True
                    btnSubtract.PerformClick()
                ElseIf Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
                    e.Handled = True
                End If
            End If
        End Sub
    
        Private Sub frmCashRegister_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub btnsubstract_Click(sender As Object, e As EventArgs) Handles btnSubtract.Click
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Balance += Decimal.Parse(txtamount.Text)
            txtbalance.Text = FormatCurrency(Balance)
            txtamount.Text = "2.50"
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            Balance += Decimal.Parse(txtamount.Text)
            txtbalance.Text = FormatCurrency(Balance)
            txtamount.Text = "2.50"
        End Sub
    End Class

    I went a different direction. Everything works as intended for the most part. Only thing is that each item button as you can see fills the price in an amount box but does not change the total box until you hit the item button again. Everything else should be cut and dry from here. just this little snag and may have it whipped. Thanks once again

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Visual Studio- Windows Form- VB

    Can you avoid using textboxes for this, or it it a requirement? Decimal.Parse will throw an exception if the textbox contains any invalid characters. What you should be using is a NumericUpDown control, in which case the .Value property already is a Decimal, so no conversion is needed.

    The keypress handler won't always work for you, either, since copying and pasting will not raise that event.

    Also, what are Button1 and Button3. They do the same thing as btnAdd, but the names are meaningless.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    8

    Re: Visual Studio- Windows Form- VB

    Button 1 and Button 3 are the start of all of the items that will be for sale to add toward the total. More or less this is a simple pos for a beer bar to count running totals for the day and help with transactions, change etc.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    8

    Re: Visual Studio- Windows Form- VB

    Name:  snip.jpg
Views: 179
Size:  13.1 KB

    here is a visual of what I am trying to do

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Visual Studio- Windows Form- VB

    So, it's the Button1 and so on (which sound like the item buttons) which are giving you trouble?

    You put an item into txtamount, but only after you have already added whatever is in there into the balance. If that amount that you put into the txtamount is what you want to have added to the balance, then just add it to the balance. Whether you ALSO put it into txtamount is up to you.

    You'd still be much better off using NumericUpDown controls to avoid the conversion stuff, and you might consider a ListBox rather than a multiline textbox. After all, a textbox suggests to the user that they can do something meaningful by editing the text. In this case, that wouldn't be wise at all. So, a label might make more sense than anything, and a Listbox would be easier to work with than a multi line textbox, but would still allow people to select items.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    8

    Re: Visual Studio- Windows Form- VB

    I also think some of the trouble is this line of code here on each sale button

    Balance += Decimal.Parse(txtamount.Text)

    also because of the += because hitting the button first does nothing then it adds the second item to the total because the operator is += at first the value is nothing so there is nothing to add to the grand total box. Been toying with the NumericUpDown but I guess I havent been able to do anything productive with it. I could eat exceptions for lunch now if I needed to.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Visual Studio- Windows Form- VB

    There's nothing particularly wrong with +=. I'm not sure what your description of that was, but what you are doing is right...enough, except that Decimal.Parse will throw an exception if the text can't be fully converted to a Decimal. The value isn't nothing, it is the default for a Decimal, which is 0, so the first time you press the button you convert the text to a decimal and add it to 0, which is what you want.

    Not sure what exceptions you are getting with a NUD. It's just a control. You use the .Value property for a NUD rather than the .Text property, but it's pretty much like the textbox. If you are having issues with Keypress in a NUD, that's fine, because Keypress will ultimately let you down for the textbox, too, so you already have to find a different way to do that.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width