10 Replies - 389 Views - Last Post: 08 February 2018 - 02:07 PM Rate Topic: -----

#1 Mallard8  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 05-February 18

Randomise

Posted 07 February 2018 - 10:16 AM

Hi,
I'm just getting back in to VB after downloading VB Community 17.
I want to place names in a Listbox (I think?) and then have one picked at random.
Before I go off and get completely confused is there an article that I could read to at least point me in the right direction?

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Randomise

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13755
  • View blog
  • Posts: 54,928
  • Joined: 12-June 08

Re: Randomise

Posted 07 February 2018 - 10:20 AM

It's more of knowing what you are dealing with. The listbox has the GUI elements but also 'items' collection.
Collections start indexing at 0 and go to their length - 1... with that pair of information nuggets you can use the 'random' to get a value between them and use that as an index.

https://msdn.microso...(v=vs.110).aspx
https://msdn.microso...(v=vs.110).aspx
Was This Post Helpful? 1
  • +
  • -

#3 Mallard8  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 05-February 18

Re: Randomise

Posted 07 February 2018 - 10:39 AM

Thanks for the reply and the two links will go away and give it a go.
Though I'm sure I'll be back in a couple of days with a question or two!
Was This Post Helpful? 0
  • +
  • -

#4 Mallard8  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 05-February 18

Re: Randomise

Posted 08 February 2018 - 10:31 AM

View Postmodi123_1, on 07 February 2018 - 10:20 AM, said:

It's more of knowing what you are dealing with. The listbox has the GUI elements but also 'items' collection.
Collections start indexing at 0 and go to their length - 1... with that pair of information nuggets you can use the 'random' to get a value between them and use that as an index.

https://msdn.microso...(v=vs.110).aspx
https://msdn.microso...(v=vs.110).aspx


OK as foretold here I am again, I got it to work, press the button and a name from the listbox is selected at random and placed in a label. What I want next is to highlight each name top to bottom and back again, to give the illusion that the computer is considering which name to pick then after a set time it shows the name picked on the label and also highlights the name in the Listbox.
Below is the code so far. What happens is the name appears in the label but the highlighted name is always the last one. Do I need a delay set up so that we see each Listbox item highlighted.
    Dim RandomName As New Random
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim i As Integer = LstNames.Items.Count
        Dim chosenItem As System.Object = LstNames.Items.Item(RandomName.Next(i))
        Dim x As Integer
        For x = 0 To LstNames.Items.Count - 1
            LstNames.SetSelected(x, True)
        Next x
        LstNames.SetSelected(i - 1, True)
        Lblwinner.Text = chosenItem.ToString
    End Sub

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13755
  • View blog
  • Posts: 54,928
  • Joined: 12-June 08

Re: Randomise

Posted 08 February 2018 - 10:34 AM

Look at what you are doing with these two lines.
03    Dim i As Integer = LstNames.Items.Count

09    LstNames.SetSelected(i - 1, True)



'i' never changes to be anything but the last value.
Was This Post Helpful? 1
  • +
  • -

#6 Mallard8  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 05-February 18

Re: Randomise

Posted 08 February 2018 - 10:44 AM

So Dim i is getting the number of items in the Listbox which means that i is always the total number of items in list.
I did try
LstNames.SetSelected(chosenItem - 1, True)

As I think chosenItem is the item picked at random but I get an error message when I try that.
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13755
  • View blog
  • Posts: 54,928
  • Joined: 12-June 08

Re: Randomise

Posted 08 February 2018 - 10:48 AM

Perhaps investigate that for loop more and maybe throw a 'sleep' with it to pause it long enough so folk see a change.

https://msdn.microso...(v=vs.110).aspx
Was This Post Helpful? 1
  • +
  • -

#8 Mallard8  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 05-February 18

Re: Randomise

Posted 08 February 2018 - 11:19 AM

Well that looks better now the selection moves down one at a time and can be seen.
Still can't figure how to show random selection in Listbox and I've tried a few options.
So it's really hard to do or I'm missing something really simple (probably the latter)
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13755
  • View blog
  • Posts: 54,928
  • Joined: 12-June 08

Re: Randomise

Posted 08 February 2018 - 12:16 PM

Let's see your current code.
Was This Post Helpful? 0
  • +
  • -

#10 Mallard8  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 05-February 18

Re: Randomise

Posted 08 February 2018 - 01:11 PM

Imports System.Threading
Public Class Form1

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles Txtentername.KeyDown
        If e.KeyCode = Keys.Enter Then ' Press the Enter key to place name in Listbox
            LstNames.Items.Add(Txtentername.Text)
            Txtentername.Text = String.Empty 'Clear Textbox ready for another name to be entered
        End If
    End Sub
    Dim RandomName As New Random
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim i As Integer = LstNames.Items.Count
        Dim chosenItem As System.Object = LstNames.Items.Item(RandomName.Next(i)) 'Pick a random name from listbox
        Dim x As Integer

        For x = 0 To LstNames.Items.Count - 1 'Loop round so highlighted selection moves down each name
            LstNames.SetSelected(x, True)
            Thread.Sleep(250)
        Next x

        LstNames.SetSelected(i - 1, True)
        Lblwinner.Text = chosenItem.ToString 'Place the selected name in Label.text
    End Sub

End Class


Was This Post Helpful? 0
  • +
  • -

#11 Mallard8  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 05-February 18

Re: Randomise

Posted 08 February 2018 - 02:07 PM

Finally figured it after visiting MS.
Imports System.Threading
Public Class Form1

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles Txtentername.KeyDown
        If e.KeyCode = Keys.Enter Then ' Press the Enter key to place name in Listbox
            LstNames.Items.Add(Txtentername.Text)
            Txtentername.Text = String.Empty 'Clear Textbox ready for another name to be entered
        End If
    End Sub
    Dim RandomName As New Random
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim i As Integer = LstNames.Items.Count
        Dim chosenItem As System.Object = LstNames.Items.Item(RandomName.Next(i)) 'Pick a random name from listbox
        Dim x As Integer

        For x = 0 To LstNames.Items.Count - 1 'Loop round so highlighted selection moves down each name
            LstNames.SetSelected(x, True)
            Thread.Sleep(250)
        Next x

        Dim index As Integer = LstNames.FindString(chosenItem)'Get the number of selected item
        LstNames.SetSelected(index, True)'Show selected item in Listbox
        Lblwinner.Text = chosenItem.ToString 'Place the selected name in Label.text
    End Sub

End Class



Was This Post Helpful? 1
  • +
  • -

Page 1 of 1