如何计算数量乘以价格文本框到列表框

时间:2013-10-07 21:00:50

标签: .net vb.net

我有以下代码。我有一个数量文本框和价格文本框。我需要通过quatity将价格加倍,以在特定项目的列表框中显示总数。列表框最后总计。这作为一个静态数组就好了,但我不得不允许不同价格和数量的用户输入。我在中间提供了我认为可能有效的代码,但却没有。我错了什么? ''''''''''''PriceTextBox.Text = PriceTextBox * QuantityTextBox.Text''''''''

 Public Class form1

'Module level declarations
Structure Product
    Dim ProductIDString As String
    Dim DescriptionString As String
    Dim QuantityInteger As Integer
    Dim PriceDecimal As Decimal
    Dim W As IO.StreamWriter
End Structure

Private NumberProductsInteger As Integer = 38
Private InventoryProduct(NumberProductsInteger) As Product
Private TotalDueDecimal As Decimal 'total due for a customer

'Array to store the total sales for each product
Private ProductSalesTotalDecimal(NumberProductsInteger) As Decimal




Private Sub Button3_Click(sender As Object, e As EventArgs) Handles PurchaseBtn.Click
    'Test to determine if a product was found.
    If DescriptionTextBox.Text = String.Empty Then

        'Cannot purchase, product was not found
        MessageBox.Show("You must select a valid product before purchasing.", "Cannot Purchase", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        ProductIDTextBox.Focus()
        ProductIDTextBox.SelectAll()
    Else
        'Can purchase the product
        'Build a string to display in the listbox control
        Dim ProductString As String = ProductIDTextBox.Text & "  " & DescriptionTextBox.Text & "    " & QuantityTextBox.Text & "                " & PriceTextBox.Text
        PurchaseListBox.Items.Add(ProductString)

        'Accumulate the total value of this customer order
        'and display it to the output textbox


        ''''''''''''PriceTextBox.Text = PriceTextBox * QuantityTextBox.Text'''''''''''''



        TotalDueDecimal += Decimal.Parse(PriceTextBox.Text, Globalization.NumberStyles.Currency)
        TotalDueTextBox.Text = TotalDueDecimal.ToString("C2")

        'Accumulate total sales by product to an array
        Dim IndexInteger As Integer = ProductIDComboBox.SelectedIndex
        ProductSalesTotalDecimal(IndexInteger) += Decimal.Parse(PriceTextBox.Text, Globalization.NumberStyles.Currency)

        'Here you can clear the form of product info if you think
        'that is a good way to do the processing
        ProductIDComboBox.SelectedIndex = -1
        ProductIDTextBox.Clear()
        DescriptionTextBox.Clear()
        PriceTextBox.Clear()
        QuantityTextBox.Clear()
        ProductIDTextBox.Focus()
    End If
End Sub

正如您所看到的,总应付金额会增加价格,但不会增加数量因素

enter image description here

Public Class form1     结构产品

    'Module level declarations
    Dim ProductIDString As String
    Dim DescriptionString As String
    Dim QuantityInteger As Integer
    Dim PriceDecimal As Double
    Dim Cost As Double
    Dim W As IO.StreamWriter


    Public Sub New(PID As String, Desc As String, Qty As Integer, Price As Double)
        ProductIDString = PID
        DescriptionString = Desc
        QuantityInteger = Qty
        PriceDecimal = Price
        Cost = Qty * Price
    End Sub





    Public Overrides Function ToString() As String
        Return ProductIDString & " " & DescriptionString & " " & QuantityInteger.ToString & " " & PriceDecimal.ToString("C2") & " " & Cost.ToString("C2")
    End Function
End Structure


Dim Products As New List(Of Product)

'Products..AddRange(
' New Product("test1", "Test1a", 10, 1.5),
'  NewProduct("test2", "test2a", 20, 2.25)




' PurchaseListBox.DataSource = Products
' TotalDueTextBox.Text = Products.Sum(Function(x) x.Cost).ToString("C2")

Public Sub UpdateLB()
    PurchaseListBox.DataSource = Nothing
    PurchaseListBox.DataSource = Products
    TotalDueTextBox.Text = Products.Sum(Function(x) x.Cost).ToString("C2")
End Sub

Private NumberProductsInteger As Integer = 38
Private InventoryProduct(NumberProductsInteger) As Product
Private TotalDueDecimal As Decimal 'total due for a customer
Private Property W As IO.StreamWriter
Private ProductSalesTotalDecimal(NumberProductsInteger) As Decimal  'Array to store the total sales for each product

Private Sub BookStore1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.TimeDateLBL.Text = CStr(Date.Now)

    'Initialize values - InventoryProduct array
    InventoryProduct(0).ProductIDString = "102-1091" & vbTab
    InventoryProduct(0).DescriptionString = "2x4-92 & Chr(96)  5/8  SPF  STUD" & vbTab & vbTab
    'InventoryProduct(0).QuantityInteger = 
    ' InventoryProduct(0).PriceDecimal = 65.55D

    InventoryProduct(1).ProductIDString = "102-1127" & vbTab
    InventoryProduct(1).DescriptionString = "2x4-12'  #2 &BTR  SPF  CONSTR  LUMB" & Space(33)
    'InventoryProduct(1).QuantityInteger = 15
    'InventoryProduct(1).PriceDecimal = 18.99D

    InventoryProduct(2).ProductIDString = "102-1130" & vbTab
    InventoryProduct(2).DescriptionString = "2x4-14'  #2&BTR  SPF  CONSTR LUMB" & Space(35)
    ' InventoryProduct(2).QuantityInteger = 25
    ' InventoryProduct(2).PriceDecimal = 25.99D

    InventoryProduct(3).ProductIDString = "102-1143" & vbTab
    InventoryProduct(3).DescriptionString = "2x4-16'  5/8  #2&BTR  SPF  CONSTR LUMB" & Space(27)
    ' InventoryProduct(3).QuantityInteger = 8
    'InventoryProduct(3).PriceDecimal = 5.49D

    InventoryProduct(4).ProductIDString = "102-1305" & vbTab
    InventoryProduct(4).DescriptionString = "2x4-104  5/8  SPF  STUD" & Space(64)
    ' InventoryProduct(4).QuantityInteger = 15
    ' InventoryProduct(4).PriceDecimal = 35.5D

    InventoryProduct(5).ProductIDString = "102-1758" & vbTab
    InventoryProduct(5).DescriptionString = "2x6-8'  STD/#2&BTR  SPF  CONSTR LUMB" & Space(33)
    ' InventoryProduct(5).QuantityInteger = 25
    ' InventoryProduct(5).PriceDecimal = 22.99D

    InventoryProduct(6).ProductIDString = "102-1774" & vbTab
    InventoryProduct(6).DescriptionString = "2x6-12' #2&BTR  SPF  CONSTR LUMB" & Space(33)
    ' InventoryProduct(6).QuantityInteger = 50
    ' InventoryProduct(6).PriceDecimal = 85D

    InventoryProduct(7).ProductIDString = "102-1923" & vbTab
    InventoryProduct(7).DescriptionString = "2x8-16' #2&BTR  SPF  CONSTR LUMB"
    ' InventoryProduct(7).QuantityInteger = 75
    ' InventoryProduct(7).PriceDecimal = 79.4D

    InventoryProduct(8).ProductIDString = "102-2184" & vbTab
    InventoryProduct(8).DescriptionString = "2x12-16'  #2&BTR  SPF  CONSTR LUMB  "
    ' InventoryProduct(8).QuantityInteger = 75
    'InventoryProduct(8).PriceDecimal = 79.4D

    InventoryProduct(9).ProductIDString = "106-1501" & vbTab
    InventoryProduct(9).DescriptionString = "1-3/4x11-7/8  10'  LVL"
    'InventoryProduct(9).QuantityInteger = 75
    ' InventoryProduct(9).PriceDecimal = 79.4D

    InventoryProduct(10).ProductIDString = "106-2089" & vbTab
    InventoryProduct(10).DescriptionString = "1-3/4x11-7/8  10'  LVL"
    ' InventoryProduct(10).QuantityInteger = 75
    ' InventoryProduct(10).PriceDecimal = 79.4D

    InventoryProduct(11).ProductIDString = "106-2115" & vbTab
    InventoryProduct(11).DescriptionString = "1-3/4x14  14'  LVL"
    ' InventoryProduct(11).QuantityInteger = 75
    ' InventoryProduct(11).PriceDecimal = 79.4D

    InventoryProduct(12).ProductIDString = "106-2144" & vbTab
    InventoryProduct(12).DescriptionString = "1-3/4x14  18'  LVL"
    ' InventoryProduct(12).QuantityInteger = 75
    ' InventoryProduct(12).PriceDecimal = 79.4D

    InventoryProduct(13).ProductIDString = "106-2209" & vbTab
    InventoryProduct(13).DescriptionString = "1-3/4x14  26'  20'  LVL"
    ' InventoryProduct(13).QuantityInteger = 75
    ' InventoryProduct(13).PriceDecimal = 79.4D

    InventoryProduct(14).ProductIDString = "106-6742" & vbTab
    InventoryProduct(14).DescriptionString = "2-1/2X14-18' I Joist  NI-60/PRi-60"
    ' InventoryProduct(14).QuantityInteger = 75
    ' InventoryProduct(14).PriceDecimal = 79.4D

    InventoryProduct(15).ProductIDString = "106-6768" & vbTab
    InventoryProduct(15).DescriptionString = "2-1/2X14-18' I Joist  NI-60/PRi-60"
    ' InventoryProduct(15).QuantityInteger = 75
    'InventoryProduct(15).PriceDecimal = 79.4D

    InventoryProduct(16).ProductIDString = "106-6807" & vbTab
    InventoryProduct(16).DescriptionString = "2-1/2X14-24' I Joist  NI-60/PRi-60"
    ' InventoryProduct(16).QuantityInteger = 75
    ' InventoryProduct(16).PriceDecimal = 79.4D

    InventoryProduct(17).ProductIDString = "106-8106" & vbTab
    InventoryProduct(17).DescriptionString = "2-1/2X14-12' I Joist  RIM BOARD"
    ' InventoryProduct(17).QuantityInteger = 75
    'InventoryProduct(17).PriceDecimal = 79.4D

    InventoryProduct(18).ProductIDString = "111-0834" & vbTab
    InventoryProduct(18).DescriptionString = "2x4-12'  AC2 TREATED  AG LIFETIME WTY"
    ' InventoryProduct(18).QuantityInteger = 75
    ' InventoryProduct(18).PriceDecimal = 79.4D

    InventoryProduct(19).ProductIDString = "111-0847" & vbTab
    InventoryProduct(19).DescriptionString = "2x4-14'  AC2 TREATED  AG LIFETIME WTY"""
    ' InventoryProduct(19).QuantityInteger = 75
    ' InventoryProduct(19).PriceDecimal = 79.4D

    InventoryProduct(20).ProductIDString = "124-2728" & vbTab
    InventoryProduct(20).DescriptionString = "7/16  (14/32)-4 XB  OSB 3-WHITE STRIPES"
    'InventoryProduct(20).QuantityInteger = 75
    ' InventoryProduct(20).PriceDecimal = 79.4D

    InventoryProduct(21).ProductIDString = "124-2809" & vbTab
    InventoryProduct(21).DescriptionString = "1/2  (16/32)-4 XB  OSB 2-WHITE BLAK  STRIPES" & Space(17)

    ' InventoryProduct(21).QuantityInteger = 75
    ' InventoryProduct(21).PriceDecimal = 79.4D

    InventoryProduct(22).ProductIDString = "124-2867" & vbTab
    InventoryProduct(22).DescriptionString = "3/4  (23/32)-4 XB  OSB T&G 5-WHITE STRIPES"
    ' InventoryProduct(22).QuantityInteger = 75
    ' InventoryProduct(22).PriceDecimal = 79.4D

    InventoryProduct(23).ProductIDString = "131-1248" & vbTab
    InventoryProduct(23).DescriptionString = "1/2x4'-12'  GYPsum  78LBL"
    ' InventoryProduct(23).QuantityInteger = 75
    ' InventoryProduct(23).PriceDecimal = 79.4D

    InventoryProduct(24).ProductIDString = "131-1256" & vbTab
    InventoryProduct(24).DescriptionString = "1/2x4'-8'  MOLD/MR  78LBL"
    ' InventoryProduct(24).QuantityInteger = 75
    ' InventoryProduct(24).PriceDecimal = 79.4D

    InventoryProduct(25).ProductIDString = "131-1259" & vbTab
    InventoryProduct(25).DescriptionString = "5/8x4'-8'  MOLD/MR TYPR X   105LBL"
    ' InventoryProduct(25).QuantityInteger = 75
    ' InventoryProduct(25).PriceDecimal = 79.4D

    InventoryProduct(26).ProductIDString = "131-1303" & vbTab
    InventoryProduct(26).DescriptionString = "5/8X4-12'  GYPS PC TYPE  X 105LBL"
    '  InventoryProduct(26).QuantityInteger = 75
    ' InventoryProduct(26).PriceDecimal = 79.4D

    InventoryProduct(27).ProductIDString = "131-2454" & vbTab
    InventoryProduct(27).DescriptionString = "8' METAL 1-1/4 CORNERBEAD"
    '  InventoryProduct(27).QuantityInteger = 75
    ' InventoryProduct(27).PriceDecimal = 79.4D

    InventoryProduct(28).ProductIDString = "131-2726" & vbTab
    InventoryProduct(28).DescriptionString = "ALL PURPOSE PALE-BLACK 7GAL"
    ' InventoryProduct(28).QuantityInteger = 75
    ' InventoryProduct(28).PriceDecimal = 79.4D

    InventoryProduct(29).ProductIDString = "131-2849" & vbTab
    InventoryProduct(29).DescriptionString = "EZ SAND  90  18#"
    ' InventoryProduct(29).QuantityInteger = 75
    ' InventoryProduct(29).PriceDecimal = 79.4D

    InventoryProduct(30).ProductIDString = "131-2881" & vbTab
    InventoryProduct(30).DescriptionString = "SPRAY-TEXTURE MEDIUM  40#"
    ' InventoryProduct(30).QuantityInteger = 75
    ' InventoryProduct(30).PriceDecimal = 79.4D

    InventoryProduct(31).ProductIDString = "131-3097" & vbTab
    InventoryProduct(31).DescriptionString = "PROROC JOINT TAPE  250'"
    ' InventoryProduct(31).QuantityInteger = 75
    ' InventoryProduct(31).PriceDecimal = 79.4D

    InventoryProduct(32).ProductIDString = "165-0372" & vbTab
    InventoryProduct(32).DescriptionString = "M/H208  ANJ COLUMN 3INCH DIAM -8'4"
    ' InventoryProduct(32).QuantityInteger = 75
    '  InventoryProduct(32).PriceDecimal = 79.4D

    InventoryProduct(33).ProductIDString = "227-1442" & vbTab
    InventoryProduct(33).DescriptionString = "NAIL 1-1/2 JOIST HANGER   5LB GALV"
    ' InventoryProduct(33).QuantityInteger = 75
    ' InventoryProduct(33).PriceDecimal = 79.4D

    InventoryProduct(34).ProductIDString = "228-3142" & vbTab
    InventoryProduct(34).DescriptionString = "JOIST HNGR  TM 2.5X14  THO54140"
    ' InventoryProduct(34).QuantityInteger = 75
    ' InventoryProduct(34).PriceDecimal = 79.4D

    InventoryProduct(35).ProductIDString = "229-1202" & vbTab
    InventoryProduct(35).DescriptionString = "NAIL 8D  V.C.  SINKERS   50LBL BOX"
    ' InventoryProduct(35).QuantityInteger = 75
    ' InventoryProduct(35).PriceDecimal = 79.4D

    InventoryProduct(36).ProductIDString = "229-1244" & vbTab
    InventoryProduct(36).DescriptionString = "NAIL 16D  V.C.  SINKERS   50LBL BOX"""
    ' InventoryProduct(36).QuantityInteger = 75
    ' InventoryProduct(36).PriceDecimal = 79.4D

    InventoryProduct(37).ProductIDString = "229-1303" & vbTab
    InventoryProduct(37).DescriptionString = "NAIL 16D  GALVANZED BOX  50LBL BOX"""
    ' InventoryProduct(37).QuantityInteger = 75
    ' InventoryProduct(37).PriceDecimal = 79.4D
End Sub

Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click

    'Search the ProductIDString property of the inventoryProduct
    'array to see if the value of ProductIDTextBox matches an ID 
    'in the array

    'Start variables to control the search
    Dim FoundBoolean As Boolean = False  'Control how long to search
    Dim RowInteger As Integer = 0        'Current row in the search

    'Loop to do the search
    Do Until FoundBoolean = True Or RowInteger > NumberProductsInteger

        'Compare textBox to array
        If ProductIDTextBox.Text = InventoryProduct(RowInteger).ProductIDString Then

            'found a match - display other data to the readonly textboxes
            ProductIDComboBox.SelectedItem = ProductIDTextBox.Text
            DescriptionTextBox.Text = InventoryProduct(RowInteger).DescriptionString
            QuantityTextBox.Text = InventoryProduct(RowInteger).QuantityInteger.ToString
            PriceTextBox.Text = InventoryProduct(RowInteger).PriceDecimal.ToString("C2")

            'change variable to indicate we have a match
            FoundBoolean = True
        Else
            'no match yet
            RowInteger += 1
        End If
    Loop

    'After the search determine if the ProductID was found

    If FoundBoolean = False Then  'no match was found
        'Clear the textbox controls that display product information
        'except for the ProductID textbox
        DescriptionTextBox.Clear()
        QuantityTextBox.Clear()
        PriceTextBox.Clear()
        ProductIDComboBox.SelectedIndex = -1

        'Display message that the ProductID is not valid
        MessageBox.Show("Reenter a valid SKU Product ID.", "Invalid Identifier", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        ProductIDTextBox.Focus()
        ProductIDTextBox.SelectAll()
    End If
End Sub

Private Sub PurchaseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PurchaseToolStripMenuItem.Click

    'Test to determine if a product was found.
    If DescriptionTextBox.Text = String.Empty Then

        'Cannot purchase, product was not found
        MessageBox.Show("You must select a valid product before purchasing.", "Cannot Purchase", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        ProductIDTextBox.Focus()
        ProductIDTextBox.SelectAll()
    Else
        'Can purchase the product
        'Build a string to display in the listbox control
        Dim ProductString As String = ProductIDTextBox.Text & " - " & DescriptionTextBox.Text & " - " & PriceTextBox.Text
        PurchaseListBox.Items.Add(ProductString)

        'Accumulate the total value of this customer order
        'and display it to the output textbox
        TotalDueDecimal += Decimal.Parse(PriceTextBox.Text, Globalization.NumberStyles.Currency)
        TotalDueTextBox.Text = TotalDueDecimal.ToString("C2")

        'Accumulate total sales by product to an array
        Dim IndexInteger As Integer = ProductIDComboBox.SelectedIndex
        ProductSalesTotalDecimal(IndexInteger) += Decimal.Parse(PriceTextBox.Text, Globalization.NumberStyles.Currency)

        'Here you can clear the form of product info if you think
        'that is a good way to do the processing
        ProductIDComboBox.SelectedIndex = -1
        ProductIDTextBox.Clear()
        DescriptionTextBox.Clear()
        PriceTextBox.Clear()
        QuantityTextBox.Clear()
        ProductIDTextBox.Focus()
    End If
End Sub

Private Sub ResetToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetToolStripMenuItem.Click
    'Clear all text box and combobox controls
    ProductIDComboBox.SelectedIndex = -1
    ProductIDTextBox.Clear()
    DescriptionTextBox.Clear()
    PriceTextBox.Clear()
    QuantityTextBox.Clear()
    ' TotalDueTextBox.Clear()

    'Clear the list box control
    PurchaseListBox.Items.Clear()

    'Reset the total due module-level variable to zero
    TotalDueDecimal = 0

    'Set the focus to the product ID text box
    ProductIDTextBox.Focus()
End Sub

Private Sub SearchToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchToolStripMenuItem.Click
    'Call the Click event for theSearch button control
    SearchButton.PerformClick()
End Sub

Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
    Dim MessageString As String = "Version 1 John Burtons Building Cost Estimater" & ControlChars.NewLine & "Today's date/time: " & Date.Now
    Dim TitleString As String = "About Version 1"

    MessageBox.Show(MessageString, TitleString, MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

    'Close the form
    Me.Close()
End Sub

Private Sub ProductIDComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProductIDComboBox.SelectedIndexChanged

    'Test to determine if a product has been selected
    If ProductIDComboBox.SelectedIndex <> -1 Then

        'Store the selectedIndex to variable
        Dim RowInteger As Integer = ProductIDComboBox.SelectedIndex

        'Based on RowInteger, display values to TextBox controls
        'from the array named inventoryProduct
        ProductIDTextBox.Text = InventoryProduct(RowInteger).ProductIDString
        DescriptionTextBox.Text = InventoryProduct(RowInteger).DescriptionString
        QuantityTextBox.Text = InventoryProduct(RowInteger).QuantityInteger.ToString("N0")
        PriceTextBox.Text = InventoryProduct(RowInteger).PriceDecimal.ToString("C2")
    End If
End Sub

Private Sub TotalSalesByProductToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TotalSalesByProductToolStripMenuItem.Click
    'Display output to immediate window
    Dim RowInteger As Integer
    Dim SalesString As String = "ProductID" & vbTab & "Dollar Sales" & vbCrLf
    For Each ProductItem As Product In InventoryProduct
        'Build string to display
        SalesString &= ProductItem.ProductIDString & vbTab & vbTab & ProductSalesTotalDecimal(RowInteger).ToString("C2") & vbCrLf

        'Increment RowInteger
        RowInteger += 1
    Next
    'Display string to a MessageBox
    'Debug.WriteLine(SalesString)
    MessageBox.Show(SalesString, "Sales for all Products", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub



Private Sub Button3_Click(sender As Object, e As EventArgs) Handles PurchaseBtn.Click
    'Test to determine if a product was found.
    If DescriptionTextBox.Text = String.Empty Then

        'Cannot purchase, product was not found
        MessageBox.Show("You must select a valid product before purchasing.", "Cannot Purchase", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        ProductIDTextBox.Focus()
        ProductIDTextBox.SelectAll()
    Else
        'Can purchase the product
        'Build a string to display in the listbox control
        Dim ProductString As String = ProductIDTextBox.Text & "  " & DescriptionTextBox.Text & "    " & QuantityTextBox.Text & "     " & PriceTextBox.Text
        PurchaseListBox.Items.Add(ProductString)


        ' Dim Total As Double
        '  For i = 0 To PurchaseListBox.Items.Count
        'Total += Convert.ToDouble(PriceTextBox.Items.Item(i)) * Convert.ToDouble(QuantityTextBox.Items.Item(i))
        ' Next i
        ' Debug.WriteLine(Total)


        'Accumulate the total value of this customer order
        'and display it to the output textbox

        TotalDueDecimal += Decimal.Parse(PriceTextBox.Text, Globalization.NumberStyles.Currency)
        TotalDueTextBox.Text = Products.Sum(Function(x) x.Cost).ToString("C2")
        ' TotalDueTextBox.Text = TotalDueDecimal.ToString("C2")

        'Accumulate total sales by product to an array
        Dim IndexInteger As Integer = ProductIDComboBox.SelectedIndex
        ProductSalesTotalDecimal(IndexInteger) += Decimal.Parse(PriceTextBox.Text, Globalization.NumberStyles.Currency)

        'Here you can clear the form of product info if you think
        'that is a good way to do the processing
        ProductIDComboBox.SelectedIndex = -1
        ProductIDTextBox.Clear()
        DescriptionTextBox.Clear()
        PriceTextBox.Clear()
        QuantityTextBox.Clear()
        ProductIDTextBox.Focus()
    End If
End Sub
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles SaveAsToolStripMenuItem.Click

    sFile.InitialDirectory = ("C:\")
    sFile.FileName = ("Save As...")
    sFile.Filter = ("Only Text Files (*.txt)|*.txt")
    sFile.ShowDialog()

    Dim W As New IO.StreamWriter(sFile.FileName, True)  ' notes from class need messgbox
    Dim i As Integer
    For i = 0 To PurchaseListBox.Items.Count - 1
        W.WriteLine(PurchaseListBox.Items.Item(i))

        Dim L As New IO.StreamWriter(sFile.FileName, True)
        L.WriteLine(TotalDueTextBox)


        ' Next
        ' Dim L As New IO.StreamWriter(sFile.FileName)
        ' For i = 0 To TotalDueTextBox.Items.Count - 1
        'W.WriteLine(TotalDueTextBox.Items.Item(i))

    Next
    W.Close()
End Sub

Private Sub PriceTextBox_TextChanged(sender As Object, e As EventArgs) Handles PriceTextBox.TextChanged



End Sub

' Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Dim num1, num2, product As Decimal

'  num1 = PriceTextBox.Text
'  num2 = QuantityTextBox.Text
' product = num1 * num2
' TextBox3.Text = product
' PriceTextBox.Text = TextBox3.Text


'  End Sub

结束班

3 个答案:

答案 0 :(得分:0)

您需要使用.Value属性,而不是控件的.Text属性。

答案 1 :(得分:0)

If Integer.TryParse(txtQuan.Text,ItemQuan) = False then
       ' fail msg
End If

PriceDec = ' parse like above...current code parses Price twice and
    ' ADDS to sales total the second time

Dim TotalDue as Decimal = ItemQuan * PriceDec 
TotalDueTextBox.Text = TotalDue.ToString("C2")

答案 2 :(得分:0)

  

我必须允许不同价格和数量的用户输入

有时候,一点点重新设计可以为您提供答案,同时让您更轻松。

首先,列表更适合您想要的内容。

新的构造函数和tostring覆盖将有助于此:

Structure Product
    Dim ProductIDString As String
    Dim DescriptionString As String
    Dim QuantityInteger As Integer
    Dim PriceDecimal As Double
    Dim Cost As Double
    Dim W As IO.StreamWriter
    Public Sub New(PID As String, Desc As String, Qty As Integer, Price As Double)
        ProductIDString = PID
        DescriptionString = Desc
        QuantityInteger = Qty
        PriceDecimal = Price
        Cost = Qty * Price
    End Sub
    Public Overrides Function ToString() As String
        Return ProductIDString & " " & DescriptionString & " " & QuantityInteger.ToString & " " & PriceDecimal.ToString("C2") & " " & Cost.ToString("C2")
    End Function
End Structure

Dim Products As New List(Of Product)

使用新的构造函数现在可以非常简单地将项添加到列表中:

    Products.AddRange(
        {
            New Product("test1", "Test1a", 10, 1.5),
            New Product("test2", "test2a", 20, 2.25)
        })

ToString()方法可用于使用DataSource属性填充列表框:

    ListBox1.DataSource = Products

现在,只要您想要更新总数,您就可以访问Sum方法,该方法可用于累计列表中项目的Cost属性:

    Label1.Text = Products.Sum(Function(x) x.Cost).ToString("C2")

每当您想要将新产品添加到列表中时,一个简单的子例程将用于更新列表框和总数:

Public Sub UpdateLB()
    ListBox1.DataSource = Nothing
    ListBox1.DataSource = Products
    Label1.Text = Products.Sum(Function(x) x.Cost).ToString("C2")
End Sub

添加新产品时检查空字符串,验证并解析需要它的产品,然后将新产品添加到列表中并调用UpdatLB。

一方不是,您可能要考虑使用Double而不是Decimal,因为Decimal有时会出现舍入错误。

不确定为什么你在结构中有了streamwriter,但如果你需要将产品写入文件,只需遍历列表并调用每个项目的ToString方法,就可以将该字符串写入文件。 / p>