Re: MsgBox default button
- From: "MikeD" <nobody@xxxxxxxxxxx>
- Date: Sat, 13 Aug 2005 16:15:53 -0400
"Timo" <timppa_04@xxxxxxxxxxx> wrote in message
> I do have a bad habit to write "vbYesNo + vbQuestion" but it has
> always worked. I tried quickly reason what difference does it make
> to OR or add those flags. I suppose VB threats them as long anyway?
> Under what conditions adding flags wouldn't work?
> Anyway, I'll take your word and start to use ORing.
Essentially, it's just safer to OR them because you eliminate the risk of
adding the same bitwise value twice. It's just a bad habit that too many
programmers have to add bitwise flags. If you accidently add a bitwise
value twice, you won't get the expected results.
Let's say that for whatever reason, you wrap MsgBox in your own function
(there's probably no practicle reason to do this, I'm just using it for an
example), in which you can pass any of its flags. But in your wrapper
function, you want the question icon only to be used. So, your function
header might look like this:
Public Function MsgBoxWrapper(sPrompt As String, lFlags As VbMsgBoxStyle) As
MsgBoxWrapper = MsgBox(sPrompt, lFlags + vbQuestion)
Now, call this with the following code:
lRet = MsgBoxWrapper("Continue?", vbYesNo + vbQuestion)
Notice that you don't get the question icon, but the information icon. Now,
change the call to this:
lRet = MsgBoxWrapper("Continue?", vbYesNo Or vbQuestion)
Note that you STILL get the information icon. Now, change the line of code
in MsgBoxWrapper to use Or instead of +. You get the question icon as you
want, even though you ORed it twice. Note that for this example to really
be complete, you should remove the flags for the critical, exclamation, and
information icons if any of them happened to get passed to the wrapper
function. If you tried to subtract these values, you'd have a real mess.
Here's what the function should be to ensure, as best as possible, that only
the question icon is displayed:
Public Function MsgBoxWrapper(sPrompt As String, ByVal lFlags As
VbMsgBoxStyle) As VbMsgBoxResult
lFlags = lFlags And Not (vbCritical Or vbInformation Or vbExclamation)
MsgBoxWrapper = MsgBox(sPrompt, lFlags Or vbQuestion)
Even so, you (or someone) could still butcher the call like this:
lRet = MsgBoxWrapper("Continue?", vbYesNo + vbCritical + vbExclamation +
You're still not going to get the question icon. However, if you ORed them
you would, despite the fact that all those other icon flags were specified.
As I said, there's probably no practical reason for wrapping MsgBox like
this. I just wanted to use something familiar to show what problems you can
run into when you add bitwise values instead of ORing them. In the case of
MsgBox, it's probably extremely rare that you'd run into problems adding
them because that's just not a mistake that's likely to be made (albeit,
it's still entirely possible).
The point I'm trying to make is that anytime you're dealing with bitwise
values, they should *always* be combined using Or (and likewise, removed
using And Not). It's just the correct way to do it.
Microsoft MVP Visual Basic
- Re: MsgBox default button
- From: Timo
- Re: MsgBox default button
- Prev by Date: Re: MsgBox default button
- Next by Date: Tab control
- Previous by thread: Re: MsgBox default button
- Next by thread: Re: MsgBox default button