Solved

Cancel a button event (VB.NET)

Posted on 2009-04-03
7
450 Views
Last Modified: 2012-06-21
As usual, lot of time spent, gotten nowhere, etc. Frustration. But I'm a professional, etc.

What I am trying to do is cancel a button keyboard event. The user would like right arrow and left arrow to navigate from form to form, but instead right and left are going up and down in the dynamically drawn button list (not a real list, just controls.add(button)).

I tried 1) "return" in all the event handlers, did nothing 2) CancelEventArgs (must not know what I'm doing here, but I tried what I read online for canceling form events) and 3) to intercept windows messages via "protected sub overrides sub wndProc."

It (the event) just keeps getting through.

The navigation is taking place right now, but you can see right before you leave the form that it went up or down one button (left or right respectively), and when you get to the new form, it has the wrong selected button value.

Thanks

0
Comment
Question by:jj_30
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 24063642
Not sure if I understood.
Have you tried this way ?

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Right Or e.KeyCode = Keys.Left Then
            e.SuppressKeyPress = True
        End If
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
    End Sub

Open in new window

0
 
LVL 2

Author Comment

by:jj_30
ID: 24063910
No I hadn't tried it that way, but I did just now. It doesn't work.

Imagine a page with several buttons, in a vertical line. If a button has focus, you press enter or space bar, it's equivalent to a click.  Similarly, if a button has focus and you do right arrow or left arrow, the button class thinks you are going down or up respectively (to the button above or below).

I want right arrow to mean space bar or enter key. And left arrow to mean, "go back to the prev. form." But I can not get a handle on these events.

It goes down or up before form or button's keyUp or keyDown events! That's what's killin me. By the time it breaks (first break is at form_keyup), I can see that the focus is already on the prev or next button.



0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24064035
>> Imagine a page with several buttons
It's asp.net ?
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 24064103
I think I understood.
You can override the ProcessDialogKey() to capture the event before update the form.
Here's an example that suppresses the space key over the button 1

    Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Space AndAlso Me.Button1.Focused Then
            Return True
        Else
            Return MyBase.ProcessDialogKey(keyData)
        End If
    End Function

Open in new window

0
 
LVL 2

Author Comment

by:jj_30
ID: 24064200
It worked!  I will have to look up process dialog key. Never heard of it before. Thanks, JJ.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24064228
Glad I could help!
jpaulino
0
 
LVL 4

Expert Comment

by:Avelan
ID: 24064287
Hey,
was working on this myself, thought i'd post something to add to jpaulino's solution since
just got it working and don't want to completely waste my time :P
This works on all buttons on the form, or atleast it should ;)

-Avelan
    Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If Me.ActiveControl.GetType() Is GetType(System.Windows.Forms.Button) Then
            If keyData = Keys.Right Or keyData = Keys.Left Then
                Return True
            Else
                Return MyBase.ProcessDialogKey(keyData)
            End If
        End If
    End Function

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question