8 Replies - 3739 Views - Last Post: 01 February 2010 - 02:50 PM Rate Topic: -----

#1 richard356  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 03-August 08

random lines fom text file into richtextbox

Posted 31 January 2010 - 09:47 AM

basically ive tried a few different methods which i have come accross by searching but none seem to work.

code so far after removing everything that didnt work is as follows
Public Class Form1


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

End Class


im makng a spelling test type application so that each word is on a different line and that are displayed in a random sequency.
any help?? thanks :^: :^: could just be me being dumb :P

This post has been edited by richard356: 31 January 2010 - 09:58 AM


Is This A Good Question/Topic? 0
  • +

Replies To: random lines fom text file into richtextbox

#2 LeisureProgrammer  Icon User is offline

  • N00by VB.NET Developer
  • member icon

Reputation: 110
  • View blog
  • Posts: 588
  • Joined: 06-September 09

Re: random lines fom text file into richtextbox

Posted 31 January 2010 - 10:16 AM

 Dim aaa As New OpenFileDialog 'A thing to allow the user to choose a file
		aaa.ShowDialog() 'show that thing

		Dim bbb As String() = File.ReadAllLines(aaa.FileName) 'Fill a string array with lines from that file
		Dim ccc As Integer = Rnd() * bbb.Length - 1 'Fill an integer with a random number from 0 to number of lines in that file
		TextBox1.Text &= vbNewLine & bbb(ccc) 'Display a random line


Feel free to ask if you're confused about anything or if you want to do more stuff which you're not sure about.
Was This Post Helpful? 1

#3 richard356  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 03-August 08

Re: random lines fom text file into richtextbox

Posted 31 January 2010 - 10:27 AM

2 things;

1. line
[code]Dim bbb As String() = file.readAllLines(aaa.FileName) 'Fill a string array with lines from that file[code]
throows error of 'name 'file' is not declared'

2. how would i do this by using a text file in the project resources
Was This Post Helpful? 0
  • +
  • -

#4 LeisureProgrammer  Icon User is offline

  • N00by VB.NET Developer
  • member icon

Reputation: 110
  • View blog
  • Posts: 588
  • Joined: 06-September 09

Re: random lines fom text file into richtextbox

Posted 31 January 2010 - 10:33 AM

1. I forgot - you need to import the "System.IO" namespace
2. You can use something like " File.ReadAllLines(My.Resources.theNameOfYourFile)"
Was This Post Helpful? 1
  • +
  • -

#5 richard356  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 03-August 08

Re: random lines fom text file into richtextbox

Posted 31 January 2010 - 11:03 AM

View PostLeisureProgrammer, on 31 Jan, 2010 - 09:33 AM, said:

1. I forgot - you need to import the "System.IO" namespace
2. You can use something like " File.ReadAllLines(My.Resources.theNameOfYourFile)"

im slowly getting there :P :^:

now getting 'illegal characters in path.' exception on
File.ReadAllLines(My.Resources.TextFile1.)

This post has been edited by richard356: 31 January 2010 - 11:24 AM

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1632
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: random lines fom text file into richtextbox

Posted 31 January 2010 - 11:05 AM

Tip, before using ReadAllLines please read this :)
Was This Post Helpful? 1
  • +
  • -

#7 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 84
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: random lines fom text file into richtextbox

Posted 31 January 2010 - 12:51 PM

View Postrichard356, on 31 Jan, 2010 - 10:03 AM, said:

View PostLeisureProgrammer, on 31 Jan, 2010 - 09:33 AM, said:

1. I forgot - you need to import the "System.IO" namespace
2. You can use something like " File.ReadAllLines(My.Resources.theNameOfYourFile)"

im slowly getting there :P :^:

now getting 'illegal characters in path.' exception on
File.ReadAllLines(My.Resources.TextFile1.)


Here is another way
	
	Public Function GetRandomLine(ByVal filename As String) As String
		Dim randomtext As String = String.Empty
		Dim ran As New Random()
		Dim mx As Integer = 0
		Dim cnt As Integer = 0
		' first reading to count lines only
		Using sr As New StreamReader(filename)
			While sr.Peek() >= 0
				sr.ReadLine()
				mx += 1
			End While
		End Using

		Dim num As Integer = (ran.[Next](1, mx - 1))
		' second reading
		Using sr As New StreamReader(filename)
			While Not sr.EndOfStream
				cnt += 1
				Dim line As String = sr.ReadLine()
				If cnt = num Then
					randomtext = line
					Exit While

				End If
			End While

		End Using
		Return randomtext
	End Function


Use something like this:
Dim s As String = GetRandomLine(Path.GetFullPath("TextFile1.txt"))


~'J'~
Was This Post Helpful? 0
  • +
  • -

#8 Cookiesliyr  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 136
  • Joined: 16-May 09

Re: random lines fom text file into richtextbox

Posted 01 February 2010 - 02:37 PM

u can simply load all the lines in an array then choose random ones if u asks me
Was This Post Helpful? 0
  • +
  • -

#9 LeisureProgrammer  Icon User is offline

  • N00by VB.NET Developer
  • member icon

Reputation: 110
  • View blog
  • Posts: 588
  • Joined: 06-September 09

Re: random lines fom text file into richtextbox

Posted 01 February 2010 - 02:50 PM

Quote

u can simply load all the lines in an array then choose random ones if u asks me

That's what my code was doing

Quote

Tip, before using ReadAllLines please read this
StreamReader looks confusing to me LOL, and I'm pretty sure the OP won't mind slightly inefficienter code.

But fixo figured it out, so use his code OP LOL

Quote

My.Resources.TextFile1.

Are you sure that's the path to your text file? Seems odd...

This post has been edited by LeisureProgrammer: 01 February 2010 - 03:04 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1