Results 1 to 7 of 7

Thread: [RESOLVED] "Or" problem...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Posts
    146

    Resolved [RESOLVED] "Or" problem...

    Do Or commands not work within Select Case starements? This code don't work at all. What gives?

    Code:
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    
            Select Case e.KeyCode
                Case Keys.NumPad0 Or Keys.D0
                    My.Computer.Audio.Play(My.Resources.click_x, AudioPlayMode.Background)
                    lblInput.Text += "0"
                    lblInput.Focus() 'don't want a button getting focus because it makes border around it
            End Select
        End Sub
    Also is there a nice easy way to make the button look like it was actually pressed down when the key for it is pressed just like when the mouse is held down over it?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: "Or" problem...

    What value would this return, assuming the Or operator is interpreted as a logical operator:
    Code:
    Keys.NumPad0 Or Keys.D0
    It would return either True or Flase wouldn't it? Do you really want to check whether the KeyCode is equal to True or False? I didn't think so.

    That said, what's actually happening there is that the Or is being interpreted as a bitwise operator. As such, you are adding the numerical values of those two key codes together to create a new key code, then comparing that to e.KeyCode. Again, not what you want.

    What you should have done was immediately opened your MSDN Library and read the documentation for the Select Case statement:
    Quote Originally Posted by MSDN
    You can use multiple expressions or ranges in each Case clause. For example, the following line is valid.

    Case 1 To 4, 7 To 9, 11, 13, Is > maxNumber
    Quote Originally Posted by MSDN
    Code:
    Dim number As Integer = 8
    Select Case number
        Case 1 To 5
            Debug.WriteLine("Between 1 and 5, inclusive")
            ' The following is the only Case clause that evaluates to True.
        Case 6, 7, 8
            Debug.WriteLine("Between 6 and 8, inclusive")
        Case 9 To 10
            Debug.WriteLine("Equal to 9 or 10")
        Case Else
            Debug.WriteLine("Not between 1 and 10, inclusive")
    End Select
    I think the answer is fairly obvious from that, and all you had to do was press F1. Hopefully this demonstrates how easy it is to answer the simple questions yourself. That way you only have to use forums for the really curly ones.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Posts
    146

    Re: "Or" problem...

    I fail to see how your example from MSDN would have possibly answered my question. Since it returns true or false it seems it'd simply look to the next part after the or if the first part is false and then would have ruturned true.

    I already knew you could use a range in a select case statement, but a range obviously wouldn't work here since I'm using key codes.

    Case Keys.D0 To Keys.NumPad0
    I'm nowhere near an expert VB programmer and it's obvious to me why this line wouldn't work.

    On a side note. If you thought my question didn't deem an answer why didn't you simply ignore it rather respond with a nasty attitude?? Any FYI MSDN isn't always to helpful as things there can get quite techincal and overwhelming to novices!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: "Or" problem...

    Quote Originally Posted by FooFighter
    I fail to see how your example from MSDN would have possibly answered my question. Since it returns true or false it seems it'd simply look to the next part after the or if the first part is false and then would have ruturned true.

    I already knew you could use a range in a select case statement, but a range obviously wouldn't work here since I'm using key codes.

    Case Keys.D0 To Keys.NumPad0

    On a side note. If you thought my question didn't deem an answer why didn't you simply ignore it rather respond with a nasty attitude?? Any FYI MSDN isn't always to helpful as things there can get quite techincal and overwhelming to novices!
    Oh yeah, you should never read MSDN just in case you don't understand what it says. That's the sort of attitude that holds people back. I've been using MSDN as my primary resource since I was a beginner. Every time I have an issue I look to MSDN first. If I don't find what I'm looking for or I don't understand what I find, THEN I look elsewhere. The more I've used MSDN the less often I've had to look elsewhere. That's how you learn, both to program and to use the documentation.

    As for your question, the answer is right in front of you but you appear not to have read what was provided, as opposed to just looked at it.
    Code:
    Case 6, 7, 8
    Multiple values, separated by commas.

    I did deem your question worth an answer, which is why I answered. I gave you the solution to your current problem, although you failed to see it, and I also showed you how easy it can be to find the information you need for yourself. That's a fish and a fishing lesson.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Posts
    146

    Re: "Or" problem...

    Yes you're correct I did not notice you had answered my question in your first response.

    Just FYI I often do spend hours on MSDN trying to get answers to my questions only to find more and more confusion.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] "Or" problem...

    The secret to using MSDN, I have found, is to use the index. I'm talking about the local copy you would have installed along with VS, not the online version. I very rarely use the search feature. Whenever I know the name of what you're looking for, be it a type, member, key word or whatever, I type it straight into the index. That's exactly what I did in this case to get information on the Select Case statement.

    There are numerous examples of people asking "how do I do X with class Y". I always recommend that those people do as I've suggested here and use the index to find the documentation for class Y. The first thing to do is read the class description. If that doesn't give you what you need then the next thing to do is follow the link to the member listing and read the names and descriptions to see if anything looks likely. If it does, follow the link and read more about it. That's what I've done right from the start. Trust me, it's the best way to learn. I've learned plenty on all sorts of things I wasn't even looking for as a result.

    I'd also suggest copious use of the F1 key. You can select anything in the designer or code window and press F1 to be taken straight to the most likely topic.

    Folow this advice and you'll be the one to benefit. This is the best help you can get.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Posts
    146

    Re: [RESOLVED] "Or" problem...

    Great adivce and thank you for it.

    I still like the community here and find ya'll a great help.

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