Hi all,

I didn't think this would be a problem, but it appears that others also search for this solution. There is an annoying DING! when hitting the Enter key in a ComboBox in VB.Net so I sought solutions. Here is the solution I am working with. I created a new seperate Class within my Project...

Public Class ClasscboBox
    Inherits ComboBox
    Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
        If e.KeyChar = ControlChars.Cr Then
            e.Handled = True
        Else
            MyBase.OnKeyPress(e)
        End If
    End Sub 'OnKeyPress 
End Class

Looks good, but I am having a problem calling it from the main project. Here is what I have tried using...

Dim objcboBox As New ClasscboBox
        objcboBox.OnKeyPress(KeyPressEventArgs)

However, it says that KeyPressEventArgs is a Type and cannot be used as an Expression. I tried 'e' but no go. I am a little ahead of my classes (no pun intended) so I am likely missing something about what I am doing wrong.

I have three comboboxes so I though this would best suit my needs in the interests of writting decent code and not populating each call with repeating code.

I'd really appreciate any help on this as I am sure this would be useful to others as well. THANX!

Recommended Answers

All 16 Replies

KeyPressEventArgs is a type. Procedure OnKeyPress(ByVal e As KeyPressEventArgs) requires one argument of type KeyPressEventArgs. Create an argument of type KeyPressEventArgs and pass that to the procedure.

For example:

objcboBox.OnKeyPress(New KeyPressEventArgs(CChar("a")))

Is (almost) the same as user pressed 'a'-key.

HTH

THANKS for the reply!

I tried adding it but get an error that "Overload Resolution failed because no 'OnKeyPress' is available"

So I changed the 'a' to 13 since that is the Enter Key and it tells me that not to use an integer.

So I changed it to: Microsoft.VisualBasic.ChrW

objcboBox.OnKeyPress(New KeyPressEventArgs(Microsoft.VisualBasic.ChrW(13)))

And I am back to the error: Overload Resolution failed because no 'OnKeyPress' is available"

I did make the instance of the class just before this line of code above. Could it be in the Class here: Protected Overrides Sub OnKeyPress...?

Again, thanks!

Could it be in the Class here: Protected Overrides Sub OnKeyPress...?

Yes. I suggest reading Access Levels in Visual Basic and after that Overriding the Paint Event which explains the meaning of OnPaint and Paint events (the same idea applies to OnKeyPress and KeyPress events). Here's also ComboBox Events from MSDN.

So I finally changed the inherited class itself

Public Class ClasscboBox
    Inherits ComboBox

    Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
        If e.KeyChar = ControlChars.Cr Then
            e.Handled = True
        Else
            MyBase.OnKeyPress(e)
        End If
    End Sub 'OnKeyPress

    Public Shadows Sub KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        If e.KeyCode = Keys.Enter Then ' Enter could be trapped in KeyDown event
            e.Handled = True
        Else
            MyBase.OnKeyDown(e)
        End If
    End Sub 'KeyDown

End Class

Then you can call

Dim objcboBox As New ClasscboBox

objcboBox.KeyDown(objcboBox, New KeyEventArgs(Keys.Enter))

HTH

I've tried the following:

Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
        Dim objcboBox As New ClasscboBox
        objcboBox.KeyDown(objcboBox, New KeyEventArgs(Keys.Enter))
        If e.KeyCode = Keys.Enter Then
            ComboBox1.Items.Add(ComboBox1.Text)
            WebBrowser1.Navigate(ComboBox1.Text)
            Me.Text = (ComboBox1.Text)
            'SaveHistory()
        End If
    End Sub

And still get the windows ding error sound. Reading through the MSDN I get a little info but not much, as when I tried investigating/reading further and get info on raising the event, I get "This language is not supported or no code example is available."

All you need to do now is to tell the system that the keypress is handled:

Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
    Dim objcboBox As New ClasscboBox
    objcboBox.KeyDown(objcboBox, New KeyEventArgs(Keys.Enter))
    If e.KeyCode = Keys.Enter Then
      ComboBox1.Items.Add(ComboBox1.Text)
      WebBrowser1.Navigate(ComboBox1.Text)
      Me.Text = (ComboBox1.Text)
      'SaveHistory()
      e.Handled = True ' <- This keypress has been handled, no more processing for that
    End If
End Sub

HTH

