Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

ShowDialog fired by Enter key causes Beep

Posted on 2006-04-03
22
Medium Priority
?
1,409 Views
Last Modified: 2008-02-01
If I try to open a form using the ShowDialog method from a controls Keydown event, a beep is sounded as the form is opened even if I supress the keystroke first.


        If e.KeyCode = Keys.Enter Then
            e.SuppressKeyPress = True
            With New frm
                .ShowDialog()
            End With
        End If
0
Comment
Question by:davidabyford
  • 10
  • 7
  • 4
21 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 16364307
what code is there in the form_Load?
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 16364328
e.SuppressKeyPress   this makes the beep!!! when you hold any button the buffer gets full and makes a system beep

 If e.KeyCode = Keys.Enter Then
            e.SuppressKeyPress = True
            With New frm
                .ShowDialog()
            End With
           e.SuppressKeyPress = False
        End If
0
 

Author Comment

by:davidabyford
ID: 16364529
Hi Mike

How does the e.SuppressKeyPress = False help, the beep occurrs on the showdialog line.
Form has nothing in it's open event, in fact form has no code at all.
Enter or Return keys seem to be the problem, other keytypes like function key's work silently.
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

Expert Comment

by:Mikal613
ID: 16364557
and if you do this

 If e.KeyCode = Keys.Enter Then
                        With New frm
                .ShowDialog()
            End With
                 End If

What happens?
0
 

Author Comment

by:davidabyford
ID: 16364583
beep
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 16364633
wow!!!and if you add a button to that form and use the button click to open a new form do you hear the beep?
0
 

Author Comment

by:davidabyford
ID: 16364681
Nope, beep only occurrs opening form modally when trapping Enter/Return

0
 
LVL 48

Expert Comment

by:Mikal613
ID: 16364837
try

 If e.KeyCode = Keys.Enter Then
          e.keycode = 0
             With New frm
                .ShowDialog()
            End With
End If
0
 

Author Comment

by:davidabyford
ID: 16364890
Like I would do in VB6, unfortunately keycode property is readonly in .NET
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 16364904
forgot!! sorry
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 16364907
try using the key up event
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 16364920
or keypress
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16365013
Right...use the KeyPress event and then set the Handled() property to True:

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
            e.Handled = True
            With New frm
                .ShowDialog()
            End With
        End If
    End Sub
0
 

Author Comment

by:davidabyford
ID: 16365053
That does work and I use it VB6 but hadn't considered in this context because I also use the textbox to handles certain function key presses as well as the enter. BUT.....

Does mean I could split the logic between keydopwn and keypress, keydown won't be handling enter any more, so should be fine.

Thanks, will try this tonight.
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 16365076
great
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16365102
There shouldn't be a problem splitting the logic between KeyDown/KeyUp and KeyPress (unless you duplicate keys in them).
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 16365123
even if you dup them youll have to actions for that button thats also fine. So your actions should not be duped
0
 

Author Comment

by:davidabyford
ID: 16365128
OR ....  Just keep my original keydown code(with or without keysupress) and put e.handled in keyPress
Simple as that
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 750 total points
ID: 16365152
I agree and think that would be the simplest:

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
            e.Handled = True
        End If
    End Sub

...and do the actual code in the KeyDown() event...
0
 

Author Comment

by:davidabyford
ID: 16365159
what I meant for the keypress was :

        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            e.Handled = True
        End If

surpressing all keystrokes would not be good :-)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16365166
Exactly...  =)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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 quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

581 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