关于vb.net:我需要将textbox.text更改为十进制值以汇总总数并显示在消息框中。使用vb 2010

I need to change textbox.text into decimal value to sum up totals and display in message box. Using vb 2010

我试图弄清楚如何在文本框(价格)中获取一个值,该文本框带有一个临时变量和一个数组,然后将其更改为十进制值,这样我就可以得出总销售额和然后使用相同的数组对另一个文本框的总权重求和。我已经用书和搜索在线站点进行了大约5个小时的工作,但没有任何效果。我陷入了私人次级购买按钮的过程(从底部开始第二个)。我的total items计数器正在工作,并且我也创建了subTotal Decimal和totalWeightDecimal计数器,以确保消息框正在工作,但是我想将= 1更改为正确的代码,但我找不到它。我尝试了解析,但这是不行的。更改textbox.text我包含了我的整个代码供某人查看。我知道我有太多私有变量,只是等我弄清楚所有东西之后再将它们清除掉。任何帮助将不胜感激。总而言之,我在整个程序上花了至少15个小时。感谢您的时间和精力。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'Declare Item Structure.
Structure Item
    Dim upcString As String
    Dim nameString As String
    Dim descriptionString As String
    Dim weightString As String
    Dim priceDecimal As Decimal
End Structure

'Declare the ItemPrices Structure
Public Structure ItemPrices
    Dim itemdescriptionString As String
    Dim itempriceDecimal As Decimal
    Dim itemweightDecimal As Decimal
    Dim itemupcInteger As Integer
End Structure

Public Class lab7Form
    Private price1Decimal As Decimal
    Private subTotalDecimal As Decimal
    Private grandTotalDecimal As Decimal
    Private totalDecimal As Decimal
    Private totalItemsInteger As Integer
    Private MessageString As String
    Private totalWeightDecimal As Decimal

    Dim myItem As Item
    Private ItemPricesArray(3) As ItemPrices

    Private Sub exitButton_Click(sender As System.Object, e As System.EventArgs) Handles exitButton.Click
        If MsgBox("Click Yes to Exit, Click No to Cancel", MsgBoxStyle.YesNo,"You Decide") = MsgBoxResult.Yes Then
            Me.Close()
        End If
    End Sub

    Private Sub clearButton_Click(sender As System.Object, e As System.EventArgs) Handles clearButton.Click
        'Clear the textbox values.
        upcTextBox.Text =""
        descriptionTextBox.Text =""
        weightTextBox.Text =""
        priceTextBox.Text =""
    End Sub

    Private Sub lab7Form_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'Load my itemsListbox with the items for the user.
        itemsListBox.Items.Add("Notebook PC")
        itemsListBox.Items.Add("Deskjet Printer")
        itemsListBox.Items.Add("Color Ink Cartridge")
        itemsListBox.Items.Add("Black Ink Cartridge")

        'Load the ItemPricesArray values.
        ItemPricesArray(0).itemdescriptionString ="Notebook PC"
        ItemPricesArray(1).itemdescriptionString ="Deskjet Printer"
        ItemPricesArray(2).itemdescriptionString ="Color Ink Cartridge"
        ItemPricesArray(3).itemdescriptionString ="Black Ink Cartridge"

        ItemPricesArray(0).itempriceDecimal = 1500D
        ItemPricesArray(1).itempriceDecimal = 430D
        ItemPricesArray(2).itempriceDecimal = 11D
        ItemPricesArray(3).itempriceDecimal = 10D

        ItemPricesArray(0).itemweightDecimal = 7D
        ItemPricesArray(1).itemweightDecimal = 16D
        ItemPricesArray(2).itemweightDecimal = 0.5D
        ItemPricesArray(3).itemweightDecimal = 0.5D

        ItemPricesArray(0).itemupcInteger = 111111111
        ItemPricesArray(1).itemupcInteger = 222222222
        ItemPricesArray(2).itemupcInteger = 333333333
        ItemPricesArray(3).itemupcInteger = 444444444
    End Sub

    Private Sub itemsListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles itemsListBox.SelectedIndexChanged
        descriptionTextBox.Text = itemsListBox.Text
        'I am using a table lookup   Do/Loop here.
        Dim myBoolean As Boolean        'True = I found an items price.
        Dim indexInteger As Integer = 0
        Do Until myBoolean Or indexInteger > 3
            With Me
                If .descriptionTextBox.Text = ItemPricesArray(indexInteger).itemdescriptionString Then
                    .priceTextBox.Text = ItemPricesArray(indexInteger).itempriceDecimal.ToString("C")
                    .weightTextBox.Text = ItemPricesArray(indexInteger).itemweightDecimal.ToString()
                    .upcTextBox.Text = ItemPricesArray(indexInteger).itemupcInteger.ToString()
                    myBoolean = True
                Else
                    indexInteger += 1
                End If
            End With
        Loop
    End Sub

    Private Sub purchaseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles purchaseButton.Click
         Dim DecNumber1 As Decimal
        'Parse Numbers
        DecNumber1 = Decimal.Parse(priceTextBox.Text)

        If purchaseButton.Enabled And priceTextBox.Text <>"" Then
            'Calculate the quantities
            totalItemsInteger += 1  'I can't figure out how to total up the weights and prices when they are both local variables.
            subTotalDecimal += DecNumber1
            totalWeightDecimal += 1

            'From Page 172 Put together what you want to appear in message boxes
            Dim MessageString As String
            Dim totalString As String
            Dim numberOfItemsString As String
            Dim weightOfItemsString As String

            numberOfItemsString = totalItemsInteger.ToString()
            totalString = subTotalDecimal.ToString("C")
            weightOfItemsString = totalWeightDecimal.ToString("N")

            MessageString ="Total Items :" & numberOfItemsString &
                Environment.NewLine &"Total Weight:" & weightOfItemsString &
                Environment.NewLine &"Subtotal:" & totalString

            'Display the message box
            MessageBox.Show(MessageString,"Items Purchased", MessageBoxButtons.OKCancel)
        End If
    End Sub

    Private Sub totalButton_Click(sender As System.Object, e As System.EventArgs) Handles totalButton.Click
        Dim numberofOrdersString As String
        If totalButton.Enabled Then
            numberofOrdersString ="Number of Orders: " & totalItemsInteger.ToString()
            MessageString ="Total Sales: " & grandTotalDecimal.ToString("C")

            'Display the message box
            MessageBox.Show(MessageString,"Items Purchased", MessageBoxButtons.OKCancel)
        End If
    End Sub
