MultiLine TextBox - KeyPress Question

G

Guest

Hi,

In multiLine textBox, enter moves to the next line, how can i overwrite it,
so enter will move to the next control and ctrl + enter will move to the next
line?

Thanks,
Gidi.
 
B

Brendon Bezuidenhout

Heya Gidi,

You need to override the ProcessCmdKeys method of your form to access and
tweak the keys and what they do in your system.

<code>
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg,
System.Windows.Forms.Keys keyData)
{
if (keyData == Keys.Enter)
{
//Your method to handle the enter
return true;
//This needs to return true or your application
//Won't "see" the enter being handled in the ProcessCmdKeys
}
return base.ProcessCmdKey(msg, keyData);
}
</code>

HTH
Brendon
 
D

Duggi

Hi Gidi,

I do not know why you want this functionality. Actually, Enter shold
take to the next line and tab will take you to next control. (As per
the standards).

However if you insists on the functionality, please write or enable
keydown event for the textbox and ovverride the functionality of
same.

Thanks
-Cnu.
 
G

Guest

Hi Brendon,

Thanks,

I've 2 more questions\problems:

1. As i understand i should create a user-control that inherits from TextBox
and then override the event (I can't just overRide it in my windows form),
right?

2. now, if i want to move to the next line, i tried to do:
textbox1.Text = textBox1.Text + Environment.NewLine;

the thing is that the crouser always gets back to the right upper corner of
the TextBox (instead of staying in the newline position).

Thanks,
Gidi
 
B

Brendon Bezuidenhout

Gidi,

If I understand your questions right - With #1 You would not need to create
a custom textbox to override ProcessCmdKeys - That can be done in your form
OR it can be done in a custom textbox it depends on your application. If you
know that your form is not going to handle any weird keys like this then
create a custom control to handle it - Just make sure you check that the
textbox on your UserControl has focus - Just as a fail safe incase you do
use ProcessCmd on your form.

<code>
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg,
System.Windows.Forms.Keys keyData)
{
if (this.textBox1.Focused)
{
if (keyData == Keys.Enter)
{
//Your method to handle the enter
return true;
//This needs to return true or your application
//Won't "see" the enter being handled in the
ProcessCmdKeys
}
else if (keydata == (Keys.Enter + Keys.Shift))
{
//Your method to handle Shift + Enter
return true;
}
}
return base.ProcessCmdKey(msg, keyData);
}
</code>

As for setting the new line in the textbox - I'm not sure EXACTLY off hand
how to go about that - but I'm sure you can set the selected text in a text
box - so get the last index of the text box and set it as selected :)

HTH
Brendon
 

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

Top