在Visual Basic中使用模块

时间:2014-03-06 05:52:28

标签: vb.net module

我正在编写一个包含三个表单和一个模块的程序。一个表单有两个列表框,一个用于销售商品,一个用于购买的商品,还有一个文本框供用户输入他们想要的每个商品的数量,还有一个按钮来计算总价格。待售物品及其价格如下:

小麦面包 - 1.15美元 白面包 - $ 1.05 甜甜圈 - 0.50美元和8.3%的销售税 葡萄干 - 0.25美元和2.0%的销售税

就像我说的那样,除了这个之外还有其他2种形式,但是一旦我理解了如何使用这种形式,其他形式就不会成为问题。

该模块应该用于记录所有税收,并用于执行所有与税收相关的功能。

问题是,我以前从未使用过模块,也不确定如何设置它然后从其他表单中调用它。这是我到目前为止为第一种形式编写的代码,

Option Strict On
Public Class Form1

Dim total As Double = 0
Dim Quantity As Integer

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    If (txtQuantity.Text = "") Then
        MessageBox.Show("Please enter a quantity for the item you selected")
    ElseIf Not Integer.TryParse(txtQuantity.Text, Quantity) Then
        MessageBox.Show("The quantity entered is not numeric. Please add a numeric quantity.")
        Exit Sub
    Else
        lstPurchased.Items.Add(txtQuantity.Text & " " & lstSale.Text)
    End If
End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    lstPurchased.Items.Clear()
    txtQuantity.Clear()
End Sub

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    For Each item In lstSale.SelectedItems
        Select Case CStr(item)
            Case ("Wheat Bread")
                total += (CDbl(FormatCurrency(txtQuantity.Text)) * 1.15)
            Case ("White Bread")
                total += (CDbl(FormatCurrency(txtQuantity.Text)) * 1.05)
            Case ("Donuts")
                total += (CDbl(FormatCurrency(CDbl(txtQuantity.Text) * 0.5 * DONUT_TAX)))
            Case ("Raisins")
                total += (CDbl(FormatCurrency(CDbl(txtQuantity.Text) * (0.25 * RAISIN_TAX) + 0.25)))
        End Select
    Next
    MessageBox.Show(CStr(total))
 End Sub
End Class

这就是我对模块的一点点,就像我说的那样,我以前从未使用过模块,因此我不确定如何设置它。

Module TaxesModule
Public Const DONUT_TAX As Decimal = 0.083
Public Const RAISIN_TAX As Decimal = 0.02
Public Const SS_TAX As Decimal = 0.062

Public Function SalesTax(ByVal 

End Module

非常感谢任何帮助或建议。

2 个答案:

答案 0 :(得分:1)

对于您的计算按钮单击操作,您必须转到每个选定的项目。像这样:

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Dim total As Double = 0

        'summarize all selected items
        For Each item In lstSale.SelectedItems
            Select Case CStr(item)
                Case ("Wheat Bread")
                    total += (CDbl(FormatCurrency(txtQuantity.Text)) * 1.15)
                Case ("White Bread")
                    total += (CDbl(FormatCurrency(txtQuantity.Text)) * 1.05)
                Case ("Donuts")
                    total += (CDbl(FormatCurrency(CDbl(txtQuantity.Text) * 0.5 * DONUT_TAX)))
                Case ("Raisins")
                    total += (CDbl(FormatCurrency(CDbl(txtQuantity.Text) * (0.25 * RAISIN_TAX) + 0.25)))
            End Select
        Next
        MessageBox.Show(CStr(total))
    End Sub

所有变量,函数等。可以从代码中的每个位置访问,而无需创建它的实例。所以,你可以在模块中保留税款。

访问模块:

Public Class MyClass1
    Private Sub DoSomething()
        TOTAL += Add(40, 30)
    End Sub
End Class

Public Class MyClass2
    Private Sub DoSomething()
        TOTAL += Add(10, 20)
    End Sub
End Class

Public Module MyModule1
    Public TOTAL As Integer = 0
    Public Function Add(ByVal firstValue As Integer, ByVal secondValue As Integer) As Integer
        Return firstValue + secondValue
    End Function
End Module

答案 1 :(得分:1)

你应该在课程级别声明总数,而不是在eventhandler中声明...

Option Strict On
Public Class Form1
Dim total As Double = 0 '<---- put it here

....
....

End Class