reading txt file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • federicocefalo
    New Member
    • Nov 2018
    • 1

    reading txt file

    I'm working on a program for reading a txt file. the file is read and its data are sent to the viewers with an indeterminate cycle every minute.
    I wanted to replace the sleep with a timer. I tried the same procedure under timers instead of under button1 but it does not work. how can I proceed with the change.

    Public Class Form1
    Dim disco1 As System.IO.File
    Dim leggi1 As System.IO.Strea mReader
    Dim ms As Integer = 60000

    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
    Me.Size = Screen.PrimaryS creen.WorkingAr ea.Size
    Me.Location = Screen.PrimaryS creen.WorkingAr ea.Location
    End Sub

    Private Sub Button1_Click(B yVal sender As Object, ByVal e As System.EventArg s) Handles Button1.Click

    Button1.BackCol or = Color.Red
    Dim riga1 As String
    Dim i As Integer
    Do
    leggi1 = disco1.OpenText ("e:\rgb100.txt ")
    For i = 1 To 60
    i = i + 1
    riga1 = leggi1.ReadLine ()
    TextBox1.Text = riga1
    ListBox1.Items. Add(riga1)

    Application.DoE vents()
    System.Threadin g.Thread.Sleep( ms)
    Next i
    Loop
    End Sub
    End Class
  • JClinton
    New Member
    • Nov 2018
    • 1

    #2
    This Game Reads Text Files

    Module Module1

    Structure card 'shows layout

    Dim name As String 'display the name as A word

    Dim heading As Integer 'display heading as number

    Dim shooting As Integer 'display shooting as number

    Dim dribbling As Integer 'display dribbling as a number

    Dim injury As Integer 'display injury as a number

    End Structure 'ends the layout

    Dim chosencategory As String

    Dim whostarts As Integer = 0

    Dim loseorwin As Integer = 0

    Dim cards() As card

    Dim amountofcards As String

    Dim playershand() As card

    Dim computershand() As card

    Sub Main()

    Dim playersize As String

    playersize = amountofcards

    GetMenuChoice() 'goes to the sub GetMenuChoice()

    getdecksize() 'goes to the sub getdecksize()

    getnameoffootba llers() 'goes to the sub getnameoffootba llers()

    setattributes()

    dealhands(amoun tofcards) 'goes to the sub dealhands(amoun tofcards)

    If whostarts = 1 Then

    menu2()

    Else

    menu3()

    End If

    Console.ReadLin e()

    End Sub

    Sub menu2()

    altshowtopcard( )

    shiftcards()

    End Sub

    Sub menu3()

    showtopcard()

    shiftcards()

    End Sub

    Sub DisplayMenu()

    Console.WriteLi ne("Menu---------------------------------------------------------------------------")

    Console.WriteLi ne("1.Play game--------------------------------------------------------------------")

    Console.WriteLi ne("2.Quit-------------------------------------------------------------------------")

    Console.WriteLi ne("-------------------------------------------------------------------------------")

    Console.WriteLi ne("-------------------------------------------------------------------------------")

    Console.WriteLi ne("-------------------------------------------------------------------------------")

    Console.WriteLi ne("------------------------please enter a number above----------------------------")

    Console.WriteLi ne("-------------------------------------------------------------------------------")

    Console.WriteLi ne("-------------------------------------------------------------------------------")

    End Sub

    Function GetMenuChoice() As Integer

    Dim menuChoice As String

    Dim isvalid As Boolean

    isvalid = False

    menuChoice = ""

    Do Until isvalid = True 'when it gets to true you are safe to proceed

    DisplayMenu() 'shows the menu

    menuChoice = Console.ReadLin e 'saves the entered choice

    isvalid = isoptionvalid(m enuChoice)

    Loop

    Return menuChoice

    End Function

    Function isoptionvalid(C hoosenOption As String) As Boolean



    If ChoosenOption.L ength = 0 Then

    Console.WriteLi ne("This is not valid")

    Return False

    End If

    If InStr("12345678 9", ChoosenOption) < 1 Then 'checks if the number is smaller then one if it is it returns error

    Console.WriteLi ne("This is not valid")

    Return False

    End If

    If ChoosenOption > 2 Then 'checks if the number is bigger then two if it is it returns error

    Console.WriteLi ne("This is too long, try again")

    Return False

    End If

    If ChoosenOption = 2 Then 'if the entered number is two it ends the program

    MsgBox("Goodbye ") 'says goodbye when user enters the second option

    End

    End If

    Return True

    End Function

    Sub getdecksize()

    Console.WriteLi ne("How many cards do you want to play with? please enter an even number between 4 and 22") 'asks how many cards you want to play with

    amountofcards = Console.ReadLin e()

    Do Until checkdecksize(a mountofcards) = True

    Console.WriteLi ne("How many cards do you want to play with? please enter an even number between 4 and 22") 'asks how many cards you want to play with

    amountofcards = Console.ReadLin e()

    Loop

    End Sub

    Function checkdecksize(a mountofcards As String) As Boolean

    If amountofcards.L ength = 0 Then

    Console.WriteLi ne("Please try again, press space to start again!!")

    Console.ReadLin e()

    Return False

    End If

    If amountofcards.L ength > 2 Then

    Console.WriteLi ne("Please try again, press space to start again!!")

    Console.ReadLin e()

    Return False

    End If

    If amountofcards.L ength = 1 Then

    If InStr("01234567 89", amountofcards) < 1 Then

    Console.WriteLi ne("Please try again, press space to start again!!")

    Console.ReadLin e()

    Return False

    End If

    End If

    If amountofcards.L ength = 2 Then

    If InStr("01234567 89", Mid(amountofcar ds, 1, 1)) < 1 Then

    Console.WriteLi ne("Please try again, press space to start again!!")

    Console.ReadLin e()

    Return False

    End If

    If InStr("01234567 89", Mid(amountofcar ds, 2, 1)) < 1 Then

    Console.WriteLi ne("Please try again, press space to start again!!")

    Console.ReadLin e()

    Return False

    End If

    End If





    If amountofcards < 4 Or amountofcards > 22 Then

    Console.WriteLi ne("Please try again, press space to start again!!")

    Console.ReadLin e()

    Return False

    ElseIf amountofcards Mod 2 > 0 Then

    Console.WriteLi ne("Please try again, press space to start again!!")

    Console.ReadLin e()

    Return False

    End If

    Return True

    End Function



    Sub getnameoffootba llers()

    Dim counter As Integer = 0

    FileOpen(1, "C:\FileName.tx t", OpenMode.Input)

    Do Until EOF(1)

    ReDim Preserve cards(counter)

    cards(counter). name = LineInput(1)



    counter = counter + 1

    Loop

    FileClose(1)

    End Sub

    Sub dealhands(games ize As Integer)

    ReDim playershand((ga mesize / 2) - 1)

    ReDim computershand(( gamesize / 2) - 1)

    For looper = 0 To ((gamesize / 2) - 1)

    Randomize()

    Dim randomnumber = Int(Rnd() * cards.Length)

    playershand(loo per) = cards(randomnum ber)

    removefromdeck( randomnumber)

    Randomize()

    randomnumber = Int(Rnd() * cards.Length)

    computershand(l ooper) = cards(randomnum ber)

    removefromdeck( randomnumber)

    Next

    End Sub

    Sub removefromdeck( i As Integer)

    For looper = i To cards.Length - 2

    cards(looper) = cards(looper + 1)

    Next

    ReDim Preserve cards(cards.Len gth - 2)

    End Sub

    Sub setattributes()

    For looper = 0 To cards.Length - 1

    cards(looper).h eading = Int(Rnd() * 5 + 1)

    cards(looper).s hooting = Int(Rnd() * 100 + 1)

    cards(looper).d ribbling = Int(Rnd() * 10 + 1)

    cards(looper).i njury = Int(Rnd() * 10 + 1)

    Next

    End Sub

    Sub altshowtopcard( )

    Dim computerscatogo ry As Integer

    computerscatogo ry = Int(Rnd() * 4 + 1)

    Console.WriteLi ne("computer is choosing...")

    Console.WriteLi ne("the computer has chosen " & computerscatogo ry)

    Console.ReadLin e()

    GetTopCard("P")

    Console.WriteLi ne("")

    Console.WriteLi ne("")

    GetTopCard("c")

    If computerscatogo ry = 1 Then comparestats1()

    If computerscatogo ry = 2 Then comparestats2()

    If computerscatogo ry = 3 Then comparestats3()

    If computerscatogo ry = 4 Then comparestats4()

    End Sub

    Sub GetTopCard(x As Char)

    loseorwin = 0

    If x = "P" Then

    Console.WriteLi ne("Players Card")

    Console.WriteLi ne("name: " & playershand(0). name)

    Console.WriteLi ne("1.heading: " & playershand(0). heading)

    Console.WriteLi ne("2.shooting : " & playershand(0). shooting)

    Console.WriteLi ne("3.dribbling : " & playershand(0). dribbling)

    Console.WriteLi ne("4.Injury: " & playershand(0). injury)

    Else

    Console.WriteLi ne("Computers Card")

    Console.WriteLi ne("name: " & computershand(0 ).name)

    Console.WriteLi ne("1.heading: " & computershand(0 ).heading)

    Console.WriteLi ne("2.shooting : " & computershand(0 ).shooting)

    Console.WriteLi ne("3.dribbling : " & computershand(0 ).dribbling)

    Console.WriteLi ne("4.Injury: " & computershand(0 ).injury)

    End If

    End Sub

    Function showtopcard()

    Dim chosencategory As String = 0

    GetTopCard("P")

    Console.WriteLi ne("")

    Console.WriteLi ne("please select a category to compare with the computer (1,2,3,4)")

    Dim isvalid1 As Boolean

    isvalid1 = False

    Do Until isvalid1 = True

    chosencategory = Console.ReadLin e

    isvalid1 = iscategoryvalid (chosencategory )

    Loop

    GetTopCard("c")

    If chosencategory = 1 Then comparestats1()

    If chosencategory = 2 Then comparestats2()

    If chosencategory = 3 Then comparestats3()

    If chosencategory = 4 Then comparestats4()

    Return chosencategory

    End Function

    Function iscategoryvalid (chosencategory As String) As Boolean

    If chosencategory. Length = 0 Then

    Console.WriteLi ne("This is not valid")

    Return False

    End If

    If InStr("12345678 9", chosencategory) < 1 Then

    Console.WriteLi ne("This is not valid, please try again")

    Return False

    End If

    If chosencategory > 4 Then

    Console.WriteLi ne("This is too long, try again")

    Return False

    End If

    Return True

    End Function

    Sub comparestats1()

    If playershand(0). heading >= computershand(0 ).heading Then loseorwin = 1

    End Sub

    Sub comparestats2()

    If playershand(0). shooting >= computershand(0 ).shooting Then loseorwin = 1

    End Sub

    Sub comparestats3()

    If playershand(0). dribbling >= computershand(0 ).dribbling Then loseorwin = 1

    End Sub

    Sub comparestats4()

    If playershand(0). injury <= computershand(0 ).injury Then loseorwin = 1

    End Sub

    Sub shiftcards()

    If loseorwin = 1 Then

    Console.WriteLi ne("")

    Console.WriteLi ne("YOU WIN")

    Console.WriteLi ne("")

    playerwinswapca rds()

    Else

    Console.WriteLi ne("")

    Console.WriteLi ne("you LOSE")

    Console.WriteLi ne("")

    computerwinswap cards()

    End If

    loseorwin = 0

    End Sub

    Sub playerwinswapca rds()

    ReDim Preserve playershand(pla yershand.Length )

    playershand(pla yershand.Length - 1) = playershand(0)

    For looper = 0 To playershand.Len gth - 2

    playershand(loo per) = playershand(loo per + 1)

    Next

    playershand(pla yershand.Length - 1) = computershand(0 )

    For looper = 0 To computershand.L ength - 2

    computershand(l ooper) = computershand(l ooper + 1)

    Next

    ReDim Preserve computershand(c omputershand.Le ngth - 2)

    stub()

    End Sub

    Sub computerwinswap cards()

    ReDim Preserve computershand(c omputershand.Le ngth)

    computershand(c omputershand.Le ngth - 1) = computershand(0 )

    For looper = 0 To computershand.L ength - 2

    computershand(l ooper) = computershand(l ooper + 1)

    Next

    computershand(c omputershand.Le ngth - 1) = playershand(0)

    For looper = 0 To playershand.Len gth - 2

    playershand(loo per) = playershand(loo per + 1)

    Next

    ReDim Preserve playershand(pla yershand.Length - 2)

    stub1()

    End Sub

    Sub whowins()

    If playershand.Len gth = 0 Then

    Console.WriteLi ne("You LOSE The GAME!")

    Console.ReadLin e()

    whostarts = 1

    Main()

    End If

    If computershand.L ength = 0 Then

    Console.WriteLi ne("You WIN The GAME!")

    Console.ReadLin e()

    whostarts = 0

    Main()

    End If

    End Sub

    Sub stub()

    whowins()

    showtopcard()

    shiftcards()

    End Sub

    Sub stub1()

    whowins()

    altshowtopcard( )

    shiftcards()

    End Sub

    End Module

    Comment

    Working...