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"
        Try
            Using sr As New IO.StreamReader(path)
                While Not sr.EndOfStream
                    ComboBox1.Items.Add(sr.ReadLine)
                End While
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

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

            '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

            reader.Close()
        Catch ex As Exception
            MessageBox.Show(Err.Description)
        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)
            sw.Flush()
            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"
        ListBox1.Items.Clear()

    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

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))
    Else
        dic(fields(0)) = CInt(fields(1))
    End If
Next

'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)
Next

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

Edited 3 Years Ago 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 3 Years Ago by razree

This question has already been answered. Start a new discussion instead.