如何使输入框和文本文件出现在Visual Basic中?

时间:2013-11-06 19:51:29

标签: vb.net visual-studio-2010

我正在制作一个程序,允许用户查看有关歌曲的信息,播放歌曲的摘录,以及购买所选歌曲。

并允许用户点击“购买”按钮购买指定的曲调。

退房时:

如果他们没有购买任何曲调,用户无法结帐,但他们可以退出该计划。

使用InputBox,以便用户可以输入其销售税率。由于用户输入的值,您必须对其输入执行数据验证。

单击“输入框取消”按钮,允许用户取消签出过程。

当显示输入框时,文本框应该具有焦点,并且当添加不正确的税值时,应该清除不正确的值并且文本框应该再次具有焦点。

使用Write / Writeline创建名为PurchaseOrder.txt的采购订单文本文件,其中包括创建文件的日期以及购买的逐项清单,小计,税金和总计。

当我点击所选歌曲的“购买”按钮并点击“结帐”按钮时,我收到一条错误消息: “您尚未订购任何项目。”请参阅下面代码中的cmdCheckOut_Click子程序。我想这就是我收到错误的地方。

以下是代码:

Public Structure musicInfo
    <VBFixedString(30)> Public title As String
    <VBFixedString(20)> Public artist As String
    <VBFixedString(20)> Public genre As String
    <VBFixedString(10)> Public duration As String
    Public year As Integer
    Public price As Double
    <VBFixedString(15)> Public songFileName As String
End Structure

Public Const NoOfTunes = 5
Public songs(NoOfTunes - 1) As musicInfo

Option Explicit On
Imports System.IO
Public Class frmTunes
Public index As Integer
Public purchaseCount As Integer
Public purchasePrice(10) As Decimal
Public purchaseTitle(10) As String


Private Sub frmTunes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim i As Integer

    FileOpen(1, "music.dat", OpenMode.Random, , , Len(songs(0)))
    For i = 0 To NoOfTunes - 1
        FileGet(1, songs(i))
    Next
    FileClose(1)

    cmdPrevious.Visible = False

    DisplaySong(0)
End Sub

Sub DisplaySong(ByVal i As Int32)
    lblTitle.Text = songs(i).title
    lblArtist.Text = songs(i).artist
    lblGenre.Text = songs(i).genre
    lblDuration.Text = songs(i).duration
    lblYear.Text = Convert.ToString(songs(i).year)
    lblPrice.Text = Convert.ToString(songs(i).price)
End Sub

Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
    My.Computer.Audio.Stop()
End Sub

Private Sub cmdPurchase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPurchase.Click
    purchaseTitle(purchaseCount) = lblTitle.Text
    purchasePrice(purchaseCount) = Convert.ToDecimal(lblPrice.Text)
    purchaseCount = (purchaseCount + 1)
End Sub

Private Sub cmdPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrevious.Click
    index = (index - 1)
    If (index < 4) Then
        cmdNext.Visible = True
    End If
    If (index = 0) Then
        cmdPrevious.Visible = False
    End If
    DisplaySong(index)
End Sub

Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
    index = (index + 1)
    If (index = NoOfTunes - 1) Then
        cmdNext.Visible = False
    End If
    If (index > 0) Then
        cmdPrevious.Visible = True
    End If
    DisplaySong(index)
End Sub

Private Sub cmdPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPlay.Click
    My.Computer.Audio.Play(songs(index).songFileName)
End Sub

Private Sub cmdCheckOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCheckOut.Click
    Dim decimal1 As Decimal
    Dim decimal3 As Decimal
    Dim decimal4 As Decimal
    Dim str1 As String = ""
    If (Not purchaseCount) Then
        MsgBox("You have not ordered any items!", MsgBoxStyle.Exclamation, "Order Error")
    Else
        Do While ((IsNumeric(str1) Or (Decimal.Compare(decimal3, Decimal.Zero) < 0)) Or (Decimal.Compare(decimal3, (10D)) > 0))
            str1 = InputBox("Enter your tax rate as a % between and including 0 - 10:", "Tax Rate", "", -1, -1)
            If (str1 <> "") Then
                If (Not IsNumeric(str1)) Then
                    MsgBox("You must enter a numeric tax rate", MsgBoxStyle.Exclamation, "Tax Rate Error")
                Else
                    Dim dec3 As Decimal = Convert.ToDecimal(str1)
                    If ((Decimal.Compare(decimal3, Decimal.Zero) < 0) Or (Decimal.Compare(decimal3, (10D)) > 0)) Then
                        MsgBox("You must enter a tax rate between and including 0% - 10%", MsgBoxStyle.Exclamation, "Tax Rate Error")
                    End If
                End If
            End If
        Loop
        Dim StreamWriter As StreamWriter = File.CreateText("PurchaseOrder.txt")
        StreamWriter.WriteLine("For Purchases dated: " & DateTime.Now.ToLongDateString())
        StreamWriter.WriteLine()
        Dim num2 As Integer = (purchaseCount - 1)
        Dim num1 As Integer = 0
        Do While (num1 <= num2)
            StreamWriter.Write(Strings.FormatCurrency(CType(Me.purchasePrice(num1), Decimal) & "     "))
            StreamWriter.WriteLine(Me.purchaseTitle(num1))
            Dim dec1 As Decimal = Decimal.Add(Nothing, Me.purchasePrice(num1))
            num1 = (num1 + 1)

        Loop
        StreamWriter.WriteLine("------")
        StreamWriter.WriteLine(Strings.FormatCurrency(CType(decimal1, Decimal) & "  Subtotal"))
        Dim decimal2 As Decimal = New Decimal(((Convert.ToDouble(decimal3) * 0.01) * Convert.ToDouble(decimal1)))
        StreamWriter.WriteLine(Strings.FormatCurrency(CType(decimal2, Decimal) & "  Tax"))
        StreamWriter.WriteLine("------")
        Dim dec4 As Decimal = Decimal.Add(decimal1, decimal2)
        StreamWriter.WriteLine(Strings.FormatCurrency(CType(decimal4, Decimal) & "  Total"))
        MsgBox("Purchase Order has been created", MsgBoxStyle.OkOnly)
        StreamWriter.Close()
        Me.Close()
    End If
End Sub
 End Class

1 个答案:

答案 0 :(得分:5)

Not没有按照您的想法行事。在VB.Net中,Not operator对非布尔值执行按位反转。因此,如果purchaseCount = 1然后Not purchaseCount = 0xFFFFFFFE = -2,则转换为True。只有整数值0,才能转换为false。

将测试If (Not purchaseCount)更改为If (purchaseCount = 0)