Solved

Cancel a button event (VB.NET)

Posted on 2009-04-03
7
442 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now