Results 1 to 7 of 7

Thread: How can you msgbox a random entry in a listbox?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2002
    Posts
    628

    How can you msgbox a random entry in a listbox?

    and add a 4 digit random number to it?

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    Yes

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Msgbox random entry in listbox:
    VB Code:
    1. Dim iRand As Integer
    2.  
    3.   Randomize
    4.   iRand = rnd * (List1.ListCount - 1)
    5.  
    6.   MsgBox List1.List(iRand)

    Add a random 4-didget number:
    VB Code:
    1. Dim sRand As String
    2.  
    3.   Randomize
    4.   For i = 1 to 4
    5.     sRand = sRand & str(int(rnd * 9) + 1)
    6.   Next

    Or something like that.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    Dont suppose you wanna give more details


    Theres a command called Rand(), will do what you want, alongside a command called MsgBox.

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Example:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Dim iRand As Integer
    5.  
    6.   Randomize
    7.   iRand = Rnd * (List1.ListCount - 1)
    8.  
    9.   'select the random item:
    10.   List1.Selected(iRand) = True
    11.  
    12.   MsgBox List1.List(iRand)
    13. End Sub
    14.  
    15. Private Sub Form_Load()
    16. Dim sRand As String, i As Integer, j As Integer
    17.  
    18.   Randomize
    19.   For i = 1 To 22
    20.     For j = 1 To 4
    21.       sRand = sRand & Trim(Str(Int(Rnd * 9) + 1))
    22.     Next
    23.     List1.AddItem sRand
    24.     sRand = ""
    25.   Next
    26. End Sub
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    DerFarm
    Guest
    Code:
    Function ShowList_RndNum(LstBox as ListBox) as string
       dim NumItems as integer,ThisItem as integer
       dim ThatNum as integer,Ret_Val as string
    
    
          NumItems=LstBox.ListCount-1
          ThisItem = int(Rnd()*NumItems)
          ThatNum = int(Rnd()*9999)
    
          Ret_Val = right$("0000"+ltrim$(str$(ThatNum)),4)
          Ret_Val = LstBox.List(ThisItem) & " " & Ret_Val
    
    Xit_ShowList_RndNum:
          ShowList_RndNum=Ret_Val
    End Function
    Is this what you wanted?????

    ShowList_RndNum will return a string comprised of the randomly
    selected list item and a randomly selected number between 0000-
    9999.

  7. #7
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    My evil laugh has a squeak in it.

    kristopherwilson.com

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