Кодирование кнопки для отображения списка элементов в ListBox - PullRequest
0 голосов
/ 24 ноября 2018

У меня огромные проблемы при кодировании кнопки, которая будет отображать элементы, выбранные мной в программе, в 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

Ответы [ 3 ]

0 голосов
/ 24 ноября 2018

Создан класс ReceiptItem.Это может быть просто структура, которая, вероятно, будет лучше (тип с более легким значением).Затем List (Of ReceiptItem) сохраняет объекты.

Крошечный Fucntion получает выбранный переключатель.Это может быть сделано в цикле, что и делает внутренняя часть Linq.

Чтобы чек выглядел лучше, он должен действительно отображаться в ListView или DataGridView.Вы можете смоделировать столбцы в ListBox с отступами и так далее, но вам придется использовать моноширинный шрифт, который сам по себе ужасен.

    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
0 голосов
/ 30 ноября 2018

Это решение, к которому я пришел с некоторой помощью от опытного коллеги (спасибо за попытку помочь мне):

    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
0 голосов
/ 24 ноября 2018

Я бы наверное все так настроил.

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
...