Coding a Button to display a list of items in a ListBox












0














I'm having tremendous trouble in coding a Button that will display items, that I have selected in a program, in a ListBox.



Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
If rdoNormal.Checked = True Then
txtTotal.Text = 1.05
ElseIf rdoThicc.Checked = True Then
txtTotal.Text = 1.05
ElseIf rdoCrusty.Checked = True Then
txtTotal.Text = 1.05
ElseIf rdoCob.Checked = True Then
txtTotal.Text = 1.05
End If

If rdoSausage.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 1.8
ElseIf rdoTurkey.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 3.25
ElseIf rdoCheese.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 2.4
ElseIf rdoPopchick.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 0.84
End If

If chkMayo.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 0.6
End If

If chkButter.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 0.6
End If
End Sub

Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
lstReceipt.Items.Clear()
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Application.Exit()
End Sub

Private Sub btnReceipt_Click(sender As Object, e As EventArgs) Handles btnReceipt.Click
**(What do I put here?)**
End Sub
End Class









share|improve this question
























  • Thanks for the edits, I guess...
    – KludgyOne67095
    Nov 23 '18 at 23:19










  • What I'ld propose is to, well, change everything :) You could have a Receipt class object which is bound to each group of options. When one of the options is modified, the bound class would aumatically recalculate all the values, building a Total and, as a consequence, update the bound ListBox which presents the selected elements. I understand what this implies and you're probably not willing to make this kind of change. You could use some Fields that keep track of the options selected (value and related text) and have the Buttons just sum the values then pass the text + values to the ListBox.
    – Jimi
    Nov 23 '18 at 23:50












  • Are you dealing with more than a single item? The way you have it setup now is going to be kind of clunky to maintain a running total for multiple items.
    – Nathan Champion
    Nov 24 '18 at 0:00










  • You should set Option Strict On right away - it will help you write less kludgy code. Then learn about the many, more versatile NET alternatives to that legacy Val function
    – WelcomeOverflow
    Nov 24 '18 at 0:09










  • What exactly do you want to display in the list box, you did not state that In your question
    – preciousbetine
    Nov 24 '18 at 18:32


















0














I'm having tremendous trouble in coding a Button that will display items, that I have selected in a program, in a ListBox.



Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
If rdoNormal.Checked = True Then
txtTotal.Text = 1.05
ElseIf rdoThicc.Checked = True Then
txtTotal.Text = 1.05
ElseIf rdoCrusty.Checked = True Then
txtTotal.Text = 1.05
ElseIf rdoCob.Checked = True Then
txtTotal.Text = 1.05
End If

If rdoSausage.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 1.8
ElseIf rdoTurkey.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 3.25
ElseIf rdoCheese.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 2.4
ElseIf rdoPopchick.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 0.84
End If

If chkMayo.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 0.6
End If

If chkButter.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 0.6
End If
End Sub

Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
lstReceipt.Items.Clear()
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Application.Exit()
End Sub

Private Sub btnReceipt_Click(sender As Object, e As EventArgs) Handles btnReceipt.Click
**(What do I put here?)**
End Sub
End Class









share|improve this question
























  • Thanks for the edits, I guess...
    – KludgyOne67095
    Nov 23 '18 at 23:19










  • What I'ld propose is to, well, change everything :) You could have a Receipt class object which is bound to each group of options. When one of the options is modified, the bound class would aumatically recalculate all the values, building a Total and, as a consequence, update the bound ListBox which presents the selected elements. I understand what this implies and you're probably not willing to make this kind of change. You could use some Fields that keep track of the options selected (value and related text) and have the Buttons just sum the values then pass the text + values to the ListBox.
    – Jimi
    Nov 23 '18 at 23:50












  • Are you dealing with more than a single item? The way you have it setup now is going to be kind of clunky to maintain a running total for multiple items.
    – Nathan Champion
    Nov 24 '18 at 0:00










  • You should set Option Strict On right away - it will help you write less kludgy code. Then learn about the many, more versatile NET alternatives to that legacy Val function
    – WelcomeOverflow
    Nov 24 '18 at 0:09










  • What exactly do you want to display in the list box, you did not state that In your question
    – preciousbetine
    Nov 24 '18 at 18:32
















0












0








0


1





I'm having tremendous trouble in coding a Button that will display items, that I have selected in a program, in a ListBox.



Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
If rdoNormal.Checked = True Then
txtTotal.Text = 1.05
ElseIf rdoThicc.Checked = True Then
txtTotal.Text = 1.05
ElseIf rdoCrusty.Checked = True Then
txtTotal.Text = 1.05
ElseIf rdoCob.Checked = True Then
txtTotal.Text = 1.05
End If

If rdoSausage.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 1.8
ElseIf rdoTurkey.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 3.25
ElseIf rdoCheese.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 2.4
ElseIf rdoPopchick.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 0.84
End If

If chkMayo.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 0.6
End If

If chkButter.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 0.6
End If
End Sub

Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
lstReceipt.Items.Clear()
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Application.Exit()
End Sub

Private Sub btnReceipt_Click(sender As Object, e As EventArgs) Handles btnReceipt.Click
**(What do I put here?)**
End Sub
End Class









share|improve this question















I'm having tremendous trouble in coding a Button that will display items, that I have selected in a program, in a ListBox.



Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
If rdoNormal.Checked = True Then
txtTotal.Text = 1.05
ElseIf rdoThicc.Checked = True Then
txtTotal.Text = 1.05
ElseIf rdoCrusty.Checked = True Then
txtTotal.Text = 1.05
ElseIf rdoCob.Checked = True Then
txtTotal.Text = 1.05
End If

