423,688 Members | 1,889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,688 IT Pros & Developers. It's quick & easy.

reading txt file

P: 1
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.StreamReader
Dim ms As Integer = 60000

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Size = Screen.PrimaryScreen.WorkingArea.Size
Me.Location = Screen.PrimaryScreen.WorkingArea.Location
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Button1.BackColor = 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.DoEvents()
System.Threading.Thread.Sleep(ms)
Next i
Loop
End Sub
End Class
2 Weeks Ago #1
Share this Question
Share on Google+
1 Reply


P: 1
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()

getnameoffootballers() 'goes to the sub getnameoffootballers()

setattributes()

dealhands(amountofcards) 'goes to the sub dealhands(amountofcards)

If whostarts = 1 Then

menu2()

Else

menu3()

End If

Console.ReadLine()

End Sub

Sub menu2()

altshowtopcard()

shiftcards()

End Sub

Sub menu3()

showtopcard()

shiftcards()

End Sub

Sub DisplayMenu()

Console.WriteLine("Menu---------------------------------------------------------------------------")

Console.WriteLine("1.Play game--------------------------------------------------------------------")

Console.WriteLine("2.Quit-------------------------------------------------------------------------")

Console.WriteLine("-------------------------------------------------------------------------------")

Console.WriteLine("-------------------------------------------------------------------------------")

Console.WriteLine("-------------------------------------------------------------------------------")

Console.WriteLine("------------------------please enter a number above----------------------------")

Console.WriteLine("-------------------------------------------------------------------------------")

Console.WriteLine("-------------------------------------------------------------------------------")

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.ReadLine 'saves the entered choice

isvalid = isoptionvalid(menuChoice)

Loop

Return menuChoice

End Function

Function isoptionvalid(ChoosenOption As String) As Boolean



If ChoosenOption.Length = 0 Then

Console.WriteLine("This is not valid")

Return False

End If

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

Console.WriteLine("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.WriteLine("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.WriteLine("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.ReadLine()

Do Until checkdecksize(amountofcards) = True

Console.WriteLine("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.ReadLine()

Loop

End Sub

Function checkdecksize(amountofcards As String) As Boolean

If amountofcards.Length = 0 Then

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

Console.ReadLine()

Return False

End If

If amountofcards.Length > 2 Then

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

Console.ReadLine()

Return False

End If

If amountofcards.Length = 1 Then

If InStr("0123456789", amountofcards) < 1 Then

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

Console.ReadLine()

Return False

End If

End If

If amountofcards.Length = 2 Then

If InStr("0123456789", Mid(amountofcards, 1, 1)) < 1 Then

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

Console.ReadLine()

Return False

End If

If InStr("0123456789", Mid(amountofcards, 2, 1)) < 1 Then

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

Console.ReadLine()

Return False

End If

End If





If amountofcards < 4 Or amountofcards > 22 Then

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

Console.ReadLine()

Return False

ElseIf amountofcards Mod 2 > 0 Then

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

Console.ReadLine()

Return False

End If

Return True

End Function



Sub getnameoffootballers()

Dim counter As Integer = 0

FileOpen(1, "C:\FileName.txt", 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(gamesize As Integer)

ReDim playershand((gamesize / 2) - 1)

ReDim computershand((gamesize / 2) - 1)

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

Randomize()

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

playershand(looper) = cards(randomnumber)

removefromdeck(randomnumber)

Randomize()

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

computershand(looper) = cards(randomnumber)

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.Length - 2)

End Sub

Sub setattributes()

For looper = 0 To cards.Length - 1

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

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

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

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

Next

End Sub

Sub altshowtopcard()

Dim computerscatogory As Integer

computerscatogory = Int(Rnd() * 4 + 1)

Console.WriteLine("computer is choosing...")

Console.WriteLine("the computer has chosen " & computerscatogory)

Console.ReadLine()

GetTopCard("P")

Console.WriteLine("")

Console.WriteLine("")

GetTopCard("c")

If computerscatogory = 1 Then comparestats1()

If computerscatogory = 2 Then comparestats2()

If computerscatogory = 3 Then comparestats3()

If computerscatogory = 4 Then comparestats4()

End Sub

Sub GetTopCard(x As Char)

loseorwin = 0

If x = "P" Then

Console.WriteLine("Players Card")

Console.WriteLine("name: " & playershand(0).name)

Console.WriteLine("1.heading: " & playershand(0).heading)

Console.WriteLine("2.shooting: " & playershand(0).shooting)

Console.WriteLine("3.dribbling: " & playershand(0).dribbling)

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

Else

Console.WriteLine("Computers Card")

Console.WriteLine("name: " & computershand(0).name)

Console.WriteLine("1.heading: " & computershand(0).heading)

Console.WriteLine("2.shooting: " & computershand(0).shooting)

Console.WriteLine("3.dribbling: " & computershand(0).dribbling)

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

End If

End Sub

Function showtopcard()

Dim chosencategory As String = 0

GetTopCard("P")

Console.WriteLine("")

Console.WriteLine("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.ReadLine

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.WriteLine("This is not valid")

Return False

End If

If InStr("123456789", chosencategory) < 1 Then

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

Return False

End If

If chosencategory > 4 Then

Console.WriteLine("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.WriteLine("")

Console.WriteLine("YOU WIN")

Console.WriteLine("")

playerwinswapcards()

Else

Console.WriteLine("")

Console.WriteLine("you LOSE")

Console.WriteLine("")

computerwinswapcards()

End If

loseorwin = 0

End Sub

Sub playerwinswapcards()

ReDim Preserve playershand(playershand.Length)

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

For looper = 0 To playershand.Length - 2

playershand(looper) = playershand(looper + 1)

Next

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

For looper = 0 To computershand.Length - 2

computershand(looper) = computershand(looper + 1)

Next

ReDim Preserve computershand(computershand.Length - 2)

stub()

End Sub

Sub computerwinswapcards()

ReDim Preserve computershand(computershand.Length)

computershand(computershand.Length - 1) = computershand(0)

For looper = 0 To computershand.Length - 2

computershand(looper) = computershand(looper + 1)

Next

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

For looper = 0 To playershand.Length - 2

playershand(looper) = playershand(looper + 1)

Next

ReDim Preserve playershand(playershand.Length - 2)

stub1()

End Sub

Sub whowins()

If playershand.Length = 0 Then

Console.WriteLine("You LOSE The GAME!")

Console.ReadLine()

whostarts = 1

Main()

End If

If computershand.Length = 0 Then

Console.WriteLine("You WIN The GAME!")

Console.ReadLine()

whostarts = 0

Main()

End If

End Sub

Sub stub()

whowins()

showtopcard()

shiftcards()

End Sub

Sub stub1()

whowins()

altshowtopcard()

shiftcards()

End Sub

End Module
1 Week Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.