1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

need a lil bit of help please with vb08

Discussion in 'Visual Basic .NET' started by cpaaddict, Oct 19, 2008.

  1. cpaaddict

    cpaaddict Supreme Member

    Joined:
    Apr 6, 2007
    Messages:
    1,312
    Likes Received:
    920
    Location:
    UK - Bristol
    ok all i have set up a lil chat bot as im a noob with vb etc but am following tuts and so on and trying things out. anyways like i said i have this chat bot that sends messages to the chatroom im in at the time. but what i want to know is.

    what and how do i go about being able to load a text file with random messages. ie more than one. so it then can post a random message rather than the same one over and over. so it cycles through my text file. how do i connect that to the current one i have now?

    i have vb08.

    can anyone help? i know what i want. i just dont know what icons i need to choose and link them with the correct code. all the other parts is working fine. it osts the message but the same one over and over. need to randomize them
     
  2. chc89

    chc89 Junior Member

    Joined:
    Jan 18, 2008
    Messages:
    118
    Likes Received:
    3
    You can use System.Io for reading text file and using streamreader fetch any random lines from it. You can store number of lines in text file so you can select random number from it and read that particular line.
     
  3. PostWhore

    PostWhore Registered Member

    Joined:
    Jul 22, 2008
    Messages:
    51
    Likes Received:
    2
    Add this right after Public Class Form1

    Code:
       Dim intLineCount As Integer = 0 'Variable to see how many lines are in the file
        Dim arrTextLines() As String 'An array where we'll place all the lines. 
        Dim RandomClass As New Random() 'Random Class
    
    Now place this in your Form1_Load
    Code:
         arrTextLines = System.IO.File.ReadAllLines("C:text.txt") 'Opening C:text.txt and loading it in the array
            intLineCount = arrTextLines.Length 'Counting how big is our array now
    
    Now place this in your Form1 Class outside of any Subs of Function
    Code:
        'This simple function will return a random line every time it's called
        Private Function RandomLine() As String
            RandomLine = arrTextLines(RandomClass.Next(0, intLineCount))
        End Function
    

    So now you just call RandomLine function everytime you are sending a message.
     
    • Thanks Thanks x 1
  4. razohad

    razohad Jr. VIP Jr. VIP Premium Member

    Joined:
    Jan 11, 2008
    Messages:
    701
    Likes Received:
    1,005
    Occupation:
    Software engginer
    Location:
    online most of the time
    you can add to you bot
    a listbox (say you call it listbox1)
    a textbox (say you call it textbox1)
    2 buttons (call it addbn, loadbn)
    and an OpenFileDialog (call it OpenFileDialog1)

    so when you click addbn put this code

    Code:
    Listbox1.Items.Add(textbox1.text)
    and when you click loadbn put this code
    Code:
    OpenFileDialog1.Filter = "Text Files|*.txt|All Files|*.*"
            OpenFileDialog1.Title = "Load Links file"
            OpenFileDialog1.FileName = ""
            If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim objReader As New System.IO.StreamReader(OpenFileDialog1.FileName)
                Do While objReader.Peek() <> -1
                    ListBox1.Items.Add(objReader.ReadLine)
                Loop
    
            End If
    add this function to your class

    Code:
    Private Function RandomM() As String
        RandomM = listbox1.items.item(RandomClass.Next(0, listbox1.items.count))
    End Function
    now every time you want to grab a random message
    just call RandomM

    let me know if you need more help.

    :)
     
    • Thanks Thanks x 2