Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi. I am trying to prevent users from entering a carriage return into a multiline textbox because it corrupts the data file I am storing info in. I changed the acceptsreturn property to false but it still allows it. I thought that maybe I needed to use an exception, but there doesn't seem to be one designed for it. I have also tried preventing it based on the keypress with the following code:

 

    Private Sub txtDescription_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDescription.KeyDown
       If e.KeyCode = Keys.Enter Then
           txtDescription.Clear()
           txtDescription.Text = entry
           MessageBox.Show("You may not have carriage returns in the descriptions.")
       Else
           entry = txtDescription.Text
       End If
   End Sub

 

Thanks for your time!

Posted

Hi. I took your advice. It is still allowing input from the enter key. I tried the following code and variants including the commented out text:

 

    Private Sub txtDescription_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDescription.KeyDown
       If e.KeyCode = Keys.Enter Then
           e.Handled = True
           e.Handled = True
           MessageBox.Show("You may not have carriage returns in the descriptions.")
       Else
           entry = txtDescription.Text
       End If
   End Sub

 

Thanks for your time!

Posted

I posted the wrong code... this is what I am using:

    Private Sub txtDescription_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDescription.KeyDown
       If e.KeyCode = Keys.Enter Then
           'txtDescription.Clear()
           'txtDescription.Text = entry
           'MessageBox.Show("You may not have carriage returns in the descriptions.")
           e.Handled = True
           'Else
           'entry = txtDescription.Text
       End If
   End Sub

 

Thanks!

  • *Experts*
Posted
Try using the KeyPress event -- you won't be able to use the KeyCode property because it doesn't exist, but there is a KeyData property which you can use.
Posted

The keydown and keypress events both set off my message box telling the user he cannot press enter, but then the carriage return appears anyway:

    Private Sub txtDescription_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDescription.KeyDown
       If e.KeyData = Keys.Enter Then
           'txtDescription.Clear()
           'txtDescription.Text = entry
           MessageBox.Show("You may not have carriage returns in the descriptions.")
           e.Handled = True
           'Else
           'entry = txtDescription.Text
       End If
   End Sub

Thanks for your time.

  • *Experts*
Posted
Er, you didn't change the event...
Private Sub txtDescription_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) [b]Handles txtDescription.KeyDown[/b]

You need to change *that* -- the actual name of the sub doesn't matter.

  • *Experts*
Posted

You also need to change the second argument from KeyEventArgs to KeyPressEventArgs.

And also, KeyPressEventArgs only comes with KeyChar property so you need to

convert it to an integer to detect enter being pressed:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Convert.ToInt32(e.KeyChar) = 13 Then '13 represents ENTER
   e.Handled = True
End If
End Sub

Posted
Thanks guys. It works great! I don't have much experience with event-handling. I apologize for the stupid mistake and wasting your time, VolteFace. This forum is always here and offers quick and useful responses, so I am going to donate money and try to contribute what I can.
Posted
I just thought of another question. Because I am protecting against data file corruption, it is critical that I don't allow the user to enter a carriage return. I have now disabled the enter key, but is there a way that I can prevent carriage returns from being pasted in?... possibly a validation check when the submit button is pressed. I tried a validation that looked for vbcrlf, but that didn't work. Thanks for your time.
Posted

Carriage return validation

 

Hi. I have to prevent users from entering carriage returns into my text boxes because it corrupts my data file. I have prevented input from the enter key with the keypress handler, but users can still paste in text that contains carriage returns. I have tried to parse the textboxes for the character vbcrlf, but that doesn't work. What character do I have to parse for when I am validating or is there a better way to do this? My other idea is to print the contents of the textbox to a text file and eliminate the carriage returns when I read it back in, but that seems unnecessary. Thanks for your time!

Posted

Try replacing all these:

 

Keys.LineFeed;

Keys.Return;

"\n";

 

I know one of them is not needed but you can try them all.

Just use the Replace method of the string class and replace them with "".

C#
  • Leaders
Posted
I don't know why you felt you needed to start a new thread for basically the same topic, so I merged your two threads. Continue your discussion here.
"These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
Posted (edited)

Sorry about the double-post. I didn't think anyone would read and respond to a slightly different question I had posted at the bottom of this since I had already posted that I had solved my problem.

 

I tried what you recommended, but it didn't work. I also tried using indexof to see if it could find the return characters in the textbox, but it couldn't. Any other ideas? Is there a way to restrict pasting to these text boxes? Thanks for your time.

Edited by martialarts
  • *Experts*
Posted

This piece of code should work fine:

 

If txtDescription.Text.IndexOf(Environment.NewLine) = -1 Then
 ' There are NO newline characters
Else
 ' There ARE newline character(s)
End If

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • 4 months later...
Posted

What part of that code prevents the ENTER key to affect the textbox? I am using the same code but it doesn't work for me. I have put a msgbox to appers when I press ENTER key, and it shows ok. But the ENTER key still affects the textbox content.

Could you help me with that?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...