We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,263 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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??

3
Contributors
4
Replies
10 Hours
Discussion Span
1 Year Ago
Last Updated
7
Views
Question
Answered
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

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
Deleted Member

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

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1710 seconds using 2.66MB