random result displayed in a textbox from array

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

45 Replies - 8844 Views - Last Post: 30 July 2010 - 08:41 AM Rate Topic: -----

#31 Justin3085  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-December 09

Re: random result displayed in a textbox from array

Posted 29 July 2010 - 11:12 AM

View PostNoBrain, on 29 July 2010 - 09:52 AM, said:

check if your file is not empty


File isn't empty, I only put a few entries in though to see if it worked.
Was This Post Helpful? 0
  • +
  • -

#32 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: random result displayed in a textbox from array

Posted 29 July 2010 - 11:30 AM

ok so copy your file in the folder : YourProjectFolder\Bin\Debug\ then change the code to this:
Try
        Dim sr As New System.IO.StreamReader(Application.StartupPath & "\roads.txt")
         'Catch ex As Exception
         'while there is still more in the text file
         While (Not sr.EndOfStream)
             'read a line
             Dim road = sr.ReadLine()
             'if the line isn't just empty space
             If Not road.Trim() = String.Empty Then
                 'add the line to the roads list
                 roads.Add(road)
             End If
         End While
     End Using
 Catch ex As Exception
     MessageBox.Show(ex.Message)
 End Try




this way your text file is where your exe is so it is sure to be find. for safety you can list all the items too. just to be sure that the file is loaded. also try debugging this method and see if something is happening :)

here is a tutorial about debugging if you dont know how to use it
Debugging

This post has been edited by NoBrain: 29 July 2010 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

#33 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: random result displayed in a textbox from array

Posted 29 July 2010 - 11:35 AM

Justin, are you sure the LoadRoads() is being called?
Was This Post Helpful? 1
  • +
  • -

#34 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: random result displayed in a textbox from array

Posted 29 July 2010 - 11:46 AM

a very good question :)

This post has been edited by NoBrain: 29 July 2010 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#35 Justin3085  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-December 09

Re: random result displayed in a textbox from array

Posted 29 July 2010 - 12:18 PM

Let me ask this. If I add the roads into the code it runs and shows randomly perfectly. At what point is adding too much in code bad instead of using a stream reader. I would have to recompile if I wanted to add/delete roads correct?

View PostCharlieMay, on 29 July 2010 - 10:35 AM, said:

Justin, are you sure the LoadRoads() is being called?


somehow, I don't think it is. That is probably what the problem is.
Was This Post Helpful? 0
  • +
  • -

#36 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,902
  • Joined: 08-June 10

Re: random result displayed in a textbox from array

Posted 29 July 2010 - 12:25 PM

The idea behind using the text file is that you can edit the list without recompiling. It's rarely a good idea to use hard coded values like that unless you know they're never going to change.
Was This Post Helpful? 0
  • +
  • -

#37 Justin3085  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-December 09

Re: random result displayed in a textbox from array

Posted 29 July 2010 - 12:29 PM

Ok, i stepped through the program it hits Loadroads() under Form Load then jumps to LoadRoads()sub routine and then the text file. Hope i worded that correctly.
And I hope that is what i was suppose to do :)

Private Function GetRandomRoad() As String

        Dim index = rand.Next(0, roads.Count)

        Return roads(index)

    End Function



This doesn't pop up though. Could this be the issue?

This post has been edited by Justin3085: 29 July 2010 - 12:36 PM

Was This Post Helpful? 0
  • +
  • -

#38 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,902
  • Joined: 08-June 10

Re: random result displayed in a textbox from array

Posted 29 July 2010 - 01:21 PM

Did that block execute when you clicked the button? You can put a breakpoint there to find out.
Was This Post Helpful? 0
  • +
  • -

#39 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: random result displayed in a textbox from array

Posted 29 July 2010 - 01:23 PM

Yes, but as to why it's not popping up, you will need to show us all the code again that you have. The code demonstrated earlier functions as expected so there must be something missing in what you have.

Is the TextBox1.Text = GetRandomRoad() still in the button click?

OK One last thing, in your button_click event where you would have the TextBox1.Text = GetRandomRoad() ensure that you have a handles <btnName.Click> like so...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'get a random road
TextBox1.Text = GetRandomRoad()
End Sub

The Private Sub ... Handles Button1.Click is all one line

NOTE TO ADMINS: Left code block out to hilight the area in question.

