Results 1 to 10 of 10

Thread: adding .txt files to my program

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    467

    adding .txt files to my program

    Hi, i have a program that when you click a button it randomly gets a random line from the file and adds it to a textbox.

    Well when i give ppl the program it needs to have the .txt file in the same dir right.

    But how would i add the file to the program itself ?

    Or

    Add the contents of the file to the project ? maybe a class or something idk.

    Thanks for any help

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: adding .txt files to my program

    Right-click the project, select "Add" -> "Existing Item" ... find your txt file click OK. Then click on the file in the solution explorer, right-click, select Properties... make sure the Build Action is "None" and the Copy option is set to Copy to Output folder. Now it'll go with the application...

    When it you give it to others, simply copy the contents of the bin/debug or bin/release folder.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    467

    Re: adding .txt files to my program

    well the file is still there as outside of the program, reason i asked for as it would be easyer if they did not see the .txt file

    Thanks

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: adding .txt files to my program

    Then just do not copy it to the out directory and reference it via my.resources.***

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    467

    Re: adding .txt files to my program

    Quote Originally Posted by ident View Post
    Then just do not copy it to the out directory and reference it via my.resources.***
    Ok so if i do this how would i read a random line from it with this:

    Code:
    http://www.vbforums.com/showthread.php?p=4063600#post4063600
    could be 2 birds killed with 1 stone

    Thanks

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: adding .txt files to my program

    Just create an array reading each line. Create a random number within the bounds of the array count. Then just read the returned random line number.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    467

    Re: adding .txt files to my program

    Hmm, im new to reading from files ect, I have no idea how to do this and from the other thread like you said too many bad habits. I cant learn if i dont know where to start, i normally learn from examples but some codes are bad habits.

    Thanks for any help

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: adding .txt files to my program

    So what class handles reading of files? Tkae the time to read System.IO.File namespace http://msdn.microsoft.com/en-us/libr...m.io.file.aspx

    To create an array of each line

    Code:
            Dim lines() As String = Strings.Split(My.Resources.MyText, Strings.Chr(13) & Strings.Chr(10))

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    467

    Re: adding .txt files to my program

    You see there is nothing atm, just 1 textbox and 1 button for me to learn this.

    I have tryed this but get errors

    Code:
    Dim RandomNumber As New Random()
            Dim Tr As IO.TextReader = System.IO.File.OpenText(My.Resources.fname)
            Dim FileLines() As String = Split(Tr.ReadToEnd(), vbCrLf)
            Tr.Close()
            Dim MyRandomLine As String = FileLines(RandomNumber.Next(0, UBound(FileLines)))
            TextBox1.Text = MyRandomLine
    error catches on this line "System.IO.File.OpenText(My.Resources.fname)" and says "Illegal characters in path."

    Thanks

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    467

    Re: adding .txt files to my program

    nvm, i got it with this:

    VB CODE:
    Code:
            Dim RandomNumber As New Random()
            Dim lines() As String = Strings.Split(My.Resources.fname, Strings.Chr(13) & Strings.Chr(10))
            Dim MyRandomLine As String = Lines(RandomNumber.Next(0, UBound(Lines)))
            TextBox1.Text = MyRandomLine
    How does this look ? it works fine tho but i may have overlooked something

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width