我试图在visual basic.NET中创建一个简单的应用程序,

时间:2018-03-11 11:46:15

标签: vb.net

我是Visual Basic.NET的新手,我一直在努力创建这个简单的应用程序。这是一个税收计算器。

二维数组有点......我还不容易掌握。所以我的问题是:你能看看我的代码并给我提示如何解决这个问题吗?

当我在Labelbox中运行代码时,我得到0.00

的结果
Public Class PerrytownForm
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Friend WithEvents IdFwtLabel As System.Windows.Forms.Label
    Friend WithEvents FwtLabel As System.Windows.Forms.Label
    Friend WithEvents CalculateButton As System.Windows.Forms.Button
    Friend WithEvents ExitButton As System.Windows.Forms.Button
    Friend WithEvents StatusGroupBox As System.Windows.Forms.GroupBox
    Friend WithEvents MarriedRadioButton As System.Windows.Forms.RadioButton
    Friend WithEvents SingleRadioButton As System.Windows.Forms.RadioButton
    Friend WithEvents IdTaxableLabel As System.Windows.Forms.Label
    Friend WithEvents TaxableTextBox As System.Windows.Forms.TextBox

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.IdTaxableLabel = New System.Windows.Forms.Label()
        Me.StatusGroupBox = New System.Windows.Forms.GroupBox()
        Me.MarriedRadioButton = New System.Windows.Forms.RadioButton()
        Me.SingleRadioButton = New System.Windows.Forms.RadioButton()
        Me.TaxableTextBox = New System.Windows.Forms.TextBox()
        Me.IdFwtLabel = New System.Windows.Forms.Label()
        Me.FwtLabel = New System.Windows.Forms.Label()
        Me.CalculateButton = New System.Windows.Forms.Button()
        Me.ExitButton = New System.Windows.Forms.Button()
        Me.StatusGroupBox.SuspendLayout()
        Me.SuspendLayout()
        '
        'IdTaxableLabel
        '
        Me.IdTaxableLabel.AutoSize = True
        Me.IdTaxableLabel.Location = New System.Drawing.Point(24, 24)
        Me.IdTaxableLabel.Name = "IdTaxableLabel"
        Me.IdTaxableLabel.Size = New System.Drawing.Size(99, 16)
        Me.IdTaxableLabel.TabIndex = 0
        Me.IdTaxableLabel.Text = "&Taxable wages:"
        '
        'StatusGroupBox
        '
        Me.StatusGroupBox.Controls.Add(Me.MarriedRadioButton)
        Me.StatusGroupBox.Controls.Add(Me.SingleRadioButton)
        Me.StatusGroupBox.Location = New System.Drawing.Point(184, 24)
        Me.StatusGroupBox.Name = "StatusGroupBox"
        Me.StatusGroupBox.Size = New System.Drawing.Size(95, 88)
        Me.StatusGroupBox.TabIndex = 2
        Me.StatusGroupBox.TabStop = False
        Me.StatusGroupBox.Text = "Status"
        '
        'MarriedRadioButton
        '
        Me.MarriedRadioButton.Checked = True
        Me.MarriedRadioButton.Location = New System.Drawing.Point(8, 21)
        Me.MarriedRadioButton.Name = "MarriedRadioButton"
        Me.MarriedRadioButton.Size = New System.Drawing.Size(80, 24)
        Me.MarriedRadioButton.TabIndex = 0
        Me.MarriedRadioButton.TabStop = True
        Me.MarriedRadioButton.Text = "&Married"
        '
        'SingleRadioButton
        '
        Me.SingleRadioButton.Location = New System.Drawing.Point(8, 50)
        Me.SingleRadioButton.Name = "SingleRadioButton"
        Me.SingleRadioButton.Size = New System.Drawing.Size(80, 24)
        Me.SingleRadioButton.TabIndex = 1
        Me.SingleRadioButton.Text = "&Single"
        '
        'TaxableTextBox
        '
        Me.TaxableTextBox.Location = New System.Drawing.Point(24, 40)
        Me.TaxableTextBox.Name = "TaxableTextBox"
        Me.TaxableTextBox.Size = New System.Drawing.Size(136, 23)
        Me.TaxableTextBox.TabIndex = 1
        '
        'IdFwtLabel
        '
        Me.IdFwtLabel.AutoSize = True
        Me.IdFwtLabel.Location = New System.Drawing.Point(24, 80)
        Me.IdFwtLabel.Name = "IdFwtLabel"
        Me.IdFwtLabel.Size = New System.Drawing.Size(146, 16)
        Me.IdFwtLabel.TabIndex = 5
        Me.IdFwtLabel.Text = "Federal withholding tax:"
        '
        'FwtLabel
        '
        Me.FwtLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.FwtLabel.Location = New System.Drawing.Point(24, 96)
        Me.FwtLabel.Name = "FwtLabel"
        Me.FwtLabel.Size = New System.Drawing.Size(136, 23)
        Me.FwtLabel.TabIndex = 6
        Me.FwtLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'CalculateButton
        '
        Me.CalculateButton.Location = New System.Drawing.Point(184, 120)
        Me.CalculateButton.Name = "CalculateButton"
        Me.CalculateButton.Size = New System.Drawing.Size(96, 25)
        Me.CalculateButton.TabIndex = 3
        Me.CalculateButton.Text = "&Calculate Tax"
        '
        'ExitButton
        '
        Me.ExitButton.Location = New System.Drawing.Point(184, 152)
        Me.ExitButton.Name = "ExitButton"
        Me.ExitButton.Size = New System.Drawing.Size(96, 25)
        Me.ExitButton.TabIndex = 4
        Me.ExitButton.Text = "E&xit"
        '
        'PerrytownForm
        '
        Me.AcceptButton = Me.CalculateButton
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 16)
        Me.ClientSize = New System.Drawing.Size(312, 205)
        Me.Controls.Add(Me.StatusGroupBox)
        Me.Controls.Add(Me.ExitButton)
        Me.Controls.Add(Me.CalculateButton)
        Me.Controls.Add(Me.FwtLabel)
        Me.Controls.Add(Me.IdFwtLabel)
        Me.Controls.Add(Me.TaxableTextBox)
        Me.Controls.Add(Me.IdTaxableLabel)
        Me.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Name = "PerrytownForm"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Perrytown Gift Shop"
        Me.StatusGroupBox.ResumeLayout(False)
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region

    'declare form-level arrays
    Private msngSingle(,) As Single = {{51, 0, 0, 0},
                                       {552, 0, 0.15, 51},
                                       {1196, 75.15, 0.28, 552},
                                       {2662, 255.47, 0.31, 1196},
                                       {5750, 709.93, 0.36, 2662},
                                       {99999, 1821.61, 0.396, 5750}}

    Private msngMarried(,) As Single = {{124, 0, 0, 0},
                                        {960, 0, 0.15, 124},
                                        {2023, 124.4, 0.28, 960},
                                        {3292, 423.04, 0.31, 2023},
                                        {5809, 816.43, 0.36, 3292},
                                        {99999, 1722.55, 0.396, 5809}}

    Private Sub ExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        Me.Close()
    End Sub

    Private Sub TaxableTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TaxableTextBox.Enter
        Me.TaxableTextBox.SelectAll()
    End Sub

    Private Sub ClearLabels(ByVal sender As Object, ByVal e As System.EventArgs) Handles MarriedRadioButton.Click, SingleRadioButton.Click, TaxableTextBox.TextChanged
        Me.FwtLabel.Text = ""
    End Sub

    Private Sub CalculateButton_Click(sender As Object, e As EventArgs) Handles CalculateButton.Click
        Dim sngTaxTable(5, 3) As Single
        Dim sngTaxable, sngFwt As Single, intRow As Integer
        Dim blnFound As Boolean

        'assign taxable wages to a variable
        sngTaxable = Val(Me.TaxableTextBox.Text)

        'determine appropriate array
        If Me.SingleRadioButton.Checked = True Then
            sngTaxable = msngSingle
        Else
            sngTaxable = msngMarried
        End If

        'search for taxable wages in the first column in the array
        Do While intRow < 6 AndAlso blnFound = False
            If sngTaxable <= sngTaxTable(intRow, 0) Then
                'calculate the fwt
                sngFwt = sngTaxTable(intRow, 1) _
                    + sngTaxTable(intRow, 2) _
                    * (sngTaxable - sngTaxTable(intRow, 3))
                blnFound = True
            Else
                intRow = intRow + 1

            End If
        Loop
        'display the fwt
        Me.FwtLabel.Text = Format(sngFwt, "currency")
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

我为自己尝试了代码并发现了一些错误:你忘记了字母“T”

If Me.SingleRadioButton.Checked = True Then
    sngTaxable = msngSingle
Else
    sngTaxable = msngMarried
End If

需要

If Me.SingleRadioButton.Checked = True Then
    sngTaxTable = msngSingle
Else
    sngTaxTable = msngMarried
End If

如果我改变这个,如果我尝试输入一些数字,代码将起作用并给出一些结果。 如果它们太小或太大,程序就不会做任何事情。我不知道这是否是故意的,但也许有一点错误输出(MsgBox等)可能很好。