MsgBox and flags - please about explanation
Hi All
My prob it's like this.
I get to know, that I should to write for procedure this message box instead of sign '+' it a logic expression - 'Or'.
e.g. instead
VB Code:
MsgBox "Item " & Text1.Text & " was not found!", vbOKOnly + vbInformation, "Search"
should be:
VB Code:
MsgBox "Item " & Text1.Text & " was not found!", vbOKOnly [B]Or[/B] vbInformation, "Search"
I was explained that this depends from quantity of flags
I am totally confused now because I don't know what are the flags. How I can to recognize them? Where I can read about flags for Message Box - on a MSDN I can't found it
thanks in advance
Re: MsgBox and flags - please about explanation
These flags are binary values - so they should be added together with the OR operator. + does the same thing but is considered inappropriate.
Here's an explanation - and to understand it you must consider what these values look like in binary.
0001 is the decimal value 1
0010 is the decimal value 2
1 + 2 = 3 (this is standard math)
1 OR 2 = 3 (this is done with boolean logic)
0001 (1)
0010 (2) - now let's OR these values
----
0011 (3) - the result of OR'ing the two values together
OR follows the rule that if either bit is true the results is true...
1 + 3 = 4 (this is standard math)
1 or 3 = 3 (see below for why)
0001 (1)
0011 (3)
----
0011 (3) - the result of OR'ing the decimal values 1 and 3 together
See how the "1" bit in an OR can still only produce a "1" in the binary 1 column?
Re: MsgBox and flags - please about explanation
You can see all the flags that you can use right clicking one of them and selecting Definition. What each Flag does? it's pretty self explained in the Flag's name, the only thing you need to do is adding the flags you want using + or OR
Example, you say..
Oh I want a message box asking something, the answer should be yes or no, i also want a question image, default button should be NO, the Messagebox name should be MyMessageBox.
Then you need this
VB Code:
If MsgBox("Yes or No?", vbYesNo + vbQuestion + vbDefaultButton2, "MyMessageBox") = vbYes Then
'Yes
Else
'No
End If
Re: MsgBox and flags - please about explanation
Thanks, very thanks Szlamany. It a good deal to explain.
But still I don't know how I can to recognize them?
I want ask you.
Are relationships between the flags and values assigns to a message box.
You have mean about these five groups of values? You know.
first group determines which buttons to display
second group determines which icon to display
trd group determines which button is the default
forth group determines the modality of the message box
fifth group it the constants that can be used for the buttons argument which would only be used under special circumstances
Yet is a sixth group of value which determines which button the user clicked
And this can it's something of different completely
thanks Tamgovb
Re: MsgBox and flags - please about explanation
You are referring to this:
Code:
vbOKOnly 0 Display OK button only.
vbOKCancel 1 Display OK and Cancel buttons.
vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons.
vbYesNoCancel 3 Display Yes, No, and Cancel buttons.
vbYesNo 4 Display Yes and No buttons.
vbRetryCancel 5 Display Retry and Cancel buttons.
vbCritical 16 Display Critical Message icon.
vbQuestion 32 Display Warning Query icon.
vbExclamation 48 Display Warning Message icon.
vbInformation 64 Display Information Message icon.
vbDefaultButton1 0 First button is the default.
vbDefaultButton2 256 Second button is the default.
vbDefaultButton3 512 Third button is the default.
vbDefaultButton4 768 Fourth button is the default.
vbApplicationModal 0 Application modal. The user must respond to the message box
before continuing work in the current application.
vbSystemModal 4096 System modal.
vbMsgBoxHelpButton 16384 Adds Help button to the message box
VbMsgBoxSetForeground 65536 Specifies the message box window as the foreground window
vbMsgBoxRight 524288 Text is right aligned
vbMsgBoxRtlReading 1048576 Specifies text should appear as right-to-left reading...
The first group of values (0–5) describes the number and type of buttons displayed
in the dialog box;
the second group (16, 32, 48, 64) describes the icon style;
the third group (0, 256, 512) determines which button is the default;
and the fourth group (0, 4096) determines the modality of the message box.
When adding numbers to create a final value for the buttons argument
, use only one number from each group.
I don't see a 5th group mentioned here... The constants shown in the help message that followed this indicate what key/button was clicked by the user...
At any rate - these "groups" are simply parts of the "bit pattern" that controls behavior.
So if you look at what they are calling the "first" group you will see that they are getting a lot of information passed to the function by simply using 3 bits.
Code:
Bit [0] Value 1
Bit [1] Value 2
Bit [2] Value 4
421
---
000 - decimal 0 - vkOKOnly
001 - decimal 1 - vbOKCancel
010 - decimal 2 - vbAbortRetryIgnore
011 - decimal 3 - vbYesNoCancel
100 - decimal 4 - vbYesNo
101 - decimal 5 - vbRetryCancel
110 - decimal 6 - not used
111 - decimal 7 - not used
The code in the MSGBOX function can simply check these bits and knows exactly what button combinations to show. Checking a bit is done like this:
Code:
If (lngBitPattern and 1) <> 0 Then
' bit 0 (value 1) is set - let's do something
Else
' bit 0 (value 1) is not set - let's do something else...
End If
VB is trying to be easy-to-use by suggesting that you can "add" these values together - but of course adding bits with + operator can cause "carry over" values into other columns - OR is always preferred. To let novice programmers get around having to grasp this fact the help text goes on to say that only one value should be "added" from each group.
Are you asking for explanations of this specific to MSGBOX or are you trying to understand the whole bit pattern concept??
Re: MsgBox and flags - please about explanation
Not sure if you guys noticed but the Or does work for adding the values but you dont get the intellisense with it to produce the menu of the next possible flag to add. When you use the "+" sign you do get the intellisense. I have been using the "+" because of this for many years as its easier to pick from a list then type it all out (yes, I do know almost all the arguments by heart lol).
Also, there is only 4 groups of flag possibilities you can add or Or together.
Button captions
Icon type
Default button
Modality level
VB Code:
MsgBox "Message", vbYesNo + vbInformation + vbDefaultButton2 + vbSystemModal, "Title"
1 Attachment(s)
Re: MsgBox and flags - please about explanation
Quote:
Originally Posted by RobDog888
Not sure if you guys noticed but the Or does work for adding the values but you dont get the intellisense with it to produce the menu of the next possible flag to add. When you use the "+" sign you do get the intellisense. I have been using the "+" because of this for many years as its easier to pick from a list then type it all out (yes, I do know almost all the arguments by heart lol)
Intellisense works fine for me:
Re: MsgBox and flags - please about explanation
Hmm, must be something to do with editing the "+" and switching it for an OR that it wont work. If I edit an Or for a + then it will popup. .[/color]
1 Attachment(s)
Re: MsgBox and flags - please about explanation
I cant believe I missed that you can change the RTL Reading too :lol: After all these years I never noticed them. Too bad you cant easily change the order of the text in the buttons too. :thumb:'s Steve.
Re: MsgBox and flags - please about explanation
You could take the easy way out and get MZTools
It has a great tool for making MsgBoxes.
Or try the one in my sig
Re: MsgBox and flags - please about explanation
The on and off bits for flags are often exclusive or usually the same bits are not turned in different flags within a group... so 1+3=4 (standard math) and 1 or 3=3 wouldn't normally be an issue with flags casue you wouldn't have a flag=3 if there's a flag=1... that's why you have to +/Or them in order to get the on-bits combination you require.
Re: MsgBox and flags - please about explanation
Quote:
Originally Posted by leinad31
The on and off bits for flags are often exclusive or usually the same bits are not turned in different flags within a group...
But that is not the case with the MSGBOX flags. They are using the absense of all flags and certain combinations of flags to mean specific conditions (as I posted in post #5).
If you look closely at the "combinations" of flags they actually make a lot of sense. You can almost understand how the function grew with functionality over the versions and releases of VB.
Re: MsgBox and flags - please about explanation
You mean this?
Quote:
vbOKOnly 0 Display OK button only.
vbOKCancel 1 Display OK and Cancel buttons.
vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons.
vbYesNoCancel 3 Display Yes, No, and Cancel buttons.
vbYesNo 4 Display Yes and No buttons.
vbRetryCancel 5 Display Retry and Cancel buttons.
yup its an exception to the typical, which is understandable considering you have only 4-bits alloted for the button combinations.