Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Display random tips from text file

Posted on 2007-08-02
3
Medium Priority
?
154 Views
Last Modified: 2013-12-26
Hi,

  I have a text file full of one line tips that I'd like to randomly display on screen. Does someone have some nice code that will display random lines from that file?

Thanks.
0
Comment
Question by:tvtech
  • 2
3 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 19616099
Is it for vb6 or vb.net ?


There are numerous ways in which this could be achieved - it rather depends upon how you are storing the data once you have read it from the file.
The following bit of code may give you some ideas.  It simply reads each line from a text file, adding a four digit random number string to the front of each line, before adding the line to a list.  The list is then sorted, which randomises the original order, and the four digit number is removed.
Code Snippet
Dim InputData As New List(Of String)
Dim Rnd As New Random
Dim SR As New IO.StreamReader("d:\temp\test.txt")
Do While Not SR.EndOfStream
   InputData.Add(Rnd.Next(0, 10000).ToString("0000") & SR.ReadLine)
Loop
SR.Close()
InputData.Sort()
For count As Integer = 0 To InputData.Count - 1
   InputData(count) = InputData(count).Substring(4)
Next

0
 
LVL 53

Accepted Solution

by:
Dhaest earned 1000 total points
ID: 19616107
Example for vb6

Dim A() As String, I As Long
    I = -1
    Open "Your txt path" For Input As #1
    While Not EOF(1)
        I = I + 1
        ReDim Preserve A(I)
        Line Input #1, A(I)
    Wend
    Close
   
    Randomize (Timer)
    MsgBox A(Int((UBound(A) * Rnd) + 1))
0
 

Author Comment

by:tvtech
ID: 19616166
Djaest, just perfect! Thanks for a quick and accurate reply.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question