PDA

Click to See Complete Forum and Search --> : Randomizing


n1s
Nov 14th, 1999, 10:59 AM
I am having trouble randomizing message boxes the code i am currently using does not work so well. can someone give me one that they know with work for vb 3?

i have a list of message boxes to play and i want it to randomly play 1 of them.

can someone help
thanx

Bob Baddeley
Nov 14th, 1999, 11:05 AM
could you assign each of the boxes a number and randomize that number and display the message box that corresponds?

MartinLiss
Nov 14th, 1999, 09:37 PM
What do you mean by "I have a list of message boxes to play...." Does that mean that you have one message box and you want it to show one of several messages? If it is something else, please let me know.

------------------
Marty

Yonatan
Nov 14th, 1999, 10:20 PM
I think I know what you mean... Try this: (I THINK it works in VB3)

Option Explicit


Private Type MsgBoxTemplate
Prompt As Variant
Buttons As VbMsgBoxStyle ' = VBOkOnly
Title As Variant
HelpFile As Variant
Context As Variant
End Type


Dim m_mbTemplates() As MsgBoxTemplate


Function InBounds(ByVal nIndex As Long) As Boolean
InBounds = (nIndex >= LBound(m_mbTemplates) And nIndex <= UBound(m_mbTemplates))
End Function


Sub SetMsgBoxTemplate(ByVal nIndex As Long, ByVal Prompt, Optional ByVal Buttons As VbMsgBoxStyle = vbOKOnly, Optional ByVal Title, Optional ByVal HelpFile, Optional ByVal Context)
If nIndex = UBound(m_mbTemplates) + 1 Then ReDim Preserve m_mbTemplates(1 To nIndex)
If Not InBounds(nIndex) Then Call Err.Raise(Number:=380, Description:="Invalid Template Index.")
With m_mbTemplates(nIndex)
.Prompt = Prompt
.Buttons = Buttons
.Title = Title
.HelpFile = HelpFile
.Context = Context
End With
End Sub


Function ShowMsgBoxFromTemplate(ByVal nIndex As Long) As VbMsgBoxResult
If Not InBounds(nIndex) Then Call Err.Raise(Number:=380, Description:="Invalid Template Index.")
ShowMsgBoxFromTemplate = MsgBox(m_mbTemplates(nIndex).Prompt, m_mbTemplates(nIndex).Buttons, m_mbTemplates(nIndex).Title, m_mbTemplates(nIndex).HelpFile, m_mbTemplates(nIndex).Context)
End Function


Function RandomShowMsgBoxFromTemplate() As VbMsgBoxResult
Call Randomize(Timer)
RandomShowMsgBoxFromTemplate = ShowMsgBoxFromTemplate(Rnd * (UBound(m_mbTemplates) - 1) + LBound(m_mbTemplates))
End Function


Private Sub Form_Load()
Dim I As Long
AutoRedraw = True
' This must be done before using SetMsgBoxTemplate:
ReDim m_mbTemplates(1 To 1)
' Example of how to use the MsgBox templates:
Call SetMsgBoxTemplate(1, "Prompt #1", vbExclamation, "Title #1")
Call SetMsgBoxTemplate(2, "Prompt #2", vbQuestion Or vbYesNo, "Title #2")
Call SetMsgBoxTemplate(3, "Prompt #3", vbCritical Or vbAbortRetryIgnore, "Title #3")
Call SetMsgBoxTemplate(4, "Prompt #4", vbSystemModal Or vbYesNoCancel, "Title #4")
Call SetMsgBoxTemplate(5, "Prompt #5") ' MsgBox #5 is blank except prompt
' Display the MsgBoxes:
For I = 1 To 5
Print "MsgBox #" & I & " result: " & ShowMsgBoxFromTemplate(I)
Next
' Display a random one:
Print "Random MsgBox result: " & RandomShowMsgBoxFromTemplate


' The result is a number. This is what each number means:
' 1 = OK
' 2 = Cancel
' 3 = Abort
' 4 = Retry
' 5 = Ignore
' 6 = Yes
' 7 = No
End Sub


------------------
Yonatan
Teenage Programmer
E-Mail: RZvika@netvision.net.il
ICQ: 19552879 (http://www.icq.com/19552879)
AIM: RYoni69