Option Explicit On
Option Strict On
Public Class MainForm
Structure strLotteryDraws
Public lotterydate As Date
Public num1 As String
Public num2 As String
Public num3 As String
Public num4 As String
Public num5 As String
Public LotteryDraws As ArrayList
End Structure
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
Me.Close()
End Sub
Private Sub xDisplayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xDisplayButton.Click
' displays the dates stored in a sequential access file
Dim tmpDraws As strLotteryDraws
Dim lottery As String
If My.Computer.FileSystem.FileExists("fantasyfive.csv") Then
lottery = _
My.Computer.FileSystem.ReadAllText("fantasyfive.csv")
Else
MessageBox.Show("File does not exist", "Dates", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Dim i As Integer
Dim j As Integer = 1
Dim slotNum As String
For i = 0 To lottery.Length - 1<---says 'lottery' not declared(green line)
slotNum = lottery.Substring(i, 1)
If lottery.Substring(i, 1) < "0" Or lottery.Substring(i) > "9" Then
i = i + 1
Else
Select Case j
Case 1
tmpDraws.lotterydate = CDate(lottery.Substring(i - 1, 9))
j = j + 1
i = i + 9
Case 2
If lottery.Substring(i + 1) = "," Then
tmpDraws.num1 = lottery.Substring(i)
i = i + 1
Else
tmpDraws.num1 = lottery.Substring(i, 2)
i = i + 2
End If
j = j + 1
Case 3
If lottery.Substring(i + 1) = "," Then
tmpDraws.num2 = lottery.Substring(i)
i = i + 1
Else
tmpDraws.num2 = lottery.Substring(i, 2)
End If
j = j + 1
Case 4
If lottery.Substring(i + 1) = "," Then
tmpDraws.num3 = lottery.Substring(i)
i = i + 1
Else
tmpDraws.num3 = lottery.Substring(i, 2)
i = i + 2
End If
j = j + 1
Case 5
If lottery.Substring(i + 1) = "," Then
tmpDraws.num4 = lottery.Substring(i)
i = i + 1
Else
tmpDraws.num4 = lottery.Substring(i, 2)
i = i + 2
End If
j = j + 1
Case 6
If lottery.Substring(i + 1) = "," Then
tmpDraws.num5 = lottery.Substring(i)
i = i + 1
Else
tmpDraws.num5 = lottery.Substring(i, 2)
i = i + 2
End If
j = 1
End Select
End If
xDatesLabel.Text = tmpDraws.lotterydate.ToString
Next i
End Sub
End Class
Formating DatesFormating dates from a file...
43 Replies - 2323 Views - Last Post: 30 July 2008 - 12:30 AM
#1
Formating Dates
Posted 29 July 2008 - 02:05 AM
Replies To: Formating Dates
#2
Re: Formating Dates
Posted 29 July 2008 - 02:13 AM
eg
Dim MyString as String=""
In your code
Option Explicit On
Option Strict On
Public Class MainForm
Structure strLotteryDraws
Public lotterydate As Date
Public num1 As String=""
Public num2 As String=""
Public num3 As String=""
Public num4 As String=""
Public num5 As String=""
Public LotteryDraws As ArrayList
End Structure
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
Me.Close()
End Sub
Private Sub xDisplayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xDisplayButton.Click
' displays the dates stored in a sequential access file
Dim tmpDraws As strLotteryDraws
Dim lottery As String="" 'Try adding this.
If My.Computer.FileSystem.FileExists("fantasyfive.csv") Then
lottery = _
My.Computer.FileSystem.ReadAllText("fantasyfive.csv")
Else
MessageBox.Show("File does not exist", "Dates", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
' Added this as I think you don't want the next section of code to run if the file isn't found
Exit Sub
End If
Dim i As Integer
Dim j As Integer = 1
Dim slotNum As String
For i = 0 To lottery.Length - 1'<---says 'lottery' not declared(green line)
slotNum = lottery.Substring(i, 1)
If lottery.Substring(i, 1) < "0" Or lottery.Substring(i) > "9" Then
i = i + 1
Else
Select Case j
Case 1
tmpDraws.lotterydate = CDate(lottery.Substring(i - 1, 9))
j = j + 1
i = i + 9
Case 2
If lottery.Substring(i + 1) = "," Then
tmpDraws.num1 = lottery.Substring(i)
i = i + 1
Else
tmpDraws.num1 = lottery.Substring(i, 2)
i = i + 2
End If
j = j + 1
Case 3
If lottery.Substring(i + 1) = "," Then
tmpDraws.num2 = lottery.Substring(i)
i = i + 1
Else
tmpDraws.num2 = lottery.Substring(i, 2)
End If
j = j + 1
Case 4
If lottery.Substring(i + 1) = "," Then
tmpDraws.num3 = lottery.Substring(i)
i = i + 1
Else
tmpDraws.num3 = lottery.Substring(i, 2)
i = i + 2
End If
j = j + 1
Case 5
If lottery.Substring(i + 1) = "," Then
tmpDraws.num4 = lottery.Substring(i)
i = i + 1
Else
tmpDraws.num4 = lottery.Substring(i, 2)
i = i + 2
End If
j = j + 1
Case 6
If lottery.Substring(i + 1) = "," Then
tmpDraws.num5 = lottery.Substring(i)
i = i + 1
Else
tmpDraws.num5 = lottery.Substring(i, 2)
i = i + 2
End If
j = 1
End Select
End If
xDatesLabel.Text = tmpDraws.lotterydate.ToString
Next i
End Sub
End Class
This post has been edited by AdamSpeight2008: 29 July 2008 - 02:16 AM
#3
Re: Formating Dates
Posted 29 July 2008 - 02:32 AM
Public Class Form1
Dim Lottos As New List(Of Lotto)
Private Structure Lotto
Public Datum As String
Public Number() As Integer
End Structure
' csv file Format
' Date , Number 1, Number 2,Number 3, Number 4, Number 5
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Prepare file to be parsed
Dim Delimiters() As Char = {","}
Dim tfp As Microsoft.VisualBasic.FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser("fantasyfive.csv", Delimiters)
' List to contain read in lottos
Dim NewLotto As Lotto
Dim Fields() As String
While tfp.EndOfData = False
NewLotto = New Lotto
Fields = tfp.ReadFields
' The Fields array now contains the following data
' Fields(0) => The Date
' Fields(1) => Number 1
' Fields(2) => Number 2
' Fields(3) => Number 3
' Fields(4) => Number 4
' Fields(5) => Number 5
NewLotto.Datum = CDate(Fields(0)).ToString("MM dd yyyy")' Converts Date to correct Format as well
ReDim NewLotto.Number(4)
For i As Integer = 1 To 5
NewLotto.Number(i - 1) = CInt(Fields(i))
Next
' Sort the number
Array.Sort(NewLotto.Number)
' Add it to the collection of other lottos
Lottos.Add(NewLotto)
End While
tfp.Close()
End Sub
End Class
This post has been edited by AdamSpeight2008: 29 July 2008 - 02:58 AM
#4
Re: Formating Dates
Posted 29 July 2008 - 03:59 AM
Option Explicit On
Option Strict On
Public Class MainForm
Structure strLotteryDraws
Public lotterydate As Date
Public num1 As String = ""
Public num2 As String = ""
Public num3 As String = ""<---errors here with all Num1-5 says 'Initializers on structure members are valid only for 'Shared' members and constants.
Public num4 As String = ""
Public num5 As String = ""
Public LotteryDraws As ArrayList
End Structure
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
Me.Close()
End Sub
Private Sub xDisplayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xDisplayButton.Click
' displays the dates stored in a sequential access file
Dim tmpDraws As strLotteryDraws
Dim lottery As String = ""
If My.Computer.FileSystem.FileExists("fantasyfive.csv") Then
lottery = _
My.Computer.FileSystem.ReadAllText("fantasyfive.csv")
Else
MessageBox.Show("File does not exist", "Dates", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
' Added this as I think you don't want the next section of code to run if the file isn't found
Exit Sub
End If
Dim i As Integer
Dim j As Integer = 1
Dim slotNum As String
For i = 0 To lottery.Length - 1
slotNum = lottery.Substring(i, 1)
If lottery.Substring(i, 1) < "0" Or lottery.Substring(i) > "9" Then
i = i + 1
Else
Select Case j
Case 1
tmpDraws.lotterydate = CDate(lottery.Substring(i - 1, 9))
j = j + 1
i = i + 9
Case 2
If lottery.Substring(i + 1) = "," Then
tmpDraws.num1 = lottery.Substring(i)
i = i + 1
Else
tmpDraws.num1 = lottery.Substring(i, 2)
i = i + 2
End If
j = j + 1
Case 3
If lottery.Substring(i + 1) = "," Then
tmpDraws.num2 = lottery.Substring(i)
i = i + 1
Else
tmpDraws.num2 = lottery.Substring(i, 2)
End If
j = j + 1
Case 4
If lottery.Substring(i + 1) = "," Then
tmpDraws.num3 = lottery.Substring(i)
i = i + 1
Else
tmpDraws.num3 = lottery.Substring(i, 2)
i = i + 2
End If
j = j + 1
Case 5
If lottery.Substring(i + 1) = "," Then
tmpDraws.num4 = lottery.Substring(i)
i = i + 1
Else
tmpDraws.num4 = lottery.Substring(i, 2)
i = i + 2
End If
j = j + 1
Case 6
If lottery.Substring(i + 1) = "," Then
tmpDraws.num5 = lottery.Substring(i)
i = i + 1
Else
tmpDraws.num5 = lottery.Substring(i, 2)
i = i + 2
End If
j = 1
End Select
End If
xDatesLabel.Text = tmpDraws.lotterydate.ToString
Next i
End Sub
End Class
Also another error: Event 'Load' cannot be found, in fantasyfive.vb?
Public Class fantasyfive
Dim Lottos As New List(Of Lotto)
Private Structure Lotto
Public Datum As String
Public Number() As Integer
End Structure
' csv file Format
' Date , Number 1, Number 2,Number 3, Number 4, Number 5
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Prepare file to be parsed
Dim Delimiters() As Char = CType((","), Char())<--had to tweak this
Dim tfp As Microsoft.VisualBasic.FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser("fantasyfive.csv", Delimiters)
' List to contain read in lottos
Dim NewLotto As Lotto
Dim Fields() As String
While tfp.EndOfData = False
NewLotto = New Lotto
Fields = tfp.ReadFields
' The Fields array now contains the following data
' Fields(0) => The Date
' Fields(1) => Number 1
' Fields(2) => Number 2
' Fields(3) => Number 3
' Fields(4) => Number 4
' Fields(5) => Number 5
NewLotto.Datum = CDate(Fields(0)).ToString("MM dd yyyy") ' Converts Date to correct Format as well
ReDim NewLotto.Number(4)
For i As Integer = 1 To 5
NewLotto.Number(i - 1) = CInt(Fields(i))
Next
' Sort the number
Array.Sort(NewLotto.Number)
' Add it to the collection of other lottos
Lottos.Add(NewLotto)
End While
tfp.Close()
End Sub
End Class
#5
Re: Formating Dates
Posted 29 July 2008 - 04:01 AM
Option Explicit On Option Strict On Public Class MainForm Structure strLotteryDraws Public lotterydate As Date Public num1 As String Public num2 As String Public num3 As String Public num4 As String Public num5 As String Public LotteryDraws As ArrayList End Structure
#6
Re: Formating Dates
Posted 29 July 2008 - 04:11 AM
#7
Re: Formating Dates
Posted 29 July 2008 - 04:14 AM
AdamSpeight2008, on 29 Jul, 2008 - 07:01 AM, said:
Option Explicit On Option Strict On Public Class MainForm Structure strLotteryDraws Public lotterydate As Date Public num1 As String Public num2 As String Public num3 As String Public num4 As String Public num5 As String Public LotteryDraws As ArrayList End Structure
#8
Re: Formating Dates
Posted 29 July 2008 - 04:15 AM
Option Explicit On
Option Strict On
Public Class MainForm
' Structure strLotteryDraws
' Public lotterydate As Date
' Public num1 As String
' Public num2 As String
' Public num3 As String
'Public num4 As String
' Public num5 As String
' Public LotteryDraws As ArrayList
' End Structure
Dim Lottos As New List(Of Lotto)
Private Structure Lotto
Public Datum As String
Public Number() As Integer
End Structure
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
Me.Close()
End Sub
Private Sub xDisplayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xDisplayButton.Click
' displays the dates stored in a sequential access file
Dim tmpDraws As strLotteryDraws
Dim lottery As String = ""
If My.Computer.FileSystem.FileExists("fantasyfive.csv") Then
lottery = _
My.Computer.FileSystem.ReadAllText("fantasyfive.csv")
Else
MessageBox.Show("File does not exist", "Dates", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
' Added this as I think you don't want the next section of code to run if the file isn't found
Exit Sub
End If
' csv file Format
' Date , Number 1, Number 2,Number 3, Number 4, Number 5
' Prepare file to be parsed
Dim Delimiters() As Char = Dim Delimiters() As Char = {CChar(",")} 'Should be this
Dim tfp As Microsoft.VisualBasic.FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser("fantasyfive.csv", Delimiters)
' List to contain read in lottos
Dim NewLotto As Lotto
Dim Fields() As String
While tfp.EndOfData = False
NewLotto = New Lotto
Fields = tfp.ReadFields
' The Fields array now contains the following data
' Fields(0) => The Date
' Fields(1) => Number 1
' Fields(2) => Number 2
' Fields(3) => Number 3
' Fields(4) => Number 4
' Fields(5) => Number 5
NewLotto.Datum = CDate(Fields(0)).ToString("MM dd yyyy") ' Converts Date to correct Format as well
ReDim NewLotto.Number(4)
For i As Integer = 1 To 5
NewLotto.Number(i - 1) = CInt(Fields(i))
Next
' Sort the number
Array.Sort(NewLotto.Number)
' Add it to the collection of other lottos
Lottos.Add(NewLotto)
End While
tfp.Close()
' show date
xDatesLabel.Text = Lottos(0).datum
End Sub
End Class
This post has been edited by AdamSpeight2008: 29 July 2008 - 04:21 AM
#9
Re: Formating Dates
Posted 29 July 2008 - 04:19 AM
This post has been edited by LadyWolf: 29 July 2008 - 04:23 AM
#10
Re: Formating Dates
Posted 29 July 2008 - 04:23 AM
#11
Re: Formating Dates
Posted 29 July 2008 - 04:49 AM
AdamSpeight2008, on 29 Jul, 2008 - 07:23 AM, said:
fantasyfive.txt (39.99K)
Number of downloads: 53
LadyWolf, on 29 Jul, 2008 - 07:47 AM, said:
AdamSpeight2008, on 29 Jul, 2008 - 07:23 AM, said:
I sent as a txt file though when coding it's an csv file.
#12
Re: Formating Dates
Posted 29 July 2008 - 05:48 AM
LadyWolf, on 29 Jul, 2008 - 07:49 AM, said:
AdamSpeight2008, on 29 Jul, 2008 - 07:23 AM, said:
LadyWolf, on 29 Jul, 2008 - 07:47 AM, said:
AdamSpeight2008, on 29 Jul, 2008 - 07:23 AM, said:
I sent as a txt file though when coding it's an csv file, even though it looks like a txt file when I 'Open file'. Not sure if that is normal.
Don't need to add lottery numbers, only read what's in the file..and display the formatted dates in the xDatesLabel, and the lottery numbers from file to the xLotteryLabel.
#13
Re: Formating Dates
Posted 29 July 2008 - 06:16 AM
Public Class MainForm
Dim Lottos As New List(Of Lotto)
Private Structure Lotto
Public Datum As String
Public Number() As Integer
End Structure
' csv file Format
' Date , Number 1, Number 2,Number 3, Number 4, Number 5
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Prepare file to be parsed
Dim Delimiters() As Char = CType((","), Char())
Dim tfp As Microsoft.VisualBasic.FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser("fantasyfive.csv", Delimiters)
' List to contain read in lottos
Dim NewLotto As Lotto
Dim Fields() As String
While tfp.EndOfData = False
NewLotto = New Lotto
Fields = tfp.ReadFields
' The Fields array now contains the following data
' Fields(0) => The Date
' Fields(1) => Number 1
' Fields(2) => Number 2
' Fields(3) => Number 3
' Fields(4) => Number 4
' Fields(5) => Number 5
NewLotto.Datum = CDate(Fields(0)).ToString("mm dd yyyy")
'Converts Date to correct Format as well <--error here says 'Conversion from string "Sun." to type 'Date' is not valid.'
ReDim NewLotto.Number(4)
For i As Integer = 1 To 5
NewLotto.Number(i - 1) = CInt(Fields(i))
Next
' Sort the number
Array.Sort(NewLotto.Number)
' Add it to the collection of other lottos
Lottos.Add(NewLotto)
End While
tfp.Close()
End Sub
End Class
There's days of the week in the file also.
#14
Re: Formating Dates
Posted 29 July 2008 - 11:32 AM
#15
Re: Formating Dates
Posted 29 July 2008 - 05:05 PM
Selecting a date displays the lotto numbers for that date,
Private Sub xDisplayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xDisplayButton.Click
' Prepare file to be parsed
Dim Delimiters() As Char = {CChar(",")}
Dim tfp As Microsoft.VisualBasic.FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser("fantasyfive.txt", Delimiters)
' List to contain read in lottos
Dim NewLotto As Lotto
' Define stringBuild to speed up the building of the string.
Dim listText As New System.Text.StringBuilder
Dim Fields() As String
While tfp.EndOfData = False
NewLotto = New Lotto
Fields = tfp.ReadFields
' The Fields array now contains the following data
' Fields(0) => The Day
' Fields(1) => The Date
' Fields(2) => Number 1
' Fields(3) => Number 2
' Fields(4) => Number 3
' Fields(5) => Number 4
' Fields(6) => Number 5
NewLotto.Datum = CDate(Fields(1)).ToString("MM dd yyyy") ' The format extension is Case-sensitive.
ReDim NewLotto.Number(4)
For i As Integer = 1 To 5
NewLotto.Number(i - 1) = CInt(Fields(i + 1))
Next
' Sort the number
Array.Sort(NewLotto.Number)
' Add it to the collection of other lottos
Lottos.Add(NewLotto)
listText.AppendLine(Lottos(Lottos.Count - 1).Datum)
' add listbox
Me.ListBox1.Items.Add(Lottos(Lottos.Count - 1).Datum)
End While
Me.xDatesLabel.Text = listText.ToString
tfp.Close()
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If Me.ListBox1.SelectedIndex <> -1 Then
Me.xLbl_Lotto.Text = _
Lottos(Me.ListBox1.SelectedIndex).Number(0) & " " & _
Lottos(Me.ListBox1.SelectedIndex).Number(1) & " " & _
Lottos(Me.ListBox1.SelectedIndex).Number(2) & " " & _
Lottos(Me.ListBox1.SelectedIndex).Number(3) & " " & _
Lottos(Me.ListBox1.SelectedIndex).Number(4)
End If
End Sub
|
|

New Topic/Question
Reply




MultiQuote




|