Solved

Cancel a button event (VB.NET)

Posted on 2009-04-03
7
445 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 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