Wrox Programmer Forums
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 25th, 2004, 06:21 AM
Authorized User
 
Join Date: Jun 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default Modifier Keys

Hi all,

I have the following example procedure that checks if the SHIFT key is held down when I left mouse click. I want to be able to check for a combination of keys such as CTRL-SHIFT but can figure out how to do it. The 2nd code snippet is what I have tried with no success...


THIS WORKS AS EXPECTED:
   Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown

        If e.Button = MouseButtons.Left Then

            If Control.ModifierKeys = Keys.Shift Then
                'Do something...
            Else
                'Do something else...
            End If

        End If

    End Sub

THIS DOES NOT WORK:
    Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown

        If e.Button = MouseButtons.Left Then

            If Control.ModifierKeys = Keys.Shift AndAlso Control.ModifierKeys = Keys.Control Then
                'Do something...
            Else
                'Do something else...
            End If

        End If

    End Sub


Any help appreciated...
 
Old March 31st, 2004, 06:07 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I think your problem here is that you are trying two separate comparisons, and both are false. The reason for this is that when two keys are pressed down, the values are combined in a bitwise value. It probably works something like as follows:

Shift = 1
Control = 2
Alt = 4

Therefore, if shift and control are both pressed, the value in control.modifier keys will be 1 + 2 = 3. Thus, the comparision

Code:
If control.modifierkeys = keys.shift andalso control.modifierkeys = keys.control
is really

if 3 = 1 andalso 3 = 2

what we need is ' if 3 = 3 ' which we'll get by doing the following

Code:
If Control.ModifierKeys = (Keys.Shift + Keys.Control)
I think this should work.

 
Old March 31st, 2004, 12:20 PM
Authorized User
 
Join Date: Jun 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Excellent explanation...I actually figured this out and should have posted my result. Thanks for taking the time to respond. Much appreciated.





Similar Threads
Thread Thread Starter Forum Replies Last Post
CS0260: Missing partial modifier on declaration of belete ASP.NET 2.0 Basics 11 November 15th, 2007 04:04 PM
Friend access modifier acts as private sandeep ASP.NET 2.0 Professional 2 July 19th, 2007 07:35 AM
Unknown Modifier Error - Regular Expression Beebs BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 0 March 20th, 2007 05:48 PM
Modifier use within the viod main() Ibn_Aziz Java Basics 2 June 3rd, 2006 10:08 PM
When to define primary keys and foregin keys? method SQL Server 2000 1 August 26th, 2005 09:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.