I have a custom message box type thing and I'm not sure how to do this. I want to know how to set up the select case statement.
Anything you can tell me about how to do this is appreciated. I've never understood bitwise comparison, so I always use brute force in these situations - I.e. check every possible combination.
VB Code:
Public Enum CommandButtons
cbOK = 0 ' Default ' Should I start with 0 ???
cbCancel = 1
cbYes = 2
cbNo = 4
cbPrevious = 8
cbNext = 16
cbQuestion = 32
cbHelp = 1024
cbDontShowAgain = 2048
End Enum
Sub DisplayButtons(iValue as long)
' iValue needs to be split up to display the proper buttons
Hopefully this will get you off on the right foot. If you want an explanation of why this confusing code works, I'd be happy to explain, but I wanted to let you get your hands on something you can use.
You probably don't want to use 0 if you want to be able to combine an OK button with other buttons. You could still have 0 as the default by shecking for 0 when you enter the sub, and if ivalue is 0, use only the OK button.
Thanks. That's what I was looking for. I'll drop the 0 because I thought I might need to start with 1.
If would like to explain it, feel free. Unfortunately, I've already had it explained to me a half-dozen times and it just hasn't stuck. I think the main reason for that is that I've never really used it. I could use a refresher I guess
The key to understanding bit-wise comparison is understanding what AND and OR evaluate to when it looks at bits. AND makes a bit 1 if the 2 bits it compares are 1. OR makes a bit 1 if either bit is one. You’ve probably seen this table.
Code:
Bit Bit
1 2 AND OR
1 1 1 1
1 0 0 1
0 1 0 1
0 0 0 0
Once you’ve got this down, you should be able to figure out when to use AND and when to use OR. Here are the binary values for your CammandButtons (put in a code block to preserve spacing)
Now, if you add 2 or more of the values together, each bit in the sum is a 1 if it was a 1 in either of the values.
Cbyes + cbno = 6 = 00000110
So the trick is to check each bit in ivalue to see whether it is a 1 or a 0. To do this, you need a mask, which is a number that isolates the bit you care about. You compare your value to the mask using the AND operation. Take a look at cbyes + cbno And cbyes:
Code:
Cbyes + cbno = 00000110 And
Cbyes = 00000010
Result = 00000010
If the result equals the mask, you know that cbyes was specified.
Here’s a quick example of how it works if the mask isn’t in the value. The expression is cbyes + cbno And cbprevious
Code:
Cbyes + cbno = 00000110 And
Cbprevious = 00001000
Result = 00000000
No bit is 1 in both of the values, so the result is 0. Thus, you know that cbprevious wasn’t specified.
Hope this helps make things clearer, and not murkier.
Thank you. It makes it very clear. I need to print it out and paste it to my forehead though. I really appreciate it. Now I'm busy figuring out how to get the buttons to line up right. I've got a handle on that though. I'll post it once I've got it.
Don't sweat it. I posted the wrong thing. What I posted was old dicked up code. Just delete it. I'll post the right code when I get it. I'm going to delete that thread. Sorry.
There are TWO things that I REALLY, REALLY HATE about VB!
1) When you right click a module and then click Save As... it doesn't replace the module in your project. For example, when you tell it to save the module in a different folder, the project still uses the module in the original folder. I've lost a lot of code because I forget about that. You actually have to remove the module and then load the one you want to use.
2) Whenever you have a module maximized and then run your code and then end the app, VB minimizes the window you had open and maximizes one of its windows, like the properties window, for example. That is just so extremely irritating.
OK, I get a message box with cafeenman propaganda spewed on it, and if I click on "Do not show this message again" and click Yes, it returns 16. Fine so far.
The next time I run it, I get the same propagandabox again. Does it work for you?
Originally posted by mendhak OK, I get a message box with cafeenman propaganda spewed on it, and if I click on "Do not show this message again" and click Yes, it returns 16. Fine so far.
The next time I run it, I get the same propagandabox again. Does it work for you?
No, I haven't done anything with the "Don't Show Again" bit yet. Really I just posted this code to show how I did the buttons. That's the whole point of this thread.
In other words, I didn't post the code so you can slap it into an app. I mean it's cool if you want to do that, but there's more work to be done to make this complete. I just wanted a better way to display the right buttons without a 40 case, Select Case block of code.
Originally posted by cafeenman actually, this is what I'm doing.
VB Code:
Sub DisplayButtons(iVal As Long)
cmdYes.Visible = IIf((iVal And cbYes) = cbYes, True, False)
cmdNo.Visible = IIf((iVal And cbno) = cbno, True, False)
End Sub
You don't need the Iif function. Just try this:
VB Code:
Sub DisplayButtons(iVal As Long)
cmdYes.Visible = ((iVal And cbYes) > 0)
cmdNo.Visible = ((iVal And cbno) > 0)
End Sub
Emiliano F. Martín
If a post has helped you then pleaseRate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
Originally posted by Bruce Fox Suggestion:
(As from Mendhak)
You could still use Select Case to tidy thins up, and loose the If stuff.
ie.
VB Code:
Select Case True
Case (myNumber And cbYes)
MsgBox "cbyes"
Case (myNumber And cbNo)
MsgBox "vbno"
Case (myNumber And cbPrevious)
MsgBox "cbprevious"
End Select
That's not correct. This way you should get one message, if myNumber = cbYes + cbNo + cbprevious, when you would get three messages if you have three different if statements.
Last edited by Mc Brain; May 6th, 2002 at 04:41 AM.
Emiliano F. Martín
If a post has helped you then pleaseRate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.