Multiline textbox with skype like Functionalities - c#

I was trying to have a textbox that creates new line when Shift+Enter is pressed and we can get keyevents when Enter is pressed
on backend of KeyDown Event i want to note that if Enter is pressed then do something.
if (e.Key.Equals(Key.RightShift))
{
}
this works fine for single line.as far as i click AcceptReturn = true and textwrapping to wrap then on pressing Enter new line is added to textbox but the event does not fire up.
i want new line to happen at Shift+Enter
and on Enter event should fire.
any idea?

I think you probably need to handle the PreviewKeyDown event. Catch the keypress combination you want to handle, handle it, then set e.Handled = true to make sure it doesn't get handled anywhere else as the keypress event tunnels & bubbles.
XAML:
<TextBox TextWrapping="Wrap" AcceptsReturn="True"
PreviewKeyDown="TextBox_PreviewKeyDown" />
Code-behind:
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyboardDevice.Modifiers == ModifierKeys.None && e.Key == Key.Enter)
{
e.Handled = true;
// Do your special enter handling here...
}
// Shift+Enter (and any other keys) will be handled as normally...
// ...you'll still get your new line on Shift+Enter
}
Note: If you want an Enter keypress to still add a new line as well as your special handling then just remove the e.Handled = true line.

Try the following on the KeyDown event, should work on a vanilla multiline textbox, no need for anything further:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (!e.Shift && e.KeyValue == (int)Keys.Enter)
{
e.SuppressKeyPress = true;
// Fire my custom event
}
}

Related

Get keydown event without a textbox

Im trying to do like that: If I press the key "P" a messagebox will open in the screen.But I need to do it without a textbox or other tool, I want to do that direct in the form.
I tried:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.P)
{ MessageBox.Show("Key P pressed"); }
}
Try with the KeyPress Event of the form. It just works fine.
Assuming this is Winforms, on the form you're trying to catch the event on, make sure to set
Form1.KeyPreview = true;
KeyPreview ensures that keyboard events anywhere on the particular form (such as to a textbox with focus) will still count as a keyboard event for the form itself.

Keydown Event fires twice

