Hi all,
I'm struggling with a simple little program which I would use for a test to (for example) sell footbal tickets.

So I have a for on which user selects a game from combobox (combobox populates from text file) and number of tickets in a numericupdown field. On "Save" button this data is saved to a txt file in this form:

Milan - Arsenal 1
Real - Barcelona 2
Milan - Arsenal 7
Celtic - Milan 4
Real - Barcelona 2
Celtic - Arsenal 2

so 1st the game selected from combobox, vbtab and then nr. of tickets from numericupdown.

Now I have this data in text file. Now I want to show when form is opened (in Listbox), how many tickets were sold for each game and how many all together for all games.

this is my code so far:

Imports System.IO
Imports System.Data.SqlServerCe
Imports System.Data.SqlClient

Structure Struktura
    Dim game As String
    Dim nr_tickets As Integer

End Structure

Public Class Form1

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

        'games available are loaded from this file to combobox1
        Dim path As String = "path_to_file\games_to_choose.txt"
            Using sr As New IO.StreamReader(path)
                While Not sr.EndOfStream
                End While
            End Using
        Catch ex As Exception
        End Try

        'data is read from file where sold game tickets are saved
            Dim reader As New Microsoft.VisualBasic.FileIO.TextFieldParser("path_to_file\file_to_save_games.txt")
            reader.TextFieldType = FileIO.FieldType.Delimited

            'data is populated to an array
            While Not reader.EndOfData
                Dim Fields() As String = reader.ReadFields
                ListBox1.Items.Add(Fields(0) & vbTab & Fields(1))

            End While

        Catch ex As Exception
        End Try

    End Sub

    Private Sub Button1_save(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_save.Click

        Dim Datoteka As String = "path_to_file\file_to_save_games.txt"

        If ComboBox1.SelectedItem = Nothing Then
            MsgBox("Please choose a game!")
            Exit Sub
        End If

        If NumericUpDown1.Value = "0" Then
            MsgBox("Please choose number of tickets!")
            Exit Sub
        End If

        Dim zapis As Struktura
        Dim sw As StreamWriter

        With zapis
            .game = ComboBox1.Text
            .nr_tickets = NumericUpDown1.Value

            sw = File.AppendText(Datoteka)
            sw.WriteLine(.game & vbTab & .nr_tickets)
            MsgBox(.game & vbTab & .nr_tickets)
        End With

        MsgBox("Data is saved")

        'form is reset
        TextBox1.Text = ""
        ComboBox1.Text = ""
        RadioButton1.Checked = False
        RadioButton2.Checked = False
        RadioButton3.Checked = False
        DateTimePicker1.Value = DateTime.Now
        NumericUpDown1.Value = "0"

    End Sub
End Class

What I did is to import datafrom "file_to_save_games.txt" to an array, but I can't read unique values (games) from it and matching numbers.

Any help would be great! tnx

3 Years
Discussion Span
Last Post by razree

You should save the data in a way that makes it easy to parse when you read it back in. For example, if you save it as

Milan - Arsenal;1
Real - Barcelona;2
Milan - Arsenal;7
Celtic - Milan;4
Real - Barcelona;2
Celtic - Arsenal;2

then you can read the file back into a dictionary and display the totals as

Dim dic As New Dictionary(Of String, Integer)

'read the file and calculate totals

For Each line As String In System.IO.File.ReadAllLines("d:\temp\games.txt")
    Dim fields() As String = line.Split(";")
    If dic.ContainsKey(fields(0)) Then
        dic(fields(0)) += CInt(fields(1))
        dic(fields(0)) = CInt(fields(1))
    End If

'display number of tickets for each game plus grand total

Dim numTickets As Integer = 0

For Each key As String In dic.Keys
    ListBox1.Items.Add(key & " = " & dic(key))
    numTickets += dic(key)

ListBox1.Items.Add("Total tickets = " & numTickets)

Edited by Reverend Jim


Thats exactly what I needed! :) Works perfectly, now I just need to add some structure to listbox1 so it looks neat...tnx a lot

Edited by razree

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.