|
-
Jan 3rd, 2010, 04:22 PM
#1
Thread Starter
Member
[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:
Randomize
Form1.Text1.Text = Form2.List1.List(Int(Rnd() * Form2.List1.ListCount))
This generates a different number in textbox.
-
Jan 3rd, 2010, 04:37 PM
#2
Re: listbox problem
Check out the Random class in the MSDN - that is what you need.
-
Jan 3rd, 2010, 05:45 PM
#3
Thread Starter
Member
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
-
Jan 3rd, 2010, 06:34 PM
#4
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
-
Jan 4th, 2010, 04:31 AM
#5
Thread Starter
Member
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.
-
Jan 4th, 2010, 06:35 AM
#6
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
-
Jan 4th, 2010, 10:18 AM
#7
Thread Starter
Member
Re: [RESOLVED] listbox problem
Absolutely perfect thanks very much dbasnett.
. Code:
If Form2.ListBox1.Items.Count > 0 Then
Dim i As Integer = rndm.Next(0, Form2.ListBox1.Items.Count)
Me.TextBox1.Text = Form2.ListBox1.Items(i).ToString
Form2.ListBox1.Items.RemoveAt(i)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|