Results 1 to 7 of 7

Thread: [RESOLVED] listbox problem

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2009
    Location
    ireland
    Posts
    36

    Resolved [RESOLVED] listbox problem

    hello,
    Hope someone can help with listbox problem.I have two forms.on form 1 i have button and textbox,form 2 there is a listbox.I have 100 random numbers in listbox.when i press button on form1 i want textbox to display a totally different number each time.I already done this in vb6 but i want to convert it to vb 2008.
    in vb6 i use
    < Code:
    1. Randomize
    2. Form1.Text1.Text = Form2.List1.List(Int(Rnd() * Form2.List1.ListCount))
    This generates a different number in textbox.

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: listbox problem

    Check out the Random class in the MSDN - that is what you need.

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2009
    Location
    ireland
    Posts
    36

    Re: listbox problem

    Thanks for replying.I am completely new to vb 2008 and am struggling with this conversion.so if you have any more suggestions it would be great.thanks

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: listbox problem

    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            'in form load add - ListBox1.Items.Clear()
            addNums()
            Dim i As Integer = rand.Next(0, ListBox1.Items.Count)
            TextBox1.Text = ListBox1.Items(i).ToString
            ListBox1.Items.RemoveAt(i)
        End Sub
        Private Sub addNums()
            If ListBox1.Items.Count > 0 Then Exit Sub
            ListBox1.Items.Clear()
            For x = 1 To 10
                ListBox1.Items.Add(rand.Next(1, 1001))
            Next
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2009
    Location
    ireland
    Posts
    36

    Re: listbox problem

    Thanks for the reply.As i siad i am new to vb 2008.if i already have the listbox populated with numbers.(copy and pasted from excel),I just need to be able to display a completely random number in textbox from the numbers in my listbox already.just like in vb6.I was just hoping for something to match the code i had in vb6.Thanks again.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: listbox problem

    Code:
        Dim rndm As New Random
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If ListBox1.Items.Count > 0 Then
                Dim i As Integer = rndm.Next(0, ListBox1.Items.Count)
                TextBox1.Text = ListBox1.Items(i).ToString
                ListBox1.Items.RemoveAt(i)
            End If
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2009
    Location
    ireland
    Posts
    36

    Re: [RESOLVED] listbox problem

    Absolutely perfect thanks very much dbasnett.
    . Code:
    1. If Form2.ListBox1.Items.Count > 0 Then
    2.             Dim i As Integer = rndm.Next(0, Form2.ListBox1.Items.Count)
    3.             Me.TextBox1.Text = Form2.ListBox1.Items(i).ToString
    4.             Form2.ListBox1.Items.RemoveAt(i)
    5.         End If

Posting Permissions

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



Click Here to Expand Forum to Full Width