16 Replies - 928 Views - Last Post: 20 July 2011 - 10:32 AM
#1
Need help with a new idea - text imports help
Posted 18 July 2011 - 08:38 PM
So I can arrange the labels and buttons but need help with a part of it. Let us use the below psudo layout to show what I want to do. I then need the command I would use and an example of how to put it in - hoping I do not need the exact but enough to know where to put it.
Lets say the program will have 1 button for now. It will have 3 labels. lblAction, lblItem, and lblLocation. When the button is pressed I want it to change change the label to an item from a text file - so lets say lblAction will pull from "Action.txt" for example. And then in Action.txt I will have 1 action per line (stop, drop, roll, hug, etc) and same for other files (except appropriate type of words). I want to make it so the pressing of the button pulls a random word/line from the text file and puts it into a string to use for the label.
How do I do this?
So far all I have done is 4 words per text file (Action.txt, Item.txt, and Location.txt), the design form has a button and 3 labels, and I have double clicked the button to make the sub for the button.
Replies To: Need help with a new idea - text imports help
#2
Re: Need help with a new idea - text imports help
Posted 18 July 2011 - 08:59 PM
You'll instantiate the object, open the file, use the .readline() method, and assign it to a string variable. You could also generate a number, and use a case statement to pick the line you want to read by calling the readline method multiple times until you get where you want it.
I've run out of time, but this will get you started. I'll return when I have time to get you the rest, good luck for now!
#3
Re: Need help with a new idea - text imports help
Posted 18 July 2011 - 10:01 PM
Imports System
Imports System.IO
Public Class Form1
'to be able to use random lines of text
Dim randomizer As New Random
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'to pull from a text file and put into lblAction
lblAction.Readline()
End Sub
End Class
What is missing that would allow me to use the readline command because it is underlined blue in VB.net 2010 express.
Error =
Error 1 'Readline' is not a member of 'System.Windows.Forms.Label'. C:\Users\Brian\Documents\Visual Studio 2010\Projects\FunTimesHelper\FunTimesHelper\Form1.vb 16 9 FunTimesHelper
Also, this is being done as a Windows Form if that makes a difference.
This post has been edited by gryphin: 18 July 2011 - 10:02 PM
#4
Re: Need help with a new idea - text imports help
Posted 18 July 2011 - 10:36 PM
'Good job, a random object. :)/> Dim randomizer As New Random Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'New StreamReader Object Dim MyReader as System.io.streamreader = new System.io.streamreader(C:\Action.txt) 'your path goes there, that's just an example' 'Try this one, do what you will with it, but this will generate your number between 1, and 5. :)/> Dim theRand as integer = randomizer.next(1,5) 'Your string to which you'll place the value of the readline. Dim TheLine as string = MyReader.readline() 'manipulate your controls text lblAction.text = TheLine 'Dispose of your StreamReader object MyReader.Dispose end sub
Now you should try using a case statement for the random number, so if it's 1 tell it to readline, if it's 2 then readline twice. I'm sure there is probably a cleaner way, but that's the easiest one I can think of off the top of my head.
Hope this helps!
#5
Re: Need help with a new idea - text imports help
Posted 18 July 2011 - 10:59 PM
Errors -
Error 1 Type expected. C:\Users\Brian\Documents\Visual Studio 2010\Projects\FunTimesHelper\FunTimesHelper\Form1.vb 16 52 FunTimesHelper Error 2 'StreamReader' is a type in 'IO' and cannot be used as an expression. C:\Users\Brian\Documents\Visual Studio 2010\Projects\FunTimesHelper\FunTimesHelper\Form1.vb 19 9 FunTimesHelper Error 3 ')' expected. C:\Users\Brian\Documents\Visual Studio 2010\Projects\FunTimesHelper\FunTimesHelper\Form1.vb 19 32 FunTimesHelper Error 4 'c' is not declared. It may be inaccessible due to its protection level. C:\Users\Brian\Documents\Visual Studio 2010\Projects\FunTimesHelper\FunTimesHelper\Form1.vb 19 32 FunTimesHelper Error 5 Syntax error. C:\Users\Brian\Documents\Visual Studio 2010\Projects\FunTimesHelper\FunTimesHelper\Form1.vb 19 34 FunTimesHelper Error 6 'MyReader' is not declared. It may be inaccessible due to its protection level. C:\Users\Brian\Documents\Visual Studio 2010\Projects\FunTimesHelper\FunTimesHelper\Form1.vb 25 33 FunTimesHelper Error 7 'MyReader' is not declared. It may be inaccessible due to its protection level. C:\Users\Brian\Documents\Visual Studio 2010\Projects\FunTimesHelper\FunTimesHelper\Form1.vb 31 9 FunTimesHelper
Code -
Imports System
Imports System.IO
Public Class Form1
'to be able to use random lines of text
Dim randomizer As New Random
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'new streamreader object
Dim MyReader As System.IO.StreamReader = New
'path goes in the ()
System.IO.StreamReader(c:\Action.txt)
'this generates random number between 1 and 5
Dim theRand As Integer = randomizer.Next(1, 5)
'the string where the value of readline goes
Dim TheLine As String = MyReader.readline()
'manipulates text of label
lblAction.Text = TheLine
'gets rid of streamreader object
MyReader.Dispose()
End Sub
End Class
#6
Re: Need help with a new idea - text imports help
Posted 19 July 2011 - 01:11 AM
1. Path is a string, so it must be in the ""
2. This is the same line statement:
Dim MyReader As System.IO.StreamReader = New
'path goes in the ()
System.IO.StreamReader(c:\Action.txt)
It should be:
Dim MyReader As System.IO.StreamReader = New System.IO.StreamReader("c:\Action.txt")
#7
Re: Need help with a new idea - text imports help
Posted 19 July 2011 - 07:19 AM
errors now are -
Error 1 Type expected. C:\Users\Brian\Documents\Visual Studio 2010\Projects\FunTimesHelper\FunTimesHelper\Form1.vb 16 52 FunTimesHelper Error 2 'StreamReader' is a type in 'IO' and cannot be used as an expression. C:\Users\Brian\Documents\Visual Studio 2010\Projects\FunTimesHelper\FunTimesHelper\Form1.vb 17 9 FunTimesHelper Error 3 'MyReader' is not declared. It may be inaccessible due to its protection level. C:\Users\Brian\Documents\Visual Studio 2010\Projects\FunTimesHelper\FunTimesHelper\Form1.vb 25 33 FunTimesHelper Error 4 'MyReader' is not declared. It may be inaccessible due to its protection level. C:\Users\Brian\Documents\Visual Studio 2010\Projects\FunTimesHelper\FunTimesHelper\Form1.vb 31 9 FunTimesHelper
Code with changes is -
Imports System
Imports System.IO
Public Class Form1
'to be able to use random lines of text
Dim randomizer As New Random
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'new streamreader object
Dim MyReader As System.IO.StreamReader = New
System.IO.StreamReader("c:\Action.txt")
'path goes in the ()
'this generates random number between 1 and 5
Dim theRand As Integer = randomizer.Next(1, 5)
'the string where the value of readline goes
Dim TheLine As String = MyReader.readline()
'manipulates text of label
lblAction.Text = TheLine
'gets rid of streamreader object
MyReader.Dispose()
End Sub
End Class
#8
Re: Need help with a new idea - text imports help
Posted 19 July 2011 - 03:01 PM
#9
Re: Need help with a new idea - text imports help
Posted 19 July 2011 - 05:32 PM
Imports System
Imports System.IO
Public Class Form1
'to be able to use random lines of text
Dim randomizer As New Random
Dim fileReader As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'new streamreader object
Dim MyReader As System.IO.StreamReader = New System.IO.StreamReader(My.Computer.FileSystem.CurrentDirectory + "\Action.txt")
'path goes in the ()
'this generates random number between 1 and 5
Dim theRand As Integer = randomizer.Next(1, 5)
'the string where the value of readline goes
Dim TheLine As String = MyReader.ReadLine(theRand - 1)
'manipulates text of label
lblAction.Text = TheLine
'gets rid of streamreader object
MyReader.Dispose()
End Sub
End Class
#10
Re: Need help with a new idea - text imports help
Posted 19 July 2011 - 09:19 PM
gryphin, on 19 July 2011 - 05:32 PM, said:
Imports System
Imports System.IO
Public Class Form1
'to be able to use random lines of text
Dim randomizer As New Random
Dim fileReader As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'new streamreader object
Dim MyReader As System.IO.StreamReader = New System.IO.StreamReader(My.Computer.FileSystem.CurrentDirectory + "\Action.txt")
'path goes in the ()
'this generates random number between 1 and 5
Dim theRand As Integer = randomizer.Next(1, 5)
'the string where the value of readline goes
Dim TheLine As String = MyReader.ReadLine(theRand - 1)
'manipulates text of label
lblAction.Text = TheLine
'gets rid of streamreader object
MyReader.Dispose()
End Sub
End Class
Dim TheLine As String = MyReader.ReadLine(theRand - 1)
There's your problem. If you want to grab something from the line don't use an argument. take TheLine and create various other string variables, where you use the various string manipulation methods such as SubString.
Dim TheLine as String = MyReader.ReadLine() Dim FirstStep as String = TheLine. <<<<
I like to think of it as chipping at the rough edges. The string itself isn't something you are changing, you're just getting real picky with it.
#11
Re: Need help with a new idea - text imports help
Posted 20 July 2011 - 08:45 AM
trevster344, on 19 July 2011 - 10:19 PM, said:
Dim TheLine as String = MyReader.ReadLine() Dim FirstStep as String = TheLine. <<<<
I like to think of it as chipping at the rough edges. The string itself isn't something you are changing, you're just getting real picky with it.
I do not understand the last part - Dim FirstStep as String = TheLine. <<<<
What is the <<<< for? I assume it needs something else in that spot, but I do not know what because I am not yet familiar with the layout of the commands in VB.
#12
Re: Need help with a new idea - text imports help
Posted 20 July 2011 - 08:51 AM
#13
Re: Need help with a new idea - text imports help
Posted 20 July 2011 - 09:08 AM
http://msdn.microsof...v=vs.71%29.aspx
Refresh yourself on string manipulation.
This post has been edited by trevster344: 20 July 2011 - 09:09 AM
#14
Re: Need help with a new idea - text imports help
Posted 20 July 2011 - 10:03 AM
trevster344, on 20 July 2011 - 10:08 AM, said:
http://msdn.microsof...v=vs.71%29.aspx
Refresh yourself on string manipulation.
Ok, 2 things...
1. There is no refreshing, I am learning from scratch - never went to school or had a job that taught me VB.
2. I have a hard time making heads and tails of those MSDN pages. They do not seem well written for a self taught person, they look more geared towards those with experience in programming or else those having gone to school for it.
I would appreciate any help from someone willing to describe this in normal english so I can understand it. I am not a programmer and have only been self teaching myself VB for a couple days now.
#15
Re: Need help with a new idea - text imports help
Posted 20 July 2011 - 10:09 AM
http://www.dreaminco...1&#entry1272994
http://www.dreaminco..._1&#entry974418
Last but not least, if you are confused about the string manipulation part. Keep in mind it's not one method that will do the job, but a combination. It's up to you to think that part through, I've found there are more than enough methods that in combination can do anything.
This post has been edited by trevster344: 20 July 2011 - 10:24 AM
|
|

New Topic/Question
Reply



MultiQuote




|