|
-
Nov 22nd, 2011, 11:53 PM
#1
Thread Starter
PowerPoster
[RESOLVED] Remove any random number from a listbox?
Hi there, I have a program that gives students math questions, but I want to add a feature so if the teacher only wants to give the student a certain number then it will remove the rest. The program has a max of 20 questions in a listbox called QList and all items added are numbered 1-20
so it is like Qlist.additem "1"....etc to 20
But what I would like to do is be able to randomly remove a certain number of numbers from that listbox.
For example, all questions are loaded, but say a teacher wants only 10 questions. I want the program to randomly remove 10 items from the list.
I know you can go Qlist.removeitem (index number of item), but I either need to be able to randomize the listbox items from the start so they are out of order and then remove index 0-9, or randomly pick 10 items to get rid of.
All help is duly appreciated!
-
Nov 23rd, 2011, 12:09 AM
#2
Re: Remove any random number from a listbox?
Here's an idea
Code:
Private Sub RemoveRandom(lst As ListBox, intNumber As Integer)
'
' Remove intNumber of items randomly from lst
'
Dim intRnd As Integer
Dim intI As Integer
'
' Check that the number of questions to remove
' is less than the total number of questions
' in the listbox
'
If intNumber < lst.ListCount Then
'
' Create a Random number between 0 and the number of items -1
' and remove that item
' (Rnd Function creates a Random Number such that 0 <= Rnd < 1)
'
For intI = 1 To intNumber
intRnd = Int(Rnd * lst.ListCount)
lst.RemoveItem intRnd
Next intI
End If
End Sub
You need to put
into the Form Load event to seed the Random Number Generator
To use
Code:
Call RemoveRandom(Qlist, number_of_items_to_remove)
eg Call RemoveRandom(Qlist, 10) will remove 10 items randomly
Last edited by Doogle; Nov 23rd, 2011 at 12:14 AM.
-
Nov 23rd, 2011, 07:15 AM
#3
Thread Starter
PowerPoster
Re: Remove any random number from a listbox?
Great work, thanks Doogle.
-
Nov 23rd, 2011, 07:37 AM
#4
Re: [RESOLVED] Remove any random number from a listbox?
Just as an afterthought, you may wish to save the complete list before removing the items so that it can be 'recovered' in the event that the Teacher types in, say '10' rather than '1' by mistake.
(Just thinking I may pre-empt your next question  )
That can be done several ways, the easiest might be to make a copy the first time the option is used.
In your Form's Declarations Section:
Code:
Private sQCopy() As String
Private boCopy As Boolean
Subroutine RemoveRandom now becomes:
Code:
Private Sub RemoveRandom(lst As ListBox, intNumber As Integer)
'
' Remove intNumber of items randomly from lst
'
Dim intRnd As Integer
Dim intI As Integer
'
' Check that the number of questions to remove
' is less than the total number of questions
' in the listbox
'
If intNumber < lst.ListCount Then
'
' If a copy of the original contents hasn't been made
' then make one
'
If Not boCopy Then
ReDim sQCopy(lst.ListCount - 1)
For intI = 0 To lst.ListCount - 1
sQCopy(intI) = lst.List(intI)
Next intI
boCopy = True
End If
'
' Create a Random number between 0 and the number of items -1
' and remove that item
' (Rnd Function creates a Random Number such that 0 >= Rnd < 1)
'
For intI = 1 To intNumber
intRnd = Int(Rnd * lst.ListCount)
lst.RemoveItem intRnd
Next intI
End If
End Sub
and you can add another CommandButton, eg cmdRestoreQList, to your Form to restore from the copy
Code:
Private Sub cmdRestoreQList_Click()
'
' Restore the original contents of QList from the copy made previously
' (If a copy has been made)
'
Dim intI As Integer
If boCopy Then
QList.Clear
For intI = 0 To UBound(sQCopy)
QList.AddItem sQCopy(intI)
Next intI
End If
End Sub
Last edited by Doogle; Nov 23rd, 2011 at 07:41 AM.
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
|