If rdoSausage.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 1.8
ElseIf rdoTurkey.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 3.25
ElseIf rdoCheese.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 2.4
ElseIf rdoPopchick.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 0.84
End If

If chkMayo.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 0.6
End If

If chkButter.Checked = True Then
txtTotal.Text = Val(txtTotal.Text) + 0.6
End If
End Sub

Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
lstReceipt.Items.Clear()
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Application.Exit()
End Sub

Private Sub btnReceipt_Click(sender As Object, e As EventArgs) Handles btnReceipt.Click
**(What do I put here?)**
End Sub
End Class






vb.net winforms checkbox listbox radio-button






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 23:07









Jimi

7,28241833




7,28241833










asked Nov 23 '18 at 22:58









KludgyOne67095KludgyOne67095

12




12












  • Thanks for the edits, I guess...
    – KludgyOne67095
    Nov 23 '18 at 23:19










  • What I'ld propose is to, well, change everything :) You could have a Receipt class object which is bound to each group of options. When one of the options is modified, the bound class would aumatically recalculate all the values, building a Total and, as a consequence, update the bound ListBox which presents the selected elements. I understand what this implies and you're probably not willing to make this kind of change. You could use some Fields that keep track of the options selected (value and related text) and have the Buttons just sum the values then pass the text + values to the ListBox.
    – Jimi
    Nov 23 '18 at 23:50












  • Are you dealing with more than a single item? The way you have it setup now is going to be kind of clunky to maintain a running total for multiple items.
    – Nathan Champion
    Nov 24 '18 at 0:00










  • You should set Option Strict On right away - it will help you write less kludgy code. Then learn about the many, more versatile NET alternatives to that legacy Val function
    – WelcomeOverflow
    Nov 24 '18 at 0:09










  • What exactly do you want to display in the list box, you did not state that In your question
    – preciousbetine
    Nov 24 '18 at 18:32




















  • Thanks for the edits, I guess...
    – KludgyOne67095
    Nov 23 '18 at 23:19










  • What I'ld propose is to, well, change everything :) You could have a Receipt class object which is bound to each group of options. When one of the options is modified, the bound class would aumatically recalculate all the values, building a Total and, as a consequence, update the bound ListBox which presents the selected elements. I understand what this implies and you're probably not willing to make this kind of change. You could use some Fields that keep track of the options selected (value and related text) and have the Buttons just sum the values then pass the text + values to the ListBox.
    – Jimi
    Nov 23 '18 at 23:50












  • Are you dealing with more than a single item? The way you have it setup now is going to be kind of clunky to maintain a running total for multiple items.
    – Nathan Champion
    Nov 24 '18 at 0:00










  • You should set Option Strict On right away - it will help you write less kludgy code. Then learn about the many, more versatile NET alternatives to that legacy Val function
    – WelcomeOverflow
    Nov 24 '18 at 0:09










  • What exactly do you want to display in the list box, you did not state that In your question
    – preciousbetine
    Nov 24 '18 at 18:32


















Thanks for the edits, I guess...
– KludgyOne67095
Nov 23 '18 at 23:19




Thanks for the edits, I guess...
– KludgyOne67095
Nov 23 '18 at 23:19












What I'ld propose is to, well, change everything :) You could have a Receipt class object which is bound to each group of options. When one of the options is modified, the bound class would aumatically recalculate all the values, building a Total and, as a consequence, update the bound ListBox which presents the selected elements. I understand what this implies and you're probably not willing to make this kind of change. You could use some Fields that keep track of the options selected (value and related text) and have the Buttons just sum the values then pass the text + values to the ListBox.
– Jimi
Nov 23 '18 at 23:50






What I'ld propose is to, well, change everything :) You could have a Receipt class object which is bound to each group of options. When one of the options is modified, the bound class would aumatically recalculate all the values, building a Total and, as a consequence, update the bound ListBox which presents the selected elements. I understand what this implies and you're probably not willing to make this kind of change. You could use some Fields that keep track of the options selected (value and related text) and have the Buttons just sum the values then pass the text + values to the ListBox.
– Jimi
Nov 23 '18 at 23:50














Are you dealing with more than a single item? The way you have it setup now is going to be kind of clunky to maintain a running total for multiple items.
– Nathan Champion
Nov 24 '18 at 0:00




Are you dealing with more than a single item? The way you have it setup now is going to be kind of clunky to maintain a running total for multiple items.
– Nathan Champion
Nov 24 '18 at 0:00












You should set Option Strict On right away - it will help you write less kludgy code. Then learn about the many, more versatile NET alternatives to that legacy Val function
– WelcomeOverflow
Nov 24 '18 at 0:09




You should set Option Strict On right away - it will help you write less kludgy code. Then learn about the many, more versatile NET alternatives to that legacy Val function
– WelcomeOverflow
Nov 24 '18 at 0:09












What exactly do you want to display in the list box, you did not state that In your question
– preciousbetine
Nov 24 '18 at 18:32






What exactly do you want to display in the list box, you did not state that In your question
– preciousbetine
Nov 24 '18 at 18:32














3 Answers
3






active

oldest

votes


















0














I'd probably set everything up like this.



Imports System.Text

Public Class Form1
Public ItemList As New List(Of MyItem)

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim NewItem As MyItem = CalculateTotal()
ItemList.Add(NewItem)

End Sub

