Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Cancel a button event (VB.NET)

Posted on 2009-04-03
7
Medium Priority
?
454 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
  • 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
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!

 
LVL 48

Accepted Solution

by:
jpaulino earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month20 days, 20 hours left to enroll

810 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