首页 文章

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

提问于
浏览
-1

我正在制作一个程序,允许用户查看歌曲信息,播放歌曲摘录,并购买选定的歌曲 .

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

退房时:

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

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

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

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

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

当我单击所选歌曲的 "Purchase" button 并单击 "Check Out" button 时,出现错误信息: "You have not ordered any items". 请参阅下面代码中的cmdCheckOut_Click子例程 . 我认为's where I'我得到了我的错误 .

这是代码:

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 回答

  • 5

    Not 没有按照你的想法去做 . 在VB.Net中,Not operator对非布尔值执行按位反转 . 所以如果 purchaseCount = 1 那么 Not purchaseCount = 0xFFFFFFFE = -2 ,其中转换为 True . 只有整数值0,才能转换为false .

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

相关问题