Take a simple messaging program, such as Steam friend conversation.
When you hit ENTER
, the message is sent, and the message field is emptied.
When you enter CTRL/SHIFT + ENTER
a newline is created. If your cursor is not at the end of the input text, then all the text appearing after your cursor will be sent to a newline.
Well, how do you accomplish such a feat ?
Furthermore, I'd like to know how to have the aforementioned features and also how to still be able to paste a multiline text into the message field.
For now, this is my code. It's something but does not get all the job done :
private bool ctrlOrShift = false;
private void MessageField_KeyDown( object sender, KeyEventArgs e )
{
if( e.Key == Key.LeftCtrl || e.Key == Key.LeftShift )
{
ctrlOrShift = true;
}
else if( e.Key == Key.Enter && ctrlOrShift != true && !MessageField.AcceptsReturn )
{
AsyncSendMessage();
}
else if( e.Key == Key.Enter && ctrlOrShift != true && MessageField.AcceptsReturn )
{
MessageField.AcceptsReturn = false;
}
else if( e.Key == Key.Enter && ctrlOrShift == true )
{
ctrlOrShift = false;
MessageField.AcceptsReturn = true;
MessageField.Text += System.Environment.NewLine;
MessageField.Select( MessageField.Text.Length, 0 );
MessageField.AcceptsReturn = false;
}
else
{
ctrlOrShift = false; // Canceled because follow-up key wat not ENTER !
}
}
The following scenarios occur :
- Using
CTR
orSHIFT
, I can create a new line in myTextBox
:) ; - I cannot paste a multiline text from the
Clipboard
: only the first line will be pasted, nothing else :( ; - If I use
CTRL + V
to paste content, theMessageField_KeyDown
event takes theCTRL
hit is taken into account, therefore, if I pressENTER
, message is not sent but a newline is created instead :/ (in a case where you would paste a content and send it right away) ; - If my cursor position is before the end of the input text,
CTR/SHIT + ENTER
will create a newline at the end of the text regardless of the cursor position :/
So, how can I tweak this code ? Thanks for the help !
The Result of the Solution is this:
Normal
After one
SHIFT + ENTER
When you push
ENTER
it looks like in Normal only without text in the boxAs mentioned in the comments you could use the
AcceptsReturn
andTextWrapping
properties for a multiline textbox (like in steam). UseHeight = Auto
for a better looking one (otherwise you only have one line and all other lines disappear)XAML
for Textbox:
Event Handler:
This is not that easy as i thought first :'D But i figured it out.
When you use the
AcceptsReturn
Property theEnter Key
is Handeled by theAcceptsReturn
. So if you push enter you will see a new line instead of a Send() if you programm is like this:So you need to implement the
PreviewKeyDown
eventhandler. Because in thePreviewKeyDown
event handler the Event is routed through the (Parent)Elements. Look at this AnswerAlso note the e.Handled = true line. Otherwise the Enter is routed through the method to the AcceptsReturn and you will have 2 lines after the enter, but the Textbox is empty. With this method the KeyDown Method is no longer needed!
The pros of the Multiline Textbox is that you can copy and paste + you have no problems with CTRL pushed or not.
What do you think about it?
Hope it helps