1,105,546 Community Members

Preventing carriage return in multiline text box

Member Avatar
mrbungle
Junior Poster in Training
82 posts since Sep 2010
Reputation Points: 29 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
hericles
Veteran Poster
1,078 posts since Nov 2007
Reputation Points: 98 [?]
Q&As Helped to Solve: 232 [?]
Skill Endorsements: 14 [?]
Featured
 
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.

Member Avatar
mrbungle
Junior Poster in Training
82 posts since Sep 2010
Reputation Points: 29 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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
‡
Unhnd_Exception
Deleted Member
 
1
 

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
Member Avatar
mrbungle
Junior Poster in Training
82 posts since Sep 2010
Reputation Points: 29 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Yup, that did it- thanks for the snippet!

Question Answered as of 2 Years Ago by Unhnd_Exception and hericles
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: