因此,我从文本文件导入数据,并根据特定信息将它们排序到不同的列表框中。数据的一个例子如下;但是我需要找到条目的经过时间。所以我需要从第一个条目时间戳中减去最后的条目时间戳。我不确定如何从列表框或文本文件的第一行和最后一行中提取特定数据,我还需要对两个单独的单元格执行此操作。
例如: 这是第一个生产单元:
Production Cell 1,4 / 20/201 2:09:18 PM,340
Production Cell 1,4 / 20/201 3:13:48 PM,211
第二小组:
Production Cell 2,4 / 20/18 2:09:24 PM,531
Production Cell 2,4 / 20/201 3:13:45 PM,720
请注意两个单元格在同一文本文件中混乱,不按顺序
基于颜色的当前排序: 仍需要:经过时间,重量和Go / NoGo是基于最后的值,我还没有尝试过。
Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
OpenFileDialog1.InitialDirectory = "c:\"
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "txt files (*.txt)|*.txt"
OpenFileDialog1.FilterIndex = 1
OpenFileDialog1.RestoreDirectory = True ' Returns to original start directory
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
m_FileName = OpenFileDialog1.FileName
srFile = New StreamReader(m_FileName) ' Need new instance of the object, this is necessary
While Not srFile.EndOfStream
strLine = srFile.ReadLine
If strLine.Contains("Production Cell 1") Then
lstProductionCell1.Items.Add(strLine)
If strLine.Contains("PM, 1") Or strLine.Contains("AM, 1") Then
white1 += 1
lblWhiteCell1.Text = CStr(white1)
ElseIf strLine.Contains("PM, 2") Or strLine.Contains("AM, 2") Then
black1 += 1
lblBlackCell1.Text = CStr(black1)
ElseIf strLine.Contains("PM, 3") Or strLine.Contains("AM, 3") Then
red1 += 1
lblRedCell1.Text = CStr(red1)
ElseIf strLine.Contains(" PM, 4") Or strLine.Contains("AM, 4") Then
yellow1 += 1
lblYellowCell1.Text = CStr(yellow1)
ElseIf strLine.Contains("PM, 5") Or strLine.Contains("AM, 5") Then
green1 += 1
lblGreenCell1.Text = CStr(green1)
ElseIf strLine.Contains("PM, 6") Or strLine.Contains("AM, 6") Then
blue1 += 1
lblBlueCell1.Text = CStr(blue1)
ElseIf strLine.Contains("PM, 7") Or strLine.Contains("AM, 7") Then
brown1 += 1
lblBrownCell1.Text = CStr(brown1)
ElseIf strLine.Contains("PM, 8") Or strLine.Contains("AM, 8") Then
grey1 += 1
lblGreyCell1.Text = CStr(grey1)
End If
ElseIf strLine.Contains("Production Cell 2") Then
lstProductionCell2.Items.Add(strLine)
If strLine.Contains("PM, 1") Or strLine.Contains("AM, 1") Then
white2 += 1
lblWhiteCell2.Text = CStr(white2)
ElseIf strLine.Contains("PM, 2") Or strLine.Contains("AM, 2") Then
black2 += 1
lblBlackCell2.Text = CStr(black2)
ElseIf strLine.Contains("PM, 3") Or strLine.Contains("AM, 3") Then
red2 += 1
lblRedCell2.Text = CStr(red2)
ElseIf strLine.Contains(" PM, 4") Or strLine.Contains("AM, 4") Then
yellow2 += 1
lblYellowCell2.Text = CStr(yellow2)
ElseIf strLine.Contains("PM, 5") Or strLine.Contains("AM, 5") Then
green2 += 1
lblGreenCell2.Text = CStr(green2)
ElseIf strLine.Contains("PM, 6") Or strLine.Contains("AM, 6") Then
blue2 += 1
lblBlueCell2.Text = CStr(blue2)
ElseIf strLine.Contains("PM, 7") Or strLine.Contains("AM, 7") Then
brown2 += 1
lblBrownCell2.Text = CStr(brown2)
ElseIf strLine.Contains("PM, 8") Or strLine.Contains("AM, 8") Then
grey2 += 1
lblGreyCell2.Text = CStr(grey2)
End If
End If
End While
End If
srFile.Close() ' Be sure to close the file
End Sub
答案 0 :(得分:1)
代码的逻辑对我来说是陌生的。我不明白你试图确定白色,黑色,灰色等颜色值的基础。所以我并没有试图纠正它。
无论如何,根据您描述的有关识别文本文件中单元格日期之间差异的问题,我尝试提出以下解决方案。
我编写了一个示例控制台应用程序来复制用例。
Imports System
Imports System.Globalization
Imports System.Collections.Generic
Imports System.Linq
Public Class Program
Public Shared Sub Main()
Dim inputData = New List(Of String)()
'I am using generic collection here for the input data instead of file.
'You can use File.ReadAllLines method to read all the lines from file and store them in an array.
' Dim inputData = File.ReadAllLines(m_FileName)
inputData.Add("Production Cell 1, 4/20/2018 2:09:18 PM, 340")
inputData.Add("Production Cell 1, 4/20/2018 3:13:48 PM, 211")
inputData.Add("Production Cell 2, 4/20/2018 2:09:24 PM, 531")
inputData.Add("Production Cell 2, 4/20/2018 3:13:45 PM, 720")
Dim cellDates = New Dictionary(Of String, List(Of DateTime))()
Dim prodCell1 As String = "Production Cell 1"
Dim prodCell2 As String = "Production Cell 2"
For Each item In inputData
Dim lineItems = item.Split({","c})
Dim datePart = lineItems(1).Trim() & lineItems(2)
Dim dateValue = DateTime.ParseExact(datePart, "M/dd/yyyy h:mm:ss tt fff", CultureInfo.InvariantCulture)
If Not cellDates.ContainsKey(lineItems(0)) Then
cellDates.Add(lineItems(0), New List(Of DateTime)())
End If
cellDates(lineItems(0)).Add(dateValue)
Next
'Sorting dates of both the Cells.
Dim prodCell1Dates = cellDates(prodCell1).OrderBy(Function(dt) dt)
Dim prodCell2Dates = cellDates(prodCell2).OrderBy(Function(dt) dt)
'You can add the dates values in the listboxes here as following.
'lstProductCell1.DataSource = cellDates(prodCell1)
'lstProductCell2.DataSource = cellDates(prodCell2)
'Getting difference between the last and the first date.
'This returns an instance of TimeSpan
Dim prodCell1Duration = prodCell1Dates.Last() - prodCell1Dates.First()
Dim prodCell2Duration = prodCell2Dates.Last() - prodCell2Dates.First()
'Display difference in the form of total minutes in the Console.
'You need to write code here to display values in the proper labels.
Console.WriteLine(prodCell1Duration.TotalMinutes)
Console.WriteLine(prodCell2Duration.TotalMinutes)
End Sub
End Class
有用的链接 https://msdn.microsoft.com/en-us/library/system.timespan(v=vs.110).aspx
这可以帮助您解决问题。
答案 1 :(得分:0)
我建议将数据导入为可用格式,然后您可以更轻松地引用所有内容,例如< CellData(2).Elapsed'
'Put Structure and 'global' arrays at module level
Structure EachCell
Dim StartTime As Date
Dim FinishTime As Date
Dim CodeNumber As Integer
Dim Elapsed As TimeSpan
End Structure
'
Private DataInFile() As String
Dim CellData(999) As EachCell
'leaving (0) element unused
'
Private Sub MyRoutine()
Dim threeparts As String(), CellRefNo As Integer, Temp As Date
If IO.File.Exists("MyFilePath") Then
DataInFile = IO.File.ReadAllLines("MyFilePath")
For Each dataline As String In DataInFile
If dataline.StartsWith("Production") Then
'split dataline on comma
threeparts = dataline.Split(","c)
'refno/array index is 3rd element of first part
CellRefNo = CInt(threeparts(0).Split()(2))
If IsNothing(CellData(CellRefNo).StartTime) OrElse CellData(CellRefNo).StartTime = DateTime.MinValue Then
'if .starttime field previously unused
CellData(CellRefNo).StartTime = CDate(threeparts(1))
Else
CellData(CellRefNo).FinishTime = CDate(threeparts(1))
End If
If CellData(CellRefNo).StartTime > CellData(CellRefNo).FinishTime Then
'if start/finish in wrong order swap
Temp = CellData(CellRefNo).StartTime
CellData(CellRefNo).StartTime = CellData(CellRefNo).FinishTime
CellData(CellRefNo).FinishTime = Temp
End If
CellData(CellRefNo).Elapsed = CellData(CellRefNo).FinishTime - CellData(CellRefNo).StartTime
CellData(CellRefNo).CodeNumber = CInt(threeparts(2))
End If
Next
End If
End Sub
答案 2 :(得分:0)
我使用了类和通用列表来跟踪数据。
Class Form1
Private Sub FillData()
Dim inputData = New List(Of String)()
inputData.Add("Production Cell 1, 4/20/2018 2:09:18 PM, 340")
inputData.Add("Production Cell 1, 4/20/2018 3:13:48 PM, 211")
inputData.Add("Production Cell 2, 4/20/2018 2:09:24 PM, 531")
inputData.Add("Production Cell 2, 4/20/2018 3:13:45 PM, 720")
Dim itgWhite1, itgBlack1, itgRed1, itgYellow1, itgGreen1, itgBlue1, itgBrown1, itgGrey1 As Integer
Dim itgWhite2, itgBlack2, itgRed2, itgYellow2, itgGreen2, itgBlue2, itgBrown2, itgGrey2 As Integer
Dim DataCell1 As New List(Of CellData)
Dim DataCell2 As New List(Of CellData)
For Each s As String In inputData
Dim sArray() As String = s.Split(","c) 'the c provides the Char that .Split requires
Dim cd As New CellData()
cd.CellNumber = CInt(sArray(0).Remove(0, 16)) 'Remove the Production Cell part
cd.CellDate = CDate(sArray(1).Trim)
cd.CellDetail = CInt(sArray(2).Trim)
If cd.CellNumber = 1 Then
Select Case cd.CellDetail
Case > 799
itgGrey1 += 1
Case > 699
itgBrown1 += 1
Case > 599
itgBlue1 += 1
Case > 499
itgGreen1 += 1
Case > 399
itgYellow1 += 1
Case > 299
itgRed1 += 1
Case > 199
itgBlack1 += 1
Case Else
itgWhite1 += 1
End Select
DataCell1.Add(cd)
Else
Select Case cd.CellDetail
Case > 799
itgGrey2 += 1
Case > 699
itgBrown2 += 1
Case > 599
itgBlue2 += 1
Case > 499
itgGreen2 += 1
Case > 399
itgYellow2 += 1
Case > 299
itgRed2 += 1
Case > 199
itgBlack2 += 1
Case Else
itgWhite2 += 1
End Select
DataCell2.Add(cd)
End If
Next
Debug.Print($"
White1 = {itgWhite1},
Grey1 = {itgBlack1},
Red1 = {itgRed1},
Yellow1 = {itgYellow1},
Green1 = {itgGreen1},
Blue1 = {itgBlue1},
Brown1 = {itgBrown1},
Grey1 = {itgGrey1},
White2 = {itgWhite2},
Grey2 = {itgBlack2},
Red2 = {itgRed2},
Yellow2 = {itgYellow2},
Green2 = {itgGreen2},
Blue2 = {itgBlue2},
Brown2 = {itgBrown2},
Grey2 = {itgGrey2}")
Dim Cell1Sorted As List(Of CellData) = DataCell1.OrderBy(Function(d) d.CellDate).ToList
Dim Cell2Sorted As List(Of CellData) = DataCell2.OrderBy(Function(d) d.CellDate).ToList
ListBox1.Items.AddRange(Cell1Sorted.ToArray)
ListBox2.Items.AddRange(Cell2Sorted.ToArray)
lblElapsed1.Text = (Cell1Sorted.Item(Cell1Sorted.Count - 1).CellDate - Cell1Sorted.Item(0).CellDate).TotalMinutes.ToString
lblElapsed2.Text = (Cell1Sorted.Item(Cell2Sorted.Count - 1).CellDate - Cell2Sorted.Item(0).CellDate).TotalMinutes.ToString
End Sub
End Class
Class CellData
Property CellDate As DateTime
Property CellNumber As Integer
Property CellDetail As Integer
Public Overrides Function ToString() As String
Return $"Procuction Cell {CellNumber}, {CellDate}, {CellDetail}"
End Function
End Class