cancel newline in multiline textbox

  • Thread starter Thread starter sverre.bakke
  • Start date Start date
S

sverre.bakke

Hi

I am using a multiline textbox with wordwrapping for creating "large"
texts, but I dont want the user to be able to create a new line. The
property called SuppressKeyPress seem to be the perfect way to solve
this using the KeyDown event. It doesnt work.

My code looks like this:

if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true; //doesnt work

MessageBox.Show("yes I pushed enter and
SuppressKeyPress was changed");
}


But still it doesnt work. After reading MSDN I am sure that there is
something MSDN hasnt told me about SuppressKeyPress or there are some
sort of bug, cause I cant get this to work.

Thanks in advance for the help.
 
I'm assuming this is handled in your KeyPress event handler. Your solution
should work fine as long as you are testing for both enter or return.

e.SuppressKeyPress = (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return);
 
Jared said:
I'm assuming this is handled in your KeyPress event handler. Your solution
should work fine as long as you are testing for both enter or return.

e.SuppressKeyPress = (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return);

Excellent! it worked. Didnt know I had to trap them both. Thanks for
the help!
 
Excellent! it worked. Didnt know I had to trap them both. Thanks for
the help!

I just found out that this doesnt work if I call a blocking messagebox
inside the method. Any quickfix for this? What happens is that the
moment I click "OK" on the messagebox the newline appear even though I
did suppresskeypress.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top