Private Function CalculateTotal() As MyItem

Dim TotalPrice As Decimal = 1.05D
Dim Description As New StringBuilder

Select Case True
Case rdoSausage.Checked
TotalPrice += 1.8D
Description.Append("Sausage ")
Case rdoTurkey.Checked
TotalPrice += 3.25D
Description.Append("Turkey ")
Case rdoCheese.Checked
TotalPrice += 2.4D
Description.Append("Cheese ")
Case rdoPopchick.Checked
TotalPrice += 0.84D
Description.Append("Popcorn Chicken ")
End Select

If chkMayo.Checked = True Then
TotalPrice += 0.6D
Description.Append("Mayo ")
End If

If chkButter.Checked = True Then
TotalPrice += 0.6D
Description.Append("Butter ")
End If



Return New MyItem With {.Description = Description.ToString(), .Price = TotalPrice}
End Function

Private Sub btnReceipt_Click(sender As Object, e As EventArgs) Handles btnReceipt.Click
For Each Item In ItemList
ListBox1.Items.Add(Item.Description & ": " & Item.Price.ToString("C"))
Next
End Sub
End Class



Public Class MyItem
Public Property Description() As String
Public Property Price() As Decimal

Public Overrides Function ToString() As String
Return Description & Price.ToString()
End Function
End Class





