martialarts Posted August 5, 2003 Posted August 5, 2003 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! Quote
*Experts* Volte Posted August 5, 2003 *Experts* Posted August 5, 2003 Just set e.Handled = True to make it ignore the key you entered. Quote
martialarts Posted August 5, 2003 Author Posted August 5, 2003 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! Quote
martialarts Posted August 5, 2003 Author Posted August 5, 2003 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! Quote
*Experts* Volte Posted August 5, 2003 *Experts* Posted August 5, 2003 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. Quote
martialarts Posted August 5, 2003 Author Posted August 5, 2003 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. Quote
*Experts* Volte Posted August 5, 2003 *Experts* Posted August 5, 2003 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. Quote
*Experts* mutant Posted August 5, 2003 *Experts* Posted August 5, 2003 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 Quote
martialarts Posted August 5, 2003 Author Posted August 5, 2003 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. Quote
martialarts Posted August 5, 2003 Author Posted August 5, 2003 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. Quote
martialarts Posted August 6, 2003 Author Posted August 6, 2003 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! Quote
aewarnick Posted August 6, 2003 Posted August 6, 2003 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 "". Quote C#
Leaders John Posted August 6, 2003 Leaders Posted August 6, 2003 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. Quote "These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
martialarts Posted August 6, 2003 Author Posted August 6, 2003 (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 August 6, 2003 by martialarts Quote
*Experts* Bucky Posted August 6, 2003 *Experts* Posted August 6, 2003 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 Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
martialarts Posted August 7, 2003 Author Posted August 7, 2003 That fixed the problem. Thanks for your time! Quote
AlexMesquita Posted January 6, 2004 Posted January 6, 2004 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? Quote
AlexMesquita Posted January 6, 2004 Posted January 6, 2004 e.Handled = True prevents that only if I am using a textbox. But is I use a Richtextbox, that doesn't work. Do you have any idea? Thanks. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.