Using Buttons and check box RRS feed

  • Question

  • I need to create a program with 4-6 buttons that have a different amount of money for purchasing an item.

    I also need a check box where if it is not checked I add $5 to the cost of the products for shipping. Otherwise it will be pick up in person.

    I have the design completed.

    I'm having trouble coding the check box so it adds to the total of the price for one of the 4 buttons.

    So when I click display full amount the two add together if box is NOT checked.

    If box is checked then the full amount will only be what is beside the button selected.

    I used ToString("C2") since there are two decimals for the cents.

    Any help would be appreciated.


    Monday, February 12, 2018 4:25 PM

All replies

  • Hi

    You need to post some relevant code.

    Regards Les, Livingston, Scotland

    Monday, February 12, 2018 5:28 PM
  • I also need a check box where if it is not checked I add $5 to the cost of the products for shipping. Otherwise it will be pick up in person.

    Don't try to put that code in the check box. The total should be calculated in one place, and that code should examine the state of the checkbox to determine how the calculation is done.  I presume that place is 'display'.  But without more information about the design of your user interface it is not possible to be very precise.  For instance you say "...the full amount will only be what is beside the button selected" which implies that the user can click one of several buttons to display the total, but then you say that "...when I click display full amount " which implies there is one button to display the amount.  The UI will determine how the code is going to work, so you have to provide more detail of that.

    Monday, February 12, 2018 8:42 PM
  • Hello,

    I would suggest loading buttons from a data source be it a text file or database etc then loop through the list and generate buttons then assign their tags as shown below.

    Proof of concept

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Button1.Tag = New Item With {.Id = 1, .Price = 12.5D, .CheckBox = CheckBox1}
            Button1.Text = CType(Button1.Tag, Item).Price.ToString("C2")
            Button2.Tag = New Item With {.Id = 2, .Price = 10.5D, .CheckBox = CheckBox2}
            Button2.Text = CType(Button2.Tag, Item).Price.ToString("C2")
        End Sub
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            Dim items As List(Of Item) = Controls.
                OfType(Of Button).
                Where(Function(but) but.Name <> "Button3").
                Select(Function(but) CType(but.Tag, Item)).
            For Each item As Item In items
                Console.WriteLine($"{item.Id}, {item.Price}, {item.ShippingCost}")
            Dim result = items.Select(Function(item) item.Total).Sum
        End Sub
    End Class
    Public Class Item
        Public Property Id As Integer
        Public Property Price As Decimal
        Public Property CheckBox As CheckBox
        Public ReadOnly Property ShippingCost As Integer
                Return If(CheckBox.Checked, 0, 5)
            End Get
        End Property
        Public ReadOnly Property Total As Decimal
                Return Price + ShippingCost
            End Get
        End Property
    End Class

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, February 12, 2018 9:00 PM
  • Hi Terri,

    I guess you are a student and it is a school assignment.

    Putting that aside, please share your design via cloud storage (e.g. OneDrive, Dropbox, and so on).
    When I understand how your design is, I will make code.



    Tuesday, February 13, 2018 1:47 AM