On a Windows store App, I have this simple TextBox
<TextBox Name="TextBoxUser" HorizontalAlignment="Right" Width="147" Margin="20,0,0,0" KeyDown="TextBox_KeyDown" /
That has a KeyDown Event associated with it.
private async void TextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
Debug.WriteLine("LA");
}
}
And the output of this function is:
LALA
although I press Enter only once, it prints 2 times.
Any reason for that or am I doing something wrong?
This should only fire the event once, so if it is firing twice I would check a couple of things.
Check that you aren't handling the key down event on a parent control. This could be a panel or the containing window. Events will bubble down through the visual tree. For example a key down on a textbox will also be a keydown on the window containing the textbox.
To stop this happening you can mark the event as handled as below;
e.Handled = true;
The other thing to check is that you aren't subscribing to the event twice. The XAML will do the same as;
TextBoxUser.KeyDown += TextBox_KeyDown
so check that you don't have this in your code behind.
You can check the sender and e.OriginalSource property to see where the event is being fired from.
This is a known bug in Windows RT.You can handle it by checking the Key RepeatCount `
if (e.KeyStatus.RepeatCount == 1)
{
//Execute code
}
I finally realized that there is probably a bug in the KeyDown event.
When I set, as #daniellepelley said,
e.Handled = true;
the event still propagates: other buttons will intercept it, also if they shouldn't.
In my code, I just replaced KeyDown event with KeyUp event and everything works fine (always setting Handled to True value)!
This might be too late, but e.preventDefault() worked for me. Especially in React - TypeScript.
(e: KeyboardEvent) => {
e.preventDefault();
console.log(e);
};
void onKeyPressEvent(object sender, KeyEventArgs e)
{
if(e.Handled)
return;
{
//
// the block of codes to be executed on the key press
// should added here.
//
}
e.Handled = true;
}
I had this same issue in Xamarin.Android where the keypress event fired twice.
In my case it is because the KeyDown and KeyUp are two separate actions which both call the same event. I only respond to one action as below:
private void KeyPress(object sender, View.KeyEventArgs e)
{
if (e.KeyCode == Keycode.Enter)
{
if (e.Event.Action == KeyEventActions.Down)
{
Event();
}
}
}
i was seeing this issue in javascript, where keydown event handler is fired twice, and was able to fix by using .off().on()
$('.text_area').off().on('keydown', function (e) {
// do something
)};
what it does is that, it switches off the extra events and the takes the latest keydown action and executes the handler for it
for me only this solution worked well
thanks

c# Textbox KeyEventArgs - each letter is checked multiple times

I am checking the keycode for a textbox, and I want a certain task to be performed when the user presses Enter.
It has been working perfectly, but the task that I am trying to perform now, usually is done using a mouse click. So on the OK on that task (a FolderBrowserDialog), it keeps calling the dialog control.
Oddly enough, even though the debugger shows me into the if branch, for e it shows {KeyData = LButton|MButton|Back}, but KeyValue is still 13...
I think it may be that the textbox remembers its last entry... True ?
In my troubleshooting, I have added a boolean variable so I only go into FolderBrowser when it is true, I have tried to add and delete a space from the textbox after the Browse, and even clear the textbox... Each attempt seemed to make things worse.
It seemed that I was in a quasi-infinite loop - yet it would go away after lots of "ok"'s, and stepping through, I found that for every letter I type in the textbox, I spend 4 to 5 rounds in the CheckKeys. I don't understand why... Or how to fix it.
I added a "e.Handled" which did me no good.
Here's the code:
private void txtDir_TextChanged(object sender, EventArgs e)
{
this.txtDir.KeyUp += new System.Windows.Forms.KeyEventHandler(CheckKeys);
}
private void CheckKeys(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (sender == txtDir && txtDir.Text != "" && System.IO.Directory.Exists(txtDir.Text))
{
btnBrowse_Click(this, e);
}
}
}
Why am I going through this check so many times ? Can I add a different test ? Am I doing something wrong ? (nothing is set as default action, for form or textbox...)
Thank you.
In your code you added handler for KeyUp at TextChanged Event. so, When TextChanged new handler will be added for KeyUp Event. Thats why multiple time each letter is checked. put Handler at Form load event.
e.g. If I have entered five letter in TextBox so, 5 Handler will be added for KeyUp. means Number of KeyUp Event Handler equals to number of time TextChanged Event called.
this.txtDir.KeyUp += new System.Windows.Forms.KeyEventHandler(CheckKeys);
this add new handler for KeyUp event. So, when this line execute new handler will be added.
By putting Handler at Form Load event, you can solve multiple letter checked problem.
Try with,
private void FormLoad(object sender, EventArgs e)
{
this.txtDir.KeyUp += new System.Windows.Forms.KeyEventHandler(CheckKeys);
}
private void CheckKeys(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (sender == txtDir && txtDir.Text != "" && System.IO.Directory.Exists(txtDir.Text))
{
btnBrowse_Click(this, e);
}
}
}
And one more thing As I understand your code, you want to execute btnBrowse_Click if Enter pressed in TextBox control. But Enter key not handled with KeyUp event you need KeyDown Event handler to handle Enter key.
Code:
private void FormLoad(object sender, EventArgs e)
{
this.txtDir.KeyDown += new System.Windows.Forms.KeyEventHandler(CheckKeys);
}
private void CheckKeys(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (sender == txtDir && txtDir.Text != "" && System.IO.Directory.Exists(txtDir.Text))
{
btnBrowse_Click(this, e);
}
}
}
Use KeyDown event instead of TextChanged and write down e.Handle = True in it.
write down following code in your textBox.KeyDown event:
if (e.KeyCode == Keys.Enter)
{
if (sender == txtDir && txtDir.Text != "" && System.IO.Directory.Exists(txtDir.Text))
{
e.Hanlde = true; //it will be close enter keydown handling at this time
btnBrowse_Click(this, e);
}
}
Replaced TextChanged with KeyDown directly and it works again !
(on a side note, I still don't understand why each letter is tested several times...)
Edit: Now after reading the accepted answer, I do understand...

Are some keyboards more loquacious than others?

The lead developer says that when he uses my app, his keyboard beeps when he moves between TextBoxes on the TableLayoutPanel via the directional arrow keys.
However, I hear no such aural activity.
Here's my code:
// Had to intercept Up and Down arrows from Windows
private void textBoxPlatypi_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
TextBox tb = (TextBox)sender;
if (e.KeyCode.Equals(Keys.Up)) {
SetFocusOneRowUp(tb.Name);
return;
}
if (e.KeyCode.Equals(Keys.Down)) {
SetFocusOneRowDown(tb.Name);
return;
}
}
private void textBoxPlatypi_KeyDown(object sender, KeyEventArgs e) {
TextBox tb = (TextBox)sender;
if (e.KeyCode.Equals(Keys.Left)) {
SetFocusOneColumnBack(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Right)) {
SetFocusOneColumnForward(tb.Name);
e.Handled = true;
return;
}
}
..He thought maybe I needed "e.Handled" but that is not available in the PreviewKeyDown event.
Is there a way to suppress the beeping (which apparently occurs only with certain keyboards or specific setups (he's using Windows7, I'm on XP still))?
UPDATE
I've got this code now:
private void textBoxPlatypus1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
switch (e.KeyCode) {
case Keys.Down:
case Keys.Up:
e.IsInputKey = true;
break;
}
}
private void textBoxPlatypus1_KeyDown(object sender, KeyEventArgs e) {
TextBox tb = (TextBox)sender;
if (e.KeyCode.Equals(Keys.Up)) {
SetFocusOneRowUp(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Down)) {
SetFocusOneRowDown(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Left)) {
SetFocusOneColumnBack(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Right)) {
SetFocusOneColumnForward(tb.Name);
e.Handled = true;
return;
}
}
...but he still hears the beeping (I don't).
He's in Alaska and using Windows 7; I'm in California and using XP. I don't know if some combination/mismatch there is the problem...
UPDATED AGAIN
I know this may be shocking to some, but the Alaska/California disconnection has nothing to do with it. I'm now hearing the beeps, too, and it's not from the arrow keys. It's when a value is entered in a TextBox and then, if that text box already has a character, focus is moved to the next textBox and the value is entered there (this is my code that causes this to happen). But the irritating beeping seems to be random - I haven't figured out the pattern for when it beeps (sometimes it does, sometimes it doesn't)...has anybody ever run across anything like that, or, better yet, know how to suppress the beep? All I'm doing is pressing either the "1" or the "2" key above the keyboard.
There is no way in the PreviewKeyDownEvent to Handle / Supress a KeyEvent like there is in the normal KeyDown Event. What the documentation suggests is to set the PreviewKeyDownEventArgs.IsInputKey property to true in order to handle key presses that are not available normally in the KeyDown Event.
From above Link, they are using a button as an example:
Some key presses, such as the TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses... By handling the PreviewKeyDown event for a Button and setting the IsInputKey property to true, you can raise the KeyDown event when the arrow keys are pressed. However, if you handle the arrow keys, the focus will no longer move to the previous or next control.
Try this:
e.SuppressKeyPress = true;

Stop the 'Ding' when pressing Enter

I have a very simple Windows Forms Application. And, in Windows (or, atleast Windows Forms Applications), when you press Enter while inside a Single-line TextBox Control, you hear a Ding. It's an unpleasent sound, that indicated you cannot enter a newline, because it is a single-line TextBox.
This is all fine. However, in my Form, I have 1 TextBox, and a Search Button. And I am allowing the user to Perform a search by pressing Enter after they've finished typing, so they don't have to use the mouse to click the Search Button.
But this Ding sound occurs. It's very annoying.
How can we make it so just that sound doesn't play at all in my Form?
#David H - Here's how I'm detecting the enter pressing:
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// Perform search now.
}
}
It works for me:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
//Se apertou o enter
if (e.KeyCode == Keys.Enter)
{
//enter key is down
this.doSomething();
e.Handled = true;
e.SuppressKeyPress = true;
}
}
The SuppressKeyPress is the really trick. I hope that help you.
Check out the Form.AcceptButton property. You can use it to specify a default button for a form, in this case for pressing enter.
From the docs:
This property enables you to designate
a default action to occur when the
user presses the ENTER key in your
application. The button assigned to
this property must be an
IButtonControl that is on the current
form or located within a container on
the current form.
There is also a CancelButton property for when the user presses escape.
Try
textBox.KeyPress += new KeyPressEventHandler(keypressed);
private void keypressed(Object o, KeyPressEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true; //this line will do the trick
}
}
Just add e.SuppressKeyPress = true; in your "if" statement.
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//If true, do not pass the key event to the underlying control.
e.SuppressKeyPress = true; //This will suppress the "ding" sound.*/
// Perform search now.
}
}
You can Use KeyPress instead of KeyUp or KeyDown its more efficient
and here's how to handle
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true;
button1.PerformClick();
}
}
and say peace to the 'Ding'
Use SuppressKeyPress to stop continued processing of the keystroke after handling it.
public class EntryForm: Form
{
public EntryForm()
{
}
private void EntryTextBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
e.Handled = true;
e.SuppressKeyPress = true;
// do some stuff
}
else if(e.KeyCode == Keys.Escape)
{
e.Handled = true;
e.SuppressKeyPress = true;
// do some stuff
}
}
private void EntryTextBox_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
// do some stuff
}
else if(e.KeyCode == Keys.Escape)
{
// do some stuff
}
}
}
On WinForms the Enter key causes a Ding sound because the form property AcceptButton is not specified.
If you don't need an AcceptButton the ding sound can be suppressed by setting the form KeyPreview to true and enter the following KeyPress event:
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
e.Handled = true;
}
No matter what control is active, there will be no more ding sound when pressing the Enter key. Since the key event proccessing order is KeyDown, KeyPress and KeyUp the Enter key will still work for the KeyDown events for the controls.
I stumbled on this post while trying to handle a KeyDown this worked for me.
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
btnLogIn.PerformClick()
End If
Supressing the Key Press stops the event from being sent to the underlying control. This should work if you're manually handling everything that the enter key will be doing within that textbox. Sorry about the Visual Basic.
$("#txtSomething").keypress(function (e) {
if (e.which == 13) {
e.Handled = true; //This will prevent the "ding" sound
//Write the rest of your code
}
});
There is a very little chance anyone gets to this answer but some other answers are truly scary. Suppressing event on KeyDown kills 2 additional events in one strike. Setting e.Handled property to true is useless in this context.
The best way is to set Form.AcceptButton property to the actual Search Button.
There is also another way of utilizing Enter key - some people may want it to act as TAB button. To do that, add a new Button, set its Location property outside of the Form area (i.e. (-100, -100)) - setting Visible property to false may disable Button handlers in some cases. Set Form.AcceptButton property to your new button. In Click event handler add following code
this.SelectNextControl(ActiveControl, true, true, true, true)
Now, you may want to transfer focus only when focus it on TextBox you may want to either test ActiveControl type or use e.Supress property in event handlers of controls not meant to use Enter as TAB
That's it. You don't even need to capture e.KeyCode
Set your Search button's IsDefault property to true. This will make it a default button and it will be auto-clicked when Enter is pressed.
Well I lived with this problem long enough and looked it up here.
After thinking about this for quite some time and wanting the simplest way to fix it I came up with the easiest but not so elegant way to fix it.
Here is what I did.
Put 2 invisible buttons "Ok" and "Cancel" on the form.
Set the AcceptButton and CancelButton Property on the form to the invisible buttons.
Added no code to the buttons!
This solved all the secondary problems listed in this thread including the ToolStripMenu. My biggest complaint was the BindingNavigator, when I would enter a record number into the Current position to navigate to and pressed enter.
As per the original question in which the programmer wanted a search function when the enter button was pressed I simply put the search code in the invisible OK Button!
So far this seems to solve all problems but as we all know with Visual Studio, something will probably crop up.
The only other possible elegant way I could think of would be to write a new keystroke handling class which is way to much work for most of my projects.
You can set your textbox multi-line to true then handle the Enter key press.
private void yourForm_Load(object sender, EventArgs e)
{
textBox1.Multiline = true;
}
//then write your TextBox codes
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// doSomething();
}
}
i changed the textbox properties for an multiline textbox and it works for me.
Concerning the e.SuppressKeyPress = true; solution, it works fine by itself. Setting SuppressKeyPress to true also sets Handled to true, so there's no need to use e.Handled= true;
void RTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
//do ...
bool temp = Multiline;
Multiline = true;
e.Handled = true;
Multiline = temp;
}
}

Categories