Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Enter Key without Beep in TextBox 2

Status
Not open for further replies.

JohnVogel

Programmer
Apr 19, 1999
117
US
Hello,

I'm building a simple form, with a textbox and no buttons. I have it so that when a user presses the Enter Key it will simply add the text to a list (as well as writing it to a file) and then blank the line waiting for another line of text. Everything is working great, except I cannot seem to get it to stop doing that "BEEP" everytime I press Enter. Here is the code I am using to allow the Enter Key to do it's thing. Any help would be greatly appreciated.

Code:
Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = Keys.Enter Then
            FileOpen(1, ThisFile, OpenMode.Append)
            ThisLine = TextBox2.Text
            PrintLine(1, ThisLine)
            ListBox1.Items.Add(ThisLine)
            I = I + 1
            TextBox2.Text = ""
            FileClose(1)
        End If

-+{John Vogel}+-


 
In the Keypress event, to stop beep on Enter and Escape
Code:
' Ignore ENTER(13) to stop beeping 
If e.KeyChar = ChrW(13) Or e.KeyChar = ChrW(27) Then e.Handled = True


Sweep
...if it works dont mess with it
 
never noticed it beeped on escape.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
I keep getting this error: 'KeyChar' is not a member of 'System.Windows.Forms.KeyEventArgs'.

Okay, what am I doing wrong now?



-+{John Vogel}+-


 
I've just about tried everything... I'm too new to this VB .net, and it's so much different from VB 6 I am just lost. I tried this (to keep from getting the error):

Code:
Dim keyPressEventArgs1 As KeyPressEventArgs
Dim returnValue As Char


Sub KeyPressed(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
   
   returnValue = keyPressEventArgs1.KeyChar
   If returnValue = ChrW(13) Then e.Handled = True

End Sub

But it doesn't seem to work. I tried just putting the e.handled in my code like this:

Code:
If e.KeyCode = Keys.Enter Then
            e.Handled = True
            FileOpen(1, ThisFile, OpenMode.Append)
            ThisLine = TextBox2.Text & " |"
            ...
End If

But that doesn't seem to work, either. Neither of the above code returns any kind of error, but it also doesn't take care of the beep either.

Is there some special way I have to define the event handler in order to use this?

Thanks.

-+{John Vogel}+-


 
I just tried this and it worked fine (no beep):
Code:
  Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    If e.KeyChar = ControlChars.Cr Then
      e.Handled = True
    End If
  End Sub

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Excellent! That was it, Rick! Thank you so much. So the problem was that I was using KeyDown instead of KeyPress, I guess. So here is the final code (for anyone interested) that actually finally (thank God) works!

Code:
    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress

        If e.KeyChar = ControlChars.Cr Then

            e.Handled = True
            FileOpen(1, ThisFile, OpenMode.Append)
            ThisLine = TextBox2.Text & " |"
            PrintLine(1, ThisLine)
            ThisLine = I & ". " & ThisLine
            ListBox1.Items.Add(ThisLine)
            I = I + 1
            TextBox1.Text = I
            TextBox2.Text = ""
            FileClose(1)

        End If

    End Sub


Thanks again, Rick!

-+{John Vogel}+-


 
John
If you read my post, it clearly mentions the keypress event. [wink]


Sweep
...if it works dont mess with it
 
Well, how do you like that? SqueakinSweep already done told me about that keypress event, and I totally missed it. Thanks SquekinSweep, I guess I didn't read your post too closely, would have saved me some time (and headaches).

-+{John Vogel}+-


 
No problem John
And honestly I wasnt begging for a star either


Sweep
...if it works dont mess with it
 
No, I know, but you deserve a star, you gave me the answer way back there! Just because I was too dense to see the answer right in front of my face doesn't mean you didn't do a great job of answering my question. The only difference was that Rick actually posted some code to show me what you already told me :-D

-+{John Vogel}+-


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top