-
Jul 4th, 2020, 06:25 PM
#1
Thread Starter
Lively Member
[HELP] My random code in text file need a little adjustment!
Hello! glad to talk with you again! you are very effective! !
ok i have a little problem with a code!
indeed, my code generate a random file but sometimes i have no file,error or a blank in a mediaplayer or other stuff!
here is my code:
Code:
Dim Randomtaunt As New Random()
Dim Tra As IO.TextReader = System.IO.File.OpenText(Application.StartupPath & "\movies\TAUNTVICTORY\" & Me.Label23.Text & ".txt")
Dim FileLinest() As String = Split(Tra.ReadToEnd(), vbCrLf)
Tra.Close()
Dim MyTAUNT As String = FileLinest(Randomtaunt.Next(0, UBound(FileLinest)))
Me.AxWindowsMediaPlayer5.URL = Application.StartupPath & "\movies\TAUNTVICTORY\" & MyTAUNT
Me.AxWindowsMediaPlayer5.settings.setMode("loop", False)
Me.AxWindowsMediaPlayer5.Ctlcontrols.play()
i suspect when the computer read my text file it could read a blank line. how must we fix it?
thank you in advance for your answer!
Last edited by danzey; Jul 4th, 2020 at 06:48 PM.
-
Jul 4th, 2020, 07:10 PM
#2
Re: [HELP] My random code in text file need a little adjustment!
Well, the first thing you need to do is break things into steps as it looks like you just randomly chucked what you found on google into a method.
Step one: Read the random class. Since the default random is time dependent you will generate the same seeded numbers.
https://docs.microsoft.com/en-us/dot...ew=netcore-3.1
step 2: read a text file using file.readalltext https://docs.microsoft.com/en-us/dot...System_String_
https://docs.microsoft.com/en-us/dot...ew=netcore-3.1
https://docs.microsoft.com/en-us/dot...veEmptyEntries
write the first parts then come back
-
Jul 4th, 2020, 07:56 PM
#3
Re: [HELP] My random code in text file need a little adjustment!
First, declare your random object at form level...
Code:
Dim Randomtaunt As New Random()
Then, be sure MyTAUNT <> ""...
Code:
Dim FileLinest() As String = io.file.readalllines(Application.StartupPath & "\movies\TAUNTVICTORY\" & Me.Label23.Text & ".txt").where(function(f) f.Trim <> "").ToArray
Dim MyTAUNT As String = FileLinest(Randomtaunt.Next(0, FileLinest.Length))
Me.AxWindowsMediaPlayer5.URL = Application.StartupPath & "\movies\TAUNTVICTORY\" & MyTAUNT
Me.AxWindowsMediaPlayer5.settings.setMode("loop", False)
Me.AxWindowsMediaPlayer5.Ctlcontrols.play()
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 4th, 2020, 08:01 PM
#4
Thread Starter
Lively Member
Re: [HELP] My random code in text file need a little adjustment!
ok i will do my best! thank you for hins.
-
Jul 4th, 2020, 08:03 PM
#5
Re: [HELP] My random code in text file need a little adjustment!
My code checks the line isn't empty
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 4th, 2020, 08:47 PM
#6
Thread Starter
Lively Member
Re: [HELP] My random code in text file need a little adjustment!
Originally Posted by .paul.
My code checks the line isn't empty
BIG THANK YOU PAUL! YOU helped me so much! you are a real gentleman! you code is perfect!
my regards
-
Jul 4th, 2020, 10:57 PM
#7
Re: [HELP] My random code in text file need a little adjustment!
If your issue is resolved then please use the Thread Tools menu to mark the thread Resolved. That will save anyone else opening the thread and reading six posts, only to find that there's no more help required.
-
Jul 5th, 2020, 09:31 AM
#8
Re: [HELP] My random code in text file need a little adjustment!
Originally Posted by danzey
BIG THANK YOU PAUL! YOU helped me so much! you are a real gentleman! you code is perfect!
my regards
Its not perfect and I guess you didnt read a single thing I posted and just accepted code instead of wanting to learn.
IsNullOrWhiteSpace
Trim the string is iterated through from each end.
-
Jul 5th, 2020, 10:16 AM
#9
Re: [HELP] My random code in text file need a little adjustment!
Originally Posted by ident
Its not perfect and I guess you didnt read a single thing I posted and just accepted code instead of wanting to learn.
IsNullOrWhiteSpace
Trim the string is iterated through from each end.
It reads the text file omitting any empty lines. That's all the OP wanted
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 5th, 2020, 10:45 AM
#10
Re: [HELP] My random code in text file need a little adjustment!
Originally Posted by .paul.
First, declare your random object at form level...
Code:
Dim Randomtaunt As New Random()
Then, be sure MyTAUNT <> ""...
Code:
Dim FileLinest() As String = io.file.readalllines(Application.StartupPath & "\movies\TAUNTVICTORY\" & Me.Label23.Text & ".txt").where(function(f) f.Trim <> "").ToArray
Dim MyTAUNT As String = FileLinest(Randomtaunt.Next(0, FileLinest.Length))
Me.AxWindowsMediaPlayer5.URL = Application.StartupPath & "\movies\TAUNTVICTORY\" & MyTAUNT
Me.AxWindowsMediaPlayer5.settings.setMode("loop", False)
Me.AxWindowsMediaPlayer5.Ctlcontrols.play()
That code should call File.ReadLines rather than File.ReadAllLines. The latter will read the whole file into an array first, then process that array and create another array, while the former will process each line as it's read from the file and only create one array when it's done. Also, while I realise it was copied from the OP, the path building should be done using Path.Combine. It's the same folder path but different file paths so the folder path should be built once and then that used to build each file path:
vb.net Code:
Dim folderPath = Path.Combine(Application.StartupPath, "movies\TAUNTVICTORY")
Dim filePath = Path.Combine(folderPath, Label23.Text & ".txt")
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|