Had me goin there, looks good, but still dings :(

Is this some sort of MS torture tactic? I mean, it shouldn't even be there, or at least should have a property setting for it LOL! Here is the current code again, with first the class and then the routine.

Public Class ClasscboBox
    Inherits ComboBox

    Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
        If e.KeyChar = ControlChars.Cr Then
            e.Handled = True
        Else
            MyBase.OnKeyPress(e)
        End If
    End Sub 'OnKeyPress

    Public Shadows Sub KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        If e.KeyCode = Keys.Enter Then ' Enter could be trapped in KeyDown event
            e.Handled = True
        Else
            MyBase.OnKeyDown(e)
        End If
    End Sub 'KeyDown

End Class
Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
        Dim objcboBox As New ClasscboBox
        objcboBox.KeyDown(objcboBox, New KeyEventArgs(Keys.Enter))
        If e.KeyCode = Keys.Enter Then
            ComboBox1.Items.Add(ComboBox1.Text)
            WebBrowser1.Navigate(ComboBox1.Text)
            Me.Text = (ComboBox1.Text)
            'SaveHistory()
            e.Handled = True ' <- This keypress has been handled, no more processing for that
        End If
    End Sub

Is it possible I need to call this from the SelectedIndexChanged event? This is brought up when pressing the Enter key from the designer. But I can't seem to get the value of e. to pass to it though I added a new sub for it.

Anyone? Someone? C'mon, there has to be somebody who knows VB enough to help me out with this.

Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown Is ComboBox1 WinForm's ComboBox or instance of the class ClasscboBox?

Dim objcboBox As New ClasscboBox
objcboBox.KeyDown(objcboBox, New KeyEventArgs(Keys.Enter))

Here you're creating a new instance of ClasscboBox and send a key press to that instance. Why? If there's no reason for that, drop these lines (dings should go away). ComboBox1.Items.Add(ComboBox1.Text) Is this correct? ComboBox1.SelectedItem.ToString() gets the selected item as a string. And that's already in the ComboBox1 so why should it be added there?

HTH

The ComboBoxes are WinForm ComboBoxes.
Dropping those lines did not make the ding go away.

The additional selected item line had since been removed, but is unimportant at this time anyway as I am mainly concerned with the annoying ding sound. (This and a TreeView are my only two stumbling blocks for this project.)

I did not think simply removing those lines would make this ding sound vanish as there are two other comboboxes in the project in the main WinForm (Form1) that exhibit the same problem (windows error ding sound) and do not have the solution attached to them. They had this sound from the start and obviously need some additional code in order to catch and stop it from happening.

You asked about what code the ComboBox belongs to, which is the main WinForm. Perhaps there is something to that, and I need to modify the Class ClasscboBox.vb?

I welcome any solutions or ideas. Being done with a Class is not a requirement, but was part of a possible solution I thought would be worth working with. It could be that I'm trying to accomplish this the wrong way altogether.

By the way. If you could use comboboxes as ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList , there won't be any dings.

Otherwise

Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
  If e.KeyCode = Keys.Enter Then
    e.SuppressKeyPress = True
  End If
End Sub

disables Enter-key for ComboBox1-control and thus prevents ding.

I might have suggested originally e.Handled = True but that doesn't prevent dinging.

HTH

CLOSE! But no cookie.

It disabled the functionality of the combobox that is needed, which is a drop down style.

I still need to stop the ding. It is driving me slightly mad, I must admit.

The ComboBox is to hold a web browser URL history as entered into the combobox by the user.

Ok. So you can't "disable" Enter-key functionality (user selects from the list and presses Enter-key).

IMHO you have two ways to act for the Enter-key. A simple solution

Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
    If e.KeyCode = Keys.Enter Then
        ' Do your stuff here (open url or whatever)
        ' Finally suppress Enter-key
        e.SuppressKeyPress = True
    End If
End Sub

Do your stuff in the event handler itself before suppressing Enter-key.

Second solution is to trap Enter-key in the PreviewKeyDown event handler, do your stuff there and let the Enter-key pass through. After PreviewKeyDown event the KeyDown event is raised and you'll suppress Enter-key there

Private Sub ComboBox1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles ComboBox1.PreviewKeyDown
    If e.KeyCode = Keys.Enter Then
        ' User pressed Enter. Save the text user typed, open url or whatever
        ' Enter-key passes through and will be suppressed in KeyDown event handler
    End If
End Sub

Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
    If e.KeyCode = Keys.Enter Then
        e.SuppressKeyPress = True
    End If
End Sub

Basically this second solution doesn't have any advantages in itself, but may be useful.

As I (quickly) tested these, the combobox behaved "normally" (ComboBoxStyle.DropDown). I could type in to the box and/or select items from the list and finally press Enter-key to indicate that the selection is ready. And all without any dings.

HTH

BRILLIANT!

The first didn't work, but the second option worked GREAT! :D

I'd wondered why the 1st didn't and tried this:

If e.KeyCode = Keys.Enter Then
            MessageBox.Show("ComboBox1_KeyDown", "ComboBox1_KeyDown Event Triggered")
            ComboBox1.Items.Add(ComboBox1.Text)
            WebBrowser1.Navigate(ComboBox1.Text)
            Me.Text = (ComboBox1.Text)
            e.SuppressKeyPress = True
	End If

I thought the MessageBox would precede the ding, but both occured simultaniously.

However, your suggestion of using the PreviewKeyDown was the answer. ^5 THANK YOU!!


HERE IS THE SOLUTION AS GIVEN BY Teme64:

Private Sub ComboBox1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles ComboBox1.PreviewKeyDown
    If e.KeyCode = Keys.Enter Then
        ' User pressed Enter. Save the text user typed, open url or whatever
        ' Enter-key passes through and will be suppressed in KeyDown event handler
    End If
End Sub

Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
    If e.KeyCode = Keys.Enter Then
        e.SuppressKeyPress = True
    End If
End Sub

Having only gone through an introudctory course my 1st semester for web programming, I was unprepared for this. I thank you SO much for your help!!!

-Tom

New problem: PreviewKeyDown executes code twice.

When testing I realized that everything in the PreviewKeyDown routine happens twice. I'm not sure what would be triggering this to happen yet. I did remove the Class file from the project, by the way, and am using this method you suggest.

It adds the entered text twice and even shows a message dialog box twice when I tested it with one. Of course it runs once in the KeyDown event routine, but then, of course, it again emits the Ding sound.

SO close to licking the problem, though, Teme64! I'll play with it some more and see what I can figure out, but if you have any ideas, do tell. :) Again, THANX!

-Tom

New problem: PreviewKeyDown executes code twice.

Yes it does and I don't know why. I checked the PreviewKeyDown event's arguments and they seemed to be same both times. Since you know that the code is executed twice (always???) you could use a flag to check if you're dealing with the second call

Private _aFlag As Boolean = False

Private Sub ComboBox1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles ComboBox1.PreviewKeyDown
    If e.KeyCode = Keys.Enter Then
        If _aFlag Then ' This is second call, reset flag and abort procedure
            _aFlag = False
            Exit Sub
        End If
        ' User pressed Enter. Save the text user typed, open url or whatever
        ' Enter-key passes through and will be suppressed in KeyDown event handler
        ' Set the flag to indicate that the first call to this proc has been executed
        _aFlag = True
    End If
End Sub

It adds the entered text twice and even shows a message dialog box twice when I tested it with one. Of course it runs once in the KeyDown event routine, but then, of course, it again emits the Ding sound.

Actually MessageBox.Show() is now causing that ding.

To check if an URL has already been added to list, and to prevent it appearing twice, check if URL exists in the list

If ComboBox1.Items.Contains(ComboBox1.Text) Then
    ' This URL exists. Do not insert it, just navigate
    WebBrowser1.Navigate(ComboBox1.Text)
Else
    ' A new URL, add to list and navigate
    ComboBox1.Items.Add(ComboBox1.Text)
    WebBrowser1.Navigate(ComboBox1.Text)
End If
Me.Text = (ComboBox1.Text)

or

If ComboBox1.Items.IndexOf(ComboBox1.Text) >= 0 Then
    ' This URL exists. Do not insert it, just navigate
    WebBrowser1.Navigate(ComboBox1.Text)
Else
    ' A new URL, add to list and navigate
    ComboBox1.Items.Add(ComboBox1.Text)
    WebBrowser1.Navigate(ComboBox1.Text)
End If
Me.Text = (ComboBox1.Text)

I didn't check for case-sensitivity. If the test (Contains or IndexOf) is case-sensitive (URLs are case-insensitive), I suggest lower-casing items before adding them to the list. In this case the code would be

' Items are stored in lower case so you have to use ToLower() when checking if item exists
If ComboBox1.Items.Contains(ComboBox1.Text.ToLower()) Then
    ' This URL exists. Do not insert it, just navigate
    WebBrowser1.Navigate(ComboBox1.Text)
Else
    ' A new URL, add to list and navigate
    ' URL is converted to lower case before adding to list
    ComboBox1.Items.Add(ComboBox1.Text.ToLower())
    WebBrowser1.Navigate(ComboBox1.Text)
End If
Me.Text = (ComboBox1.Text)

HTH

EXCELLENT!

I'd thought overnight of ways to test, such as the flag/variable, but have been a bit distracted by a sick pet, so I SUPER appreciate the help!

FINAL SOLUTION for ComboBox Ding:

Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            e.SuppressKeyPress = True
        End If
    End Sub
Private Sub ComboBox1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles ComboBox1.PreviewKeyDown
        If e.KeyCode = Keys.Enter Then
            ' Code here executes twice, so test for existing URL
            ' Items are stored in lower case so you have to use ToLower() when checking if item exists
            If ComboBox1.Items.Contains(ComboBox1.Text.ToLower()) Then
                ' This URL exists. Do not insert it, just navigate
                WebBrowser1.Navigate(ComboBox1.Text)
            Else
                ' A new URL, add to list and navigate
                ' URL is converted to lower case before adding to list
                ComboBox1.Items.Add(ComboBox1.Text.ToLower())
                WebBrowser1.Navigate(ComboBox1.Text)
            End If
            Me.Text = (ComboBox1.Text)
            'SaveHistory()
        End If
    End Sub

Thanx to Teme64 for solving this perplexing problem!

-Tom

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.