This post has been edited by CharlieMay: 29 July 2010 - 01:34 PM

Was This Post Helpful? 0
  • +
  • -

#40 Justin3085  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-December 09

Re: random result displayed in a textbox from array

Posted 30 July 2010 - 05:03 AM

Ok here is the entire code...

Imports System.IO

Public Class Form1
    Private roads As New List(Of String)
    Private rand As New Random
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Do initial load of file
        Loadroads()

    End Sub
    Private Sub resultsTxtBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ssnTxtBox2.TextChanged
        'Displays a random number from txt file
    End Sub
    
    Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click

        
        caseTxtBox1.Text = caseEnterBox.Text
        datetxtBox3.Text = DateAndTime.Now
        resultsTxtBox2.Text = GetRandomroads()

    End Sub
    Private Sub Loadroads()
        
        Dim path = "C:\dev\roads.txt"

        Using sr As StreamReader = File.OpenText(path)

            While (Not sr.EndOfStream)
                Dim roads = sr.ReadLine()
                roads.Add(road)
                If Not roads.Trim() = String.Empty Then

                End If
            End While
        End Using
    End Sub
    Private Function GetRandomroads() As String

        Dim index = rand.Next(0, roads.Count)

        Return roads(index)

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        caseTxtBox1.Clear()
        caseEnterBox.Clear()
        datetxtBox3.Clear()
        oicTxtBox.Clear()
        

    End Sub
   

    Private Sub caseTxtBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles caseTxtBox1.TextChanged

    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click

    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#41 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: random result displayed in a textbox from array

Posted 30 July 2010 - 05:46 AM

Here's the only thing I found. It looks like you're confusing Roads With Road which is why I hate naming variables that closely.
You have:
   Private Sub Loadroads()
        Dim path = "C:\dev\roads.txt"
        Using sr As StreamReader = File.OpenText(path)
            While (Not sr.EndOfStream)
                Dim roads = sr.ReadLine() //This should be Dim road
                roads.Add(road) //this should move into the If statement and 
                If Not roads.Trim() = String.Empty Then //This should be road.Trim()
                End If
            End While
        End Using
    End Sub


Here is the corrected code:

    Private Sub Loadroads()
        Dim path = "c:\devs\roads.txt"
        Using sr As StreamReader = File.OpenText(path)
            While (Not sr.EndOfStream)
                Dim road = sr.ReadLine()
                If Not road.Trim() = String.Empty Then
                    roads.Add(road)
                End If
            End While
        End Using
    End Sub


Try this and see if it works for you.

This post has been edited by CharlieMay: 30 July 2010 - 05:47 AM

Was This Post Helpful? 0
  • +
  • -

#42 Justin3085  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-December 09

Re: random result displayed in a textbox from array

Posted 30 July 2010 - 06:14 AM

Still points to the argument out of range exception on "Return roads(index)"

May have to put this project on the back burner for a while lol. Could something be off with a setting in the program perhaps or maybe my txt file properties? :wacko:
Was This Post Helpful? 0
  • +
  • -

#43 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: random result displayed in a textbox from array

Posted 30 July 2010 - 07:17 AM

Create a new project, add 1 textbox and 1 button. Copy the code located here and paste it in the form code (replace everything). Ensure your roads.txt contains a few road names and run the program.
Was This Post Helpful? 1
  • +
  • -

#44 Justin3085  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-December 09

Re: random result displayed in a textbox from array

Posted 30 July 2010 - 07:30 AM

View PostCharlieMay, on 30 July 2010 - 06:17 AM, said:

Create a new project, add 1 textbox and 1 button. Copy the code located here and paste it in the form code (replace everything). Ensure your roads.txt contains a few road names and run the program.


Hmm, worked flawlessly. I had or at least I thought I had the exact code written in. Well, ill build the rest of my form around this since it worked now :) Im really going to look closely at the original and see what happened. Perhaps I ended up changing something in properties who knows. Not sure where the problem would of been. I guess sometimes starting fresh works wonders lol.

Thanks for all the help to everyone. I really appreciate !

This post has been edited by Justin3085: 30 July 2010 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

#45 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,902
  • Joined: 08-June 10

Re: random result displayed in a textbox from array

Posted 30 July 2010 - 07:52 AM

Probably just another small typo that's hard to notice. Oh well, as long as it works in the end.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4