Solved

Cancel a button event (VB.NET)

Posted on 2009-04-03
7
448 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
Independent Software Vendors: 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 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