I have a textbox, and it's set to multiline, so a user can enter a few sentences. The problem is, if the user hits the enter button while in the textbox, it's starting a new line. This data is being saved as a text file, and it's causing problems when the text file is loaded back into the app, causing load failures.

I'd like to completely make the enter key useless when the textbox has focus. Or, if they hit enter, it tabs over to another button.

Here is some code I used to make the enter button behave more like a tab-

Private Sub txtCallNotes_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
        End If
    End Sub

The problem is, the enter button still does a carriage return, causing a new line.

In the properties, Accept Return is False.

Is there any way this can be prevented??

Recommended Answers

All 4 Replies

I wouldn't make hitting the enter key tab the focus to the submit button because all the user is going to do is hit the enter key again (being unaware that the use of it is not allowed in the textbox) thereby submitting the unfinished textbox.
You could look at why the carriage return is causing an issue and deal with that or parse the text when it is being submitted to replace carriage returns with something else (<br /> if you're doing something in a browser).
The problem with your code above is that it isn't telling the carriage return to not be entered, its saying move focus AS WELL AS enter carriage return.

Good point, I agree with your solution. I guess I should figure out the text parse part of it, as that's where the real problem lies.

This is a good line, no errors

Entry:‡002‡0610‡0650‡0040‡City ~ Unobligated Patrol‡TEST‡

This is bad, this causes errors

Entry:‡002‡0610‡0650‡0040‡City ~ Unobligated Patrol‡TEST
Member Avatar

set e.suppresskeypress = true in the key down event

Private Sub TextBox2_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
    If e.KeyCode = Keys.Enter Then
        e.SuppressKeyPress = True
    End If
End Sub
commented: Thanks for the snippet and the time you took to help! +2

Yup, that did it- thanks for the snippet!

Be a part of the DaniWeb community

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