Results 1 to 6 of 6

Thread: Grab 10 random words from text file

  1. #1
    Jali is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.0
    Join Date
    Mar 2010
    Posts
    5
    Reputation
    0

    Grab 10 random words from text file

    I have a text document. i want to click a button and 10 random words out of the 196000 words shown in a textbox or listbox.

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,587
    Reputation
    2737
    Your profile is indeterminate so I will post a VB 2008 solution
    Code:
    Private rnd As New Random
    
    Private Sub Take10Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Take10Button.Click
        Dim words = IO.File.ReadAllText("document.txt").Split(" "c)
        Dim random10 = words.OrderBy(Function() rnd.Next).Take(10)
        Me.ResultTextBox.Lines = random10.ToArray
    End Sub

  3. #3
    Jali is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.0
    Join Date
    Mar 2010
    Posts
    5
    Reputation
    0
    Let say i have a button with the name and text button1. and i have a listbox with its name listbox1 and under listbox properties the scrollalwaysvisible i change it to true. how do i open a txt file? this is step 1.. and then i will add another button with its name and text button2 and another listbox with its name listbox2 and when i click the button it will randomly pick 10 words from the textfile. Can you teach me slowly by stating if i need an openfiledialog and everything

  4. #4
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,587
    Reputation
    2737
    If you want user to select the file it could be a good idea to use the OpenFileDialog Class (System.Windows.Forms), yes. You can then get the selection from FileName property.

  5. #5
    Jali is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.0
    Join Date
    Mar 2010
    Posts
    5
    Reputation
    0
    actually i can open the text file to my listbox1 already. but now i want to click a button and generate 10 random words from listbox1 to listbox2. can i know the coding at button 2 for this pleaseeeeeee

  6. #6
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    12,987
    Reputation
    1626
    Quote Originally Posted by Jali View Post
    actually i can open the text file to my listbox1 already. but now i want to click a button and generate 10 random words from listbox1 to listbox2. can i know the coding at button 2 for this pleaseeeeeee
    JohnH has already shown you how. Getting 10 random items from a ListBox is no different to getting 10 random words from a file. You simply order the list randomly and take the first 10 items.

Bookmarks

Posting Permissions

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