End Class


您需要遍历ItemPrices对象的数组,如下所示:

1
2
3
4
5
6
7
Dim itempriceDecimalTotal As Decimal
Dim itemweightDecimalTotal As Decimal

For Each theItemPrices As ItemPrices In ItemPricesArray
    itempriceDecimalTotal += theItemPrices.itempriceDecimal
    itemweightDecimalTotal += theItemPrices.itemweightDecimal
Next

现在您可以将值转换为字符串,如下所示:

1
2
3
4
5
6
7
8
Dim MessageString As String
Dim totalString As String
Dim numberOfItemsString As String
Dim weightOfItemsString As String

numberOfItemsString = totalItemsInteger.ToString()
totalString = itempriceDecimalTotal.ToString("C")
weightOfItemsString = itemweightDecimalTotal.ToString("N")

最后,您可以在消息框中显示总计值,如下所示:

1
2
3
4
5
6
MessageString ="Total Items :" & numberOfItemsString &
        Environment.NewLine &"Total Weight:" & weightOfItemsString &
        Environment.NewLine &"Subtotal:" & totalString

'Display the message box
MessageBox.Show(MessageString,"Items Purchased", MessageBoxButtons.OKCancel)

1
2
3
4
5
Dim priceTotal As Double = (From itemPrice in ItemPricesArray
                            Select itemPrice.itempriceDecimal).Sum

Dim weightTotal As Double = (From itemPrice in ItemPricesArray
                             Select itemPrice.itemweightDecimal).Sum

使用LINQ扩展方法Sum:

即可轻松获得数组中各项的所有属性的总和。

1
Dim grandtotal As Double = ItemPricesArray.Sum(Function(x) x.itempriceDecimal)

然后以ToString()方法很好地显示为字符串:

1
Textbox1.Text = grandtotal.ToString("C2")'display as currency with 2 decimal places