share|improve this answer





























    0














    Created a ReceiptItem class. This could just as easily been a Structure which would probably be better (lighter value type). Then a List(Of ReceiptItem) stores the objects.



    A tiny Fucntion gets the selected radio button. This could be done in a loop which is what the Linq stuff is doing internally.



    To make the receipt look better it should really be displayed in a ListView or DataGridView. You can simulate columns in a ListBox with padding and such but you would have to use a monospaced font which is ugly in itself.



        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim lstItems As New List(Of ReceiptItem)
    Dim SelectedRadioButton1 As RadioButton = GetSelectedRadioButton(GroupBox1)
    lstItems.Add(New ReceiptItem(SelectedRadioButton1.Text, 1.05D))
    Dim SelectedRadioButton2 As RadioButton = GetSelectedRadioButton(GroupBox2)
    lstItems.Add(New ReceiptItem(SelectedRadioButton2.Text, CDec(SelectedRadioButton2.Tag)))
    If chkMayo.Checked = True Then
    lstItems.Add(New ReceiptItem("Mayonaise", 0.6D))
    End If
    If chkButter.Checked = True Then
    lstItems.Add(New ReceiptItem("Butter", 0.6D))
    End If
    FillReceiptListBox(lstItems)
    End Sub

    Private Function GetSelectedRadioButton(ctrl As Control) As RadioButton
    Dim rButton As RadioButton = ctrl.Controls.OfType(Of RadioButton).FirstOrDefault(Function(r) r.Checked = True)
    Return rButton
    End Function

    Private Sub FillReceiptListBox(lst As List(Of ReceiptItem))
    Dim Total As Decimal
    For Each item In lst
    Total += item.Price
    Dim strPrice As String = item.Price.ToString("N2")
    ListBox1.Items.Add($"{item.Description} -- {item.Price.ToString("N2")}")
    Next
    ListBox1.Items.Add($"Total {Total.ToString("C")}")
    End Sub

    Class ReceiptItem
    Public Property Description As String
    Public Property Price As Decimal
    Public Sub New(des As String, p As Decimal)
    Description = des
    Price = p
    End Sub
    End Class





    share|improve this answer





























      0














      This is the solution I came to with some help from an experienced peer (Thank you for attempting to help me):



          Private Sub btnReceipt_Click(sender As Object, e As EventArgs) Handles btnReceipt.Click
      If rdoNormal.Checked = True Then
      lstReceipt.Text = Val(rdoNormal.Text)
      lstReceipt.Items.Add("Normal Bread £1.05")
      End If

      If rdoThick.Checked = True Then
      lstReceipt.Text = Val(rdoThick.Text)
      lstReceipt.Items.Add("Thick Bread £1.05")
      End If

      If rdoCrusty.Checked = True Then
      lstReceipt.Text = Val(rdoCrusty.Text)
      lstReceipt.Items.Add("Crusty Bread £1.05")
      End If

      If rdoCob.Checked = True Then
      lstReceipt.Text = Val(rdoCob.Text)
      lstReceipt.Items.Add("Cob Bread £1.05")
      End If

      If rdoSausage.Checked = True Then
      lstReceipt.Text = Val(rdoSausage.Text)
      lstReceipt.Items.Add("Sausage £1.80")
      End If

      If rdoTurkey.Checked = True Then
      lstReceipt.Text = Val(rdoTurkey.Text)
      lstReceipt.Items.Add("Turkey £3.25")
      End If

      If rdoCheese.Checked = True Then
      lstReceipt.Text = Val(rdoCheese.Text)
      lstReceipt.Items.Add("Cheese £2.40")
      End If

      If rdoPopchick.Checked = True Then
      lstReceipt.Text = Val(rdoPopchick.Text)
      lstReceipt.Items.Add("Popcorn Chicken £0.84")
      End If

      If chkMayo.Checked = True Then
      lstReceipt.Text = Val(chkMayo.Text)
      lstReceipt.Items.Add("Mayo £0.60")
      End If

      If chkButter.Checked = True Then
      lstReceipt.Text = Val(chkButter.Text)
      lstReceipt.Items.Add("Butter £0.60")
      End If

      If rdoKetchup.Checked = True Then
      lstReceipt.Text = Val(rdoKetchup.Text)
      lstReceipt.Items.Add("Ketchup £0.00")
      End If

      If rdoBBQ.Checked = True Then
      lstReceipt.Text = Val(rdoBBQ.Text)
      lstReceipt.Items.Add("BBQ £0.00")
      End If

      If rdoBrown.Checked = True Then
      lstReceipt.Text = Val(rdoBrown.Text)
      lstReceipt.Items.Add("Brown £0.00")
      End If
      End Sub
      End Class





      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453696%2fcoding-a-button-to-display-a-list-of-items-in-a-listbox%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        I'd probably set everything up like this.



        Imports System.Text

        Public Class Form1
        Public ItemList As New List(Of MyItem)

        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Dim NewItem As MyItem = CalculateTotal()
        ItemList.Add(NewItem)

        End Sub

        Private Function CalculateTotal() As MyItem

        Dim TotalPrice As Decimal = 1.05D
        Dim Description As New StringBuilder

        Select Case True
        Case rdoSausage.Checked
        TotalPrice += 1.8D
        Description.Append("Sausage ")
        Case rdoTurkey.Checked
        TotalPrice += 3.25D
        Description.Append("Turkey ")
        Case rdoCheese.Checked
        TotalPrice += 2.4D
        Description.Append("Cheese ")
        Case rdoPopchick.Checked
        TotalPrice += 0.84D
        Description.Append("Popcorn Chicken ")
        End Select

        If chkMayo.Checked = True Then
        TotalPrice += 0.6D
        Description.Append("Mayo ")
        End If

        If chkButter.Checked = True Then
        TotalPrice += 0.6D
        Description.Append("Butter ")
        End If



        Return New MyItem With {.Description = Description.ToString(), .Price = TotalPrice}
        End Function

        Private Sub btnReceipt_Click(sender As Object, e As EventArgs) Handles btnReceipt.Click
        For Each Item In ItemList
        ListBox1.Items.Add(Item.Description & ": " & Item.Price.ToString("C"))
        Next
        End Sub
        End Class



        Public Class MyItem
        Public Property Description() As String
        Public Property Price() As Decimal

        Public Overrides Function ToString() As String
        Return Description & Price.ToString()
        End Function
        End Class





        share|improve this answer


























          0














          I'd probably set everything up like this.



          Imports System.Text

          Public Class Form1
          Public ItemList As New List(Of MyItem)

          Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
          Dim NewItem As MyItem = CalculateTotal()
          ItemList.Add(NewItem)

          End Sub

          Private Function CalculateTotal() As MyItem

          Dim TotalPrice As Decimal = 1.05D
          Dim Description As New StringBuilder

          Select Case True
          Case rdoSausage.Checked
          TotalPrice += 1.8D
          Description.Append("Sausage ")
          Case rdoTurkey.Checked
          TotalPrice += 3.25D
          Description.Append("Turkey ")
          Case rdoCheese.Checked
          TotalPrice += 2.4D
          Description.Append("Cheese ")
          Case rdoPopchick.Checked
          TotalPrice += 0.84D
          Description.Append("Popcorn Chicken ")
          End Select

          If chkMayo.Checked = True Then
          TotalPrice += 0.6D
          Description.Append("Mayo ")
          End If

          If chkButter.Checked = True Then
          TotalPrice += 0.6D
          Description.Append("Butter ")
          End If



          Return New MyItem With {.Description = Description.ToString(), .Price = TotalPrice}
          End Function

          Private Sub btnReceipt_Click(sender As Object, e As EventArgs) Handles btnReceipt.Click
          For Each Item In ItemList
          ListBox1.Items.Add(Item.Description & ": " & Item.Price.ToString("C"))
          Next
          End Sub
          End Class



          Public Class MyItem
          Public Property Description() As String
          Public Property Price() As Decimal

          Public Overrides Function ToString() As String
          Return Description & Price.ToString()
          End Function
          End Class





          share|improve this answer
























            0












            0








            0






            I'd probably set everything up like this.



            Imports System.Text

            Public Class Form1
            Public ItemList As New List(Of MyItem)

            Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
            Dim NewItem As MyItem = CalculateTotal()
            ItemList.Add(NewItem)

            End Sub

            Private Function CalculateTotal() As MyItem

            Dim TotalPrice As Decimal = 1.05D
            Dim Description As New StringBuilder

            Select Case True
            Case rdoSausage.Checked
            TotalPrice += 1.8D
            Description.Append("Sausage ")
            Case rdoTurkey.Checked
            TotalPrice += 3.25D
            Description.Append("Turkey ")
            Case rdoCheese.Checked
            TotalPrice += 2.4D
            Description.Append("Cheese ")
            Case rdoPopchick.Checked
            TotalPrice += 0.84D
            Description.Append("Popcorn Chicken ")
            End Select

            If chkMayo.Checked = True Then
            TotalPrice += 0.6D
            Description.Append("Mayo ")
            End If

            If chkButter.Checked = True Then
            TotalPrice += 0.6D
            Description.Append("Butter ")
            End If



            Return New MyItem With {.Description = Description.ToString(), .Price = TotalPrice}
            End Function

            Private Sub btnReceipt_Click(sender As Object, e As EventArgs) Handles btnReceipt.Click
            For Each Item In ItemList
            ListBox1.Items.Add(Item.Description & ": " & Item.Price.ToString("C"))
            Next
            End Sub
            End Class



            Public Class MyItem
            Public Property Description() As String
            Public Property Price() As Decimal

            Public Overrides Function ToString() As String
            Return Description & Price.ToString()
            End Function
            End Class





            share|improve this answer












            I'd probably set everything up like this.



            Imports System.Text

            Public Class Form1
            Public ItemList As New List(Of MyItem)

            Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
            Dim NewItem As MyItem = CalculateTotal()
            ItemList.Add(NewItem)

            End Sub

            Private Function CalculateTotal() As MyItem

            Dim TotalPrice As Decimal = 1.05D
            Dim Description As New StringBuilder

            Select Case True
            Case rdoSausage.Checked
            TotalPrice += 1.8D
            Description.Append("Sausage ")
            Case rdoTurkey.Checked
            TotalPrice += 3.25D
            Description.Append("Turkey ")
            Case rdoCheese.Checked
            TotalPrice += 2.4D
            Description.Append("Cheese ")
            Case rdoPopchick.Checked
            TotalPrice += 0.84D
            Description.Append("Popcorn Chicken ")
            End Select

            If chkMayo.Checked = True Then
            TotalPrice += 0.6D
            Description.Append("Mayo ")
            End If

            If chkButter.Checked = True Then
            TotalPrice += 0.6D
            Description.Append("Butter ")
            End If



            Return New MyItem With {.Description = Description.ToString(), .Price = TotalPrice}
            End Function

            Private Sub btnReceipt_Click(sender As Object, e As EventArgs) Handles btnReceipt.Click
            For Each Item In ItemList
            ListBox1.Items.Add(Item.Description & ": " & Item.Price.ToString("C"))
            Next
            End Sub
            End Class



            Public Class MyItem
            Public Property Description() As String
            Public Property Price() As Decimal

            Public Overrides Function ToString() As String
            Return Description & Price.ToString()
            End Function
            End Class






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 24 '18 at 0:47









            Nathan ChampionNathan Champion

            515111




            515111

























                0














                Created a ReceiptItem class. This could just as easily been a Structure which would probably be better (lighter value type). Then a List(Of ReceiptItem) stores the objects.



                A tiny Fucntion gets the selected radio button. This could be done in a loop which is what the Linq stuff is doing internally.



                To make the receipt look better it should really be displayed in a ListView or DataGridView. You can simulate columns in a ListBox with padding and such but you would have to use a monospaced font which is ugly in itself.



                    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
                Dim lstItems As New List(Of ReceiptItem)
                Dim SelectedRadioButton1 As RadioButton = GetSelectedRadioButton(GroupBox1)
                lstItems.Add(New ReceiptItem(SelectedRadioButton1.Text, 1.05D))
                Dim SelectedRadioButton2 As RadioButton = GetSelectedRadioButton(GroupBox2)
                lstItems.Add(New ReceiptItem(SelectedRadioButton2.Text, CDec(SelectedRadioButton2.Tag)))
                If chkMayo.Checked = True Then
                lstItems.Add(New ReceiptItem("Mayonaise", 0.6D))
                End If
                If chkButter.Checked = True Then
                lstItems.Add(New ReceiptItem("Butter", 0.6D))
                End If
                FillReceiptListBox(lstItems)
                End Sub

                Private Function GetSelectedRadioButton(ctrl As Control) As RadioButton
                Dim rButton As RadioButton = ctrl.Controls.OfType(Of RadioButton).FirstOrDefault(Function(r) r.Checked = True)
                Return rButton
                End Function

                Private Sub FillReceiptListBox(lst As List(Of ReceiptItem))
                Dim Total As Decimal
                For Each item In lst
                Total += item.Price
                Dim strPrice As String = item.Price.ToString("N2")
                ListBox1.Items.Add($"{item.Description} -- {item.Price.ToString("N2")}")
                Next
                ListBox1.Items.Add($"Total {Total.ToString("C")}")
                End Sub

                Class ReceiptItem
                Public Property Description As String
                Public Property Price As Decimal
                Public Sub New(des As String, p As Decimal)
                Description = des
                Price = p
                End Sub
                End Class





                share|improve this answer


























                  0














                  Created a ReceiptItem class. This could just as easily been a Structure which would probably be better (lighter value type). Then a List(Of ReceiptItem) stores the objects.



                  A tiny Fucntion gets the selected radio button. This could be done in a loop which is what the Linq stuff is doing internally.



                  To make the receipt look better it should really be displayed in a ListView or DataGridView. You can simulate columns in a ListBox with padding and such but you would have to use a monospaced font which is ugly in itself.



                      Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
                  Dim lstItems As New List(Of ReceiptItem)
                  Dim SelectedRadioButton1 As RadioButton = GetSelectedRadioButton(GroupBox1)
                  lstItems.Add(New ReceiptItem(SelectedRadioButton1.Text, 1.05D))
                  Dim SelectedRadioButton2 As RadioButton = GetSelectedRadioButton(GroupBox2)
                  lstItems.Add(New ReceiptItem(SelectedRadioButton2.Text, CDec(SelectedRadioButton2.Tag)))
                  If chkMayo.Checked = True Then
                  lstItems.Add(New ReceiptItem("Mayonaise", 0.6D))
                  End If
                  If chkButter.Checked = True Then
                  lstItems.Add(New ReceiptItem("Butter", 0.6D))
                  End If
                  FillReceiptListBox(lstItems)
                  End Sub

                  Private Function GetSelectedRadioButton(ctrl As Control) As RadioButton
                  Dim rButton As RadioButton = ctrl.Controls.OfType(Of RadioButton).FirstOrDefault(Function(r) r.Checked = True)
                  Return rButton
                  End Function

                  Private Sub FillReceiptListBox(lst As List(Of ReceiptItem))
                  Dim Total As Decimal
                  For Each item In lst
                  Total += item.Price
                  Dim strPrice As String = item.Price.ToString("N2")
                  ListBox1.Items.Add($"{item.Description} -- {item.Price.ToString("N2")}")
                  Next
                  ListBox1.Items.Add($"Total {Total.ToString("C")}")
                  End Sub

                  Class ReceiptItem
                  Public Property Description As String
                  Public Property Price As Decimal
                  Public Sub New(des As String, p As Decimal)
                  Description = des
                  Price = p
                  End Sub
                  End Class





                  share|improve this answer
























                    0












                    0








                    0






                    Created a ReceiptItem class. This could just as easily been a Structure which would probably be better (lighter value type). Then a List(Of ReceiptItem) stores the objects.



                    A tiny Fucntion gets the selected radio button. This could be done in a loop which is what the Linq stuff is doing internally.



                    To make the receipt look better it should really be displayed in a ListView or DataGridView. You can simulate columns in a ListBox with padding and such but you would have to use a monospaced font which is ugly in itself.



                        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
                    Dim lstItems As New List(Of ReceiptItem)
                    Dim SelectedRadioButton1 As RadioButton = GetSelectedRadioButton(GroupBox1)
                    lstItems.Add(New ReceiptItem(SelectedRadioButton1.Text, 1.05D))
                    Dim SelectedRadioButton2 As RadioButton = GetSelectedRadioButton(GroupBox2)
                    lstItems.Add(New ReceiptItem(SelectedRadioButton2.Text, CDec(SelectedRadioButton2.Tag)))
                    If chkMayo.Checked = True Then
                    lstItems.Add(New ReceiptItem("Mayonaise", 0.6D))
                    End If
                    If chkButter.Checked = True Then
                    lstItems.Add(New ReceiptItem("Butter", 0.6D))
                    End If
                    FillReceiptListBox(lstItems)
                    End Sub

                    Private Function GetSelectedRadioButton(ctrl As Control) As RadioButton
                    Dim rButton As RadioButton = ctrl.Controls.OfType(Of RadioButton).FirstOrDefault(Function(r) r.Checked = True)
                    Return rButton
                    End Function

                    Private Sub FillReceiptListBox(lst As List(Of ReceiptItem))
                    Dim Total As Decimal
                    For Each item In lst
                    Total += item.Price
                    Dim strPrice As String = item.Price.ToString("N2")
                    ListBox1.Items.Add($"{item.Description} -- {item.Price.ToString("N2")}")
                    Next
                    ListBox1.Items.Add($"Total {Total.ToString("C")}")
                    End Sub

                    Class ReceiptItem
                    Public Property Description As String
                    Public Property Price As Decimal
                    Public Sub New(des As String, p As Decimal)
                    Description = des
                    Price = p
                    End Sub
                    End Class





                    share|improve this answer












                    Created a ReceiptItem class. This could just as easily been a Structure which would probably be better (lighter value type). Then a List(Of ReceiptItem) stores the objects.



                    A tiny Fucntion gets the selected radio button. This could be done in a loop which is what the Linq stuff is doing internally.



                    To make the receipt look better it should really be displayed in a ListView or DataGridView. You can simulate columns in a ListBox with padding and such but you would have to use a monospaced font which is ugly in itself.



                        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
                    Dim lstItems As New List(Of ReceiptItem)
                    Dim SelectedRadioButton1 As RadioButton = GetSelectedRadioButton(GroupBox1)
                    lstItems.Add(New ReceiptItem(SelectedRadioButton1.Text, 1.05D))
                    Dim SelectedRadioButton2 As RadioButton = GetSelectedRadioButton(GroupBox2)
                    lstItems.Add(New ReceiptItem(SelectedRadioButton2.Text, CDec(SelectedRadioButton2.Tag)))
                    If chkMayo.Checked = True Then
                    lstItems.Add(New ReceiptItem("Mayonaise", 0.6D))
                    End If
                    If chkButter.Checked = True Then
                    lstItems.Add(New ReceiptItem("Butter", 0.6D))
                    End If
                    FillReceiptListBox(lstItems)
                    End Sub

                    Private Function GetSelectedRadioButton(ctrl As Control) As RadioButton
                    Dim rButton As RadioButton = ctrl.Controls.OfType(Of RadioButton).FirstOrDefault(Function(r) r.Checked = True)
                    Return rButton
                    End Function

                    Private Sub FillReceiptListBox(lst As List(Of ReceiptItem))
                    Dim Total As Decimal
                    For Each item In lst
                    Total += item.Price
                    Dim strPrice As String = item.Price.ToString("N2")
                    ListBox1.Items.Add($"{item.Description} -- {item.Price.ToString("N2")}")
                    Next
                    ListBox1.Items.Add($"Total {Total.ToString("C")}")
                    End Sub

                    Class ReceiptItem
                    Public Property Description As String
                    Public Property Price As Decimal
                    Public Sub New(des As String, p As Decimal)
                    Description = des
                    Price = p
                    End Sub
                    End Class






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 24 '18 at 5:04









                    MaryMary

                    3,0492718




                    3,0492718























                        0














                        This is the solution I came to with some help from an experienced peer (Thank you for attempting to help me):



                            Private Sub btnReceipt_Click(sender As Object, e As EventArgs) Handles btnReceipt.Click
                        If rdoNormal.Checked = True Then
                        lstReceipt.Text = Val(rdoNormal.Text)
                        lstReceipt.Items.Add("Normal Bread £1.05")
                        End If

                        If rdoThick.Checked = True Then
                        lstReceipt.Text = Val(rdoThick.Text)
                        lstReceipt.Items.Add("Thick Bread £1.05")
                        End If

                        If rdoCrusty.Checked = True Then
                        lstReceipt.Text = Val(rdoCrusty.Text)
                        lstReceipt.Items.Add("Crusty Bread £1.05")
                        End If

                        If rdoCob.Checked = True Then
                        lstReceipt.Text = Val(rdoCob.Text)
                        lstReceipt.Items.Add("Cob Bread £1.05")
                        End If

                        If rdoSausage.Checked = True Then
                        lstReceipt.Text = Val(rdoSausage.Text)
                        lstReceipt.Items.Add("Sausage £1.80")
                        End If

                        If rdoTurkey.Checked = True Then
                        lstReceipt.Text = Val(rdoTurkey.Text)
                        lstReceipt.Items.Add("Turkey £3.25")
                        End If

                        If rdoCheese.Checked = True Then
                        lstReceipt.Text = Val(rdoCheese.Text)
                        lstReceipt.Items.Add("Cheese £2.40")
                        End If

                        If rdoPopchick.Checked = True Then
                        lstReceipt.Text = Val(rdoPopchick.Text)
                        lstReceipt.Items.Add("Popcorn Chicken £0.84")
                        End If

                        If chkMayo.Checked = True Then
                        lstReceipt.Text = Val(chkMayo.Text)
                        lstReceipt.Items.Add("Mayo £0.60")
                        End If

                        If chkButter.Checked = True Then
                        lstReceipt.Text = Val(chkButter.Text)
                        lstReceipt.Items.Add("Butter £0.60")
                        End If

                        If rdoKetchup.Checked = True Then
                        lstReceipt.Text = Val(rdoKetchup.Text)
                        lstReceipt.Items.Add("Ketchup £0.00")
                        End If

                        If rdoBBQ.Checked = True Then
                        lstReceipt.Text = Val(rdoBBQ.Text)
                        lstReceipt.Items.Add("BBQ £0.00")
                        End If

                        If rdoBrown.Checked = True Then
                        lstReceipt.Text = Val(rdoBrown.Text)
                        lstReceipt.Items.Add("Brown £0.00")
                        End If
                        End Sub
                        End Class





                        share|improve this answer


























                          0














                          This is the solution I came to with some help from an experienced peer (Thank you for attempting to help me):



                              Private Sub btnReceipt_Click(sender As Object, e As EventArgs) Handles btnReceipt.Click
                          If rdoNormal.Checked = True Then
                          lstReceipt.Text = Val(rdoNormal.Text)
                          lstReceipt.Items.Add("Normal Bread £1.05")
                          End If

                          If rdoThick.Checked = True Then
                          lstReceipt.Text = Val(rdoThick.Text)
                          lstReceipt.Items.Add("Thick Bread £1.05")
                          End If

                          If rdoCrusty.Checked = True Then
                          lstReceipt.Text = Val(rdoCrusty.Text)
                          lstReceipt.Items.Add("Crusty Bread £1.05")
                          End If

                          If rdoCob.Checked = True Then
                          lstReceipt.Text = Val(rdoCob.Text)
                          lstReceipt.Items.Add("Cob Bread £1.05")
                          End If

                          If rdoSausage.Checked = True Then
                          lstReceipt.Text = Val(rdoSausage.Text)
                          lstReceipt.Items.Add("Sausage £1.80")
                          End If

                          If rdoTurkey.Checked = True Then
                          lstReceipt.Text = Val(rdoTurkey.Text)
                          lstReceipt.Items.Add("Turkey £3.25")
                          End If

                          If rdoCheese.Checked = True Then
                          lstReceipt.Text = Val(rdoCheese.Text)
                          lstReceipt.Items.Add("Cheese £2.40")
                          End If

                          If rdoPopchick.Checked = True Then
                          lstReceipt.Text = Val(rdoPopchick.Text)
                          lstReceipt.Items.Add("Popcorn Chicken £0.84")
                          End If

                          If chkMayo.Checked = True Then
                          lstReceipt.Text = Val(chkMayo.Text)
                          lstReceipt.Items.Add("Mayo £0.60")
                          End If

                          If chkButter.Checked = True Then
                          lstReceipt.Text = Val(chkButter.Text)
                          lstReceipt.Items.Add("Butter £0.60")
                          End If

                          If rdoKetchup.Checked = True Then
                          lstReceipt.Text = Val(rdoKetchup.Text)
                          lstReceipt.Items.Add("Ketchup £0.00")
                          End If

                          If rdoBBQ.Checked = True Then
                          lstReceipt.Text = Val(rdoBBQ.Text)
                          lstReceipt.Items.Add("BBQ £0.00")
                          End If

                          If rdoBrown.Checked = True Then
                          lstReceipt.Text = Val(rdoBrown.Text)
                          lstReceipt.Items.Add("Brown £0.00")
                          End If
                          End Sub
                          End Class





                          share|improve this answer
























                            0












                            0








                            0






                            This is the solution I came to with some help from an experienced peer (Thank you for attempting to help me):



                                Private Sub btnReceipt_Click(sender As Object, e As EventArgs) Handles btnReceipt.Click
                            If rdoNormal.Checked = True Then
                            lstReceipt.Text = Val(rdoNormal.Text)
                            lstReceipt.Items.Add("Normal Bread £1.05")
                            End If

                            If rdoThick.Checked = True Then
                            lstReceipt.Text = Val(rdoThick.Text)
                            lstReceipt.Items.Add("Thick Bread £1.05")
                            End If

                            If rdoCrusty.Checked = True Then
                            lstReceipt.Text = Val(rdoCrusty.Text)
                            lstReceipt.Items.Add("Crusty Bread £1.05")
                            End If

                            If rdoCob.Checked = True Then
                            lstReceipt.Text = Val(rdoCob.Text)
                            lstReceipt.Items.Add("Cob Bread £1.05")
                            End If

                            If rdoSausage.Checked = True Then
                            lstReceipt.Text = Val(rdoSausage.Text)
                            lstReceipt.Items.Add("Sausage £1.80")
                            End If

                            If rdoTurkey.Checked = True Then
                            lstReceipt.Text = Val(rdoTurkey.Text)
                            lstReceipt.Items.Add("Turkey £3.25")
                            End If

                            If rdoCheese.Checked = True Then
                            lstReceipt.Text = Val(rdoCheese.Text)
                            lstReceipt.Items.Add("Cheese £2.40")
                            End If

                            If rdoPopchick.Checked = True Then
                            lstReceipt.Text = Val(rdoPopchick.Text)
                            lstReceipt.Items.Add("Popcorn Chicken £0.84")
                            End If

                            If chkMayo.Checked = True Then
                            lstReceipt.Text = Val(chkMayo.Text)
                            lstReceipt.Items.Add("Mayo £0.60")
                            End If

                            If chkButter.Checked = True Then
                            lstReceipt.Text = Val(chkButter.Text)
                            lstReceipt.Items.Add("Butter £0.60")
                            End If

                            If rdoKetchup.Checked = True Then
                            lstReceipt.Text = Val(rdoKetchup.Text)
                            lstReceipt.Items.Add("Ketchup £0.00")
                            End If

                            If rdoBBQ.Checked = True Then
                            lstReceipt.Text = Val(rdoBBQ.Text)
                            lstReceipt.Items.Add("BBQ £0.00")
                            End If

                            If rdoBrown.Checked = True Then
                            lstReceipt.Text = Val(rdoBrown.Text)
                            lstReceipt.Items.Add("Brown £0.00")
                            End If
                            End Sub
                            End Class





                            share|improve this answer












                            This is the solution I came to with some help from an experienced peer (Thank you for attempting to help me):



                                Private Sub btnReceipt_Click(sender As Object, e As EventArgs) Handles btnReceipt.Click
                            If rdoNormal.Checked = True Then
                            lstReceipt.Text = Val(rdoNormal.Text)
                            lstReceipt.Items.Add("Normal Bread £1.05")
                            End If

                            If rdoThick.Checked = True Then
                            lstReceipt.Text = Val(rdoThick.Text)
                            lstReceipt.Items.Add("Thick Bread £1.05")
                            End If

                            If rdoCrusty.Checked = True Then
                            lstReceipt.Text = Val(rdoCrusty.Text)
                            lstReceipt.Items.Add("Crusty Bread £1.05")
                            End If

                            If rdoCob.Checked = True Then
                            lstReceipt.Text = Val(rdoCob.Text)
                            lstReceipt.Items.Add("Cob Bread £1.05")
                            End If

                            If rdoSausage.Checked = True Then
                            lstReceipt.Text = Val(rdoSausage.Text)
                            lstReceipt.Items.Add("Sausage £1.80")
                            End If

                            If rdoTurkey.Checked = True Then
                            lstReceipt.Text = Val(rdoTurkey.Text)
                            lstReceipt.Items.Add("Turkey £3.25")
                            End If

                            If rdoCheese.Checked = True Then
                            lstReceipt.Text = Val(rdoCheese.Text)
                            lstReceipt.Items.Add("Cheese £2.40")
                            End If

                            If rdoPopchick.Checked = True Then
                            lstReceipt.Text = Val(rdoPopchick.Text)
                            lstReceipt.Items.Add("Popcorn Chicken £0.84")
                            End If

                            If chkMayo.Checked = True Then
                            lstReceipt.Text = Val(chkMayo.Text)
                            lstReceipt.Items.Add("Mayo £0.60")
                            End If

                            If chkButter.Checked = True Then
                            lstReceipt.Text = Val(chkButter.Text)
                            lstReceipt.Items.Add("Butter £0.60")
                            End If

                            If rdoKetchup.Checked = True Then
                            lstReceipt.Text = Val(rdoKetchup.Text)
                            lstReceipt.Items.Add("Ketchup £0.00")
                            End If

                            If rdoBBQ.Checked = True Then
                            lstReceipt.Text = Val(rdoBBQ.Text)
                            lstReceipt.Items.Add("BBQ £0.00")
                            End If

                            If rdoBrown.Checked = True Then
                            lstReceipt.Text = Val(rdoBrown.Text)
                            lstReceipt.Items.Add("Brown £0.00")
                            End If
                            End Sub
                            End Class






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 30 '18 at 17:09









                            KludgyOne67095KludgyOne67095

                            12




                            12






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453696%2fcoding-a-button-to-display-a-list-of-items-in-a-listbox%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Contact image not getting when fetch all contact list from iPhone by CNContact

                                count number of partitions of a set with n elements into k subsets

                                A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks