954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Preventing carriage return in multiline text box

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
            Me.btnSubmit.Focus()
        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??

mrbungle
Junior Poster in Training
76 posts since Sep 2010
Reputation Points: 39
Solved Threads: 2
 

Hi,
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 (
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.

hericles
Practically a Posting Shark
823 posts since Nov 2007
Reputation Points: 136
Solved Threads: 167
 

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
‡
mrbungle
Junior Poster in Training
76 posts since Sep 2010
Reputation Points: 39
Solved Threads: 2
 


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
Unhnd_Exception
Posting Pro
571 posts since Nov 2010
Reputation Points: 249
Solved Threads: 201
 

Yup, that did it- thanks for the snippet!

mrbungle
Junior Poster in Training
76 posts since Sep 2010
Reputation Points: 39
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: