Need help with a new idea - text imports help

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1156 Views - Last Post: 20 July 2011 - 10:32 AM Rate Topic: -----

#1 gryphin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 128
  • Joined: 29-October 05

Need help with a new idea - text imports help

Posted 18 July 2011 - 08:38 PM

Ok, so I have an idea for a new program and need help... Yeah, I know... I need a lot of help...

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with a new idea - text imports help

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Need help with a new idea - text imports help

Posted 18 July 2011 - 08:59 PM

StreamReader is your best friend buddy. http://msdn.microsof...reamreader.aspx

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! :)
Was This Post Helpful? 0
  • +
  • -

#3 gryphin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 128
  • Joined: 29-October 05

Re: Need help with a new idea - text imports help

Posted 18 July 2011 - 10:01 PM

Not sure that I fully understand the way they have it on those pages... So I am going to post my code I have so far and maybe someone can share what pieces are missing.

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

Was This Post Helpful? 0
  • +
  • -

#4 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Need help with a new idea - text imports help

Posted 18 July 2011 - 10:36 PM

First you have to instantiate your object


'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! :)
Was This Post Helpful? 0
  • +
  • -

#5 gryphin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 128
  • Joined: 29-October 05

Re: Need help with a new idea - text imports help

Posted 18 July 2011 - 10:59 PM

Now, please tell me if I screwed something up... I want to fix it...

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


Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Need help with a new idea - text imports help

Posted 19 July 2011 - 01:11 AM

You have the following problems:
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") 

Was This Post Helpful? 0
  • +
  • -

#7 gryphin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 128
  • Joined: 29-October 05

Re: Need help with a new idea - text imports help

Posted 19 July 2011 - 07:19 AM

Ok, so I put the "" around action.txt. It cut out a bunch of the errors.... But no clue what the other ones need done to fix.

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

Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Need help with a new idea - text imports help

Posted 19 July 2011 - 03:01 PM

You still did the same thing, you put Dim MyReader As System.IO.StreamReader = New and System.IO.StreamReader("c:\Action.txt") in two different lines. they should be in the same line!!
Was This Post Helpful? 1
  • +
  • -

#9 gryphin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 128
  • Joined: 29-October 05

Re: Need help with a new idea - text imports help

Posted 19 July 2011 - 05:32 PM

Ok... So I corrected the issue above and it now has 0 errors. However, it is not reading an entire line into the label, it is taking 1 random letter from the top line in the list... How do I make it use a line and not a letter from the line?

 
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


Was This Post Helpful? 0
  • +
  • -

#10 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Need help with a new idea - text imports help

Posted 19 July 2011 - 09:19 PM

View Postgryphin, on 19 July 2011 - 05:32 PM, said:

Ok... So I corrected the issue above and it now has 0 errors. However, it is not reading an entire line into the label, it is taking 1 random letter from the top line in the list... How do I make it use a line and not a letter from the line?

 
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. :)
Was This Post Helpful? 0
  • +
  • -

#11 gryphin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 128
  • Joined: 29-October 05

Re: Need help with a new idea - text imports help

Posted 20 July 2011 - 08:45 AM

View Posttrevster344, 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.
Was This Post Helpful? 0
  • +
  • -

#12 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: Need help with a new idea - text imports help

Posted 20 July 2011 - 08:51 AM

Strings need to be put inside quotations, just had to throw that in there..
Was This Post Helpful? 0
  • +
  • -

#13 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Need help with a new idea - text imports help

Posted 20 July 2011 - 09:08 AM

After the period is where you choose which method you'd like to use to begin pulling the string apart man.

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

Was This Post Helpful? 0
  • +
  • -

#14 gryphin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 128
  • Joined: 29-October 05

Re: Need help with a new idea - text imports help

Posted 20 July 2011 - 10:03 AM

View Posttrevster344, on 20 July 2011 - 10:08 AM, said:

After the period is where you choose which method you'd like to use to begin pulling the string apart man.

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.
Was This Post Helpful? -1
  • +
  • -

#15 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Need help with a new idea - text imports help

Posted 20 July 2011 - 10:09 AM

Good luck to you on your endeavor.

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

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2