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
82 posts since Sep 2010
Reputation Points: 39
Solved Threads: 2
Skill Endorsements: 0
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 (<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.
hericles
Veteran Poster
1,065 posts since Nov 2007
Reputation Points: 156
Solved Threads: 228
Skill Endorsements: 9
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
82 posts since Sep 2010
Reputation Points: 39
Solved Threads: 2
Skill Endorsements: 0
Yup, that did it- thanks for the snippet!
mrbungle
Junior Poster in Training
82 posts since Sep 2010
Reputation Points: 39
Solved Threads: 2
Skill Endorsements: 0
Question Answered as of 1 Year Ago by
hericles
and
Unhnd_Exception