2016-02-22 52 views
1

我想制作一个程序,检查所有产品的库存文件,并返回来自输入的GTIN代码的正确信息(您将(希望))从看着我的代码)努力从文本文件中获取字符串(Visual Basic)

Public Class Form1 
    Dim FILE_NAME As String = "H:\Visual Studio 2013\Projects\Control Task 2 Barcode Orders\Control Task 2 Barcode Orders\bin\Debug\receipt_orders.txt" 
    Dim GTIN As String 
    Dim LineContaining As String 
    Dim quantity As Integer 
    Dim description As String 
    Dim singleamount As String 
    Dim singleprice As Double 
    Dim totalprices As Double 
    Private Sub btnEnterGTIN_Click(sender As Object, e As EventArgs) Handles btnEnterGTIN.Click 
     GTIN = getValidGTIN() 
     Call itemfinder(GTIN) 
    End Sub 
    Function getValidGTIN() 
     Dim GTIN, ValidGTINAmountCharacters As String 
     Dim GTINOK As Boolean 
     'Declaring variables. 
     GTINOK = False 
     Do 
      GTIN = InputBox("Enter the full GTIN Number (it should be 8 digits long)") 
      'Prompts the user to enter the GTIN. 
      ValidGTINAmountCharacters = Len(GTIN) 
      'Makes it so that the amount of characters of the GTIN is stored in the variable ValidGTINAmountCharacters. 
      If ValidGTINAmountCharacters = 8 Then GTINOK = True 
      'Makes it so the program will only accept an input if it was 8 characters long. 
      If Not IsNumeric(GTIN) Then GTINOK = False 
      'Makes it so that if any other character typed in apart from a number is not valid. 
      If Not GTINOK Then MsgBox("The GTIN Number isn't valid. It should be a 8 digit number. (Should not contain letters or symbols).") 
      'Makes it so that if the GTIN is not valid according to the above, a message appears saying it is invalid. 
     Loop Until GTINOK 
     Return GTIN 
    End Function 
    Private Sub itemfinder(ByVal GTIN As String) 
     Using reader As New IO.StreamReader("receipt_orders.txt") 
      While Not reader.EndOfStream 
       Dim line As String = reader.ReadLine() 
       If line.Contains(GTIN) Then 
        line = LineContaining 
        Exit While 
       End If 
      End While 
     End Using 
     description = Mid$(LineContaining, 10, 17) 
     singleamount = Mid$(LineContaining, 38, 4) 
     quantity = InputBox("Enter the amount required") 
     totalprices = quantity * singleamount 
     lstGTIN.Items.Add(GTIN) 
     lstName.Items.Add(description) 
     lstQuantity.Items.Add(quantity) 
     lstSinglePrice.Items.Add(singleamount) 
     lstTotal.Items.Add(totalprices) 

     Dim sum As Double 
     For x As Integer = 0 To lstTotal.Items.Count - 1 

      sum += Val(lstTotal.Items.Item(x).ToString) 
     Next 
     txtTotalPrice.Text = sum.ToString 
    End Sub 
End Class 

当我在数量和品种代码输入,我得到有关的总价格的计算错误代码 - 我不知道如何解决这个问题! 此外,我使用这样的

12345670 L-Shaped Brackets   7.20 
10101010 Television     1.80 
69696969 Screws      0.20 

容貌请尽量解释如何解决这个问题尽可能的简单文本文件!我不是很擅长Visual Basic!

+0

这不是vbscript。任何你没有指定错误信息的原因? – shawnt00

+0

也许你需要'LineContaining = line'而不是你得到的是相反的分配。 – shawnt00

+1

工作!我觉得很蠢,错误是因为这个,非常感谢你! – Senyd

回答

0

那里的一些代码,例如使用关键字Call,表示vb6/vbscript时代的心态。下面是使用现代和更清洁的VB.Net编码风格的相同功能的实现:

Public Class ProductItem 
    Public Property GTIN As Integer 
    Public Property Description As String 
    Public Property ItemPrice As Decimal 

    Protected Property SourceData As String 

    Public Shared Function FromLineString(ByVal lineString As String) As OrderLine 
     Return New ProductItem() With 
     { 
      .Description = lineString.SubString(9,17), 
      .ItemPrice = Decimal.Parse(lineString.SubString(37,4)), 
      .GTIN = Int32.Parse(lineString.SubString(0,8)), 'Guessed at this field 
      .SourceData = lineString  
     } 
    End Function 
End Class 

Public Class Form1 
    Const FILE_NAME As String = "H:\Visual Studio 2013\Projects\Control Task 2 Barcode Orders\Control Task 2 Barcode Orders\bin\Debug\receipt_orders.txt" 

    Private Sub btnEnterGTIN_Click(sender As Object, e As EventArgs) Handles btnEnterGTIN.Click 
     Dim item As ProductItem = itemfinder(Form1.InputValidGTIN()) 
     If item Is Nothing Then Exit Sub ' May want to show error here 

     Dim QTY As Integer = Form1.InputInteger("Enter the amount required", "Input was not a valid Integer. Please try again.") 

     Me.SuspendLayout() 
     lstGTIN.Items.Add(item.GTIN) 
     lstName.Items.Add(item.Description) 
     lstQuantity.Items.Add(QTY) 
     lstSinglePrice.Items.Add(item.ItemPrice) 
     lstTotal.Items.Add(QTY * item.ItemPrice) 

     txtTotalPrice.Text = lstTotal.Items.Cast(Of Decimal).Sum().ToString() 
     Me.ResumeLayout() 
    End Sub 

    Public Shared Function InputValidGTIN() As String 
     Dim GTIN As String = InputBox("Enter the full GTIN Number (it should be 8 digits long)").Trim() 

     While Not IsValidGTIN(GTIN) 
      MsgBox("The GTIN Number isn't valid. It should be a 8 digit number. (Should not contain letters or symbols).") 
      GTIN = InputBox("Enter the full GTIN Number (it should be 8 digits long)").Trim() 
     End While 

     Return GTIN 
    End Function 

    Public Shared Function IsValidGTIN(ByVal GTIN As String) As Boolean 
     Static regex As New Regex("^\d{8}$") 
     Return regex.IsMatch(GTIN) 
    End Function 

    Public Shared Function InputInteger(ByVal PromptText As String, ByVal RePromptText As String) As Integer 
     Dim result As Integer 
     Dim input As String = InputBox(PromptText) 

     While Not Int32.TryParse(input, result) 
      input = InputBox(RePromptText) 
     End While 

     Return result 
    End Function 

    Private Function itemfinder(ByVal GTIN As String) As ProductItem 
     Using reader As New IO.StreamReader(FILE_NAME) 
      Dim line As String = reader.ReadLine() 
      While line IsNot Nothing 

       If line.Contains(GTIN) Then 
        Return ProductItem.FromLineString(line) 
       End If 
       line = reader.ReadLine() 
      End While 
     End Using 
     Return Nothing 
    End Function 
End Class