Backspace Detection on KeyDown

I am working on a silverlight web application. It interacts with the module that sends SMS. I want to limit the text to 160 and show a counter. I did it like this:

public partial class SendSMSView { public SendSMSView() { InitializeComponent(); ApplyTheme(); } protected void tbMessage_KeyDown(object sender, KeyEventArgs e) { count = 160 - this.tbMessage.Text.Length; this.lblCount.Content = count.ToString(); } } 

This works fine for all keys except backspace and delete. Of course, this is done like this. I dug up more about this and tried to override the keydown event, so I added the following code snippet:

 public class CustomTextBox : TextBox { public CustomTextBox(): base() { } protected override void OnKeyDown(KeyEventArgs e) { e.handler=false; base.OnKeyDown(e); //this place } } 

In the OnKeyDown function, I get all the registered strokes. Setting Handler to false here does not help, and yet I cannot get backspace to run tbMessage_KeyDow.

I want to somehow call the tbMessage_KeyDow function from // this place from there for the backup space.

I searched the MSDN and found that IsInputKey could be overridden to return true, so that onKeyDown also responds to it, but my framework does not have IsInputKey or PreviewKeyPress. Is there a way around the return key registered as an input key, or to call tbMessage_KeyDow [which is a very rude approach]? Please, help.

+8
c # onkeydown textbox
source share
2 answers

try it....

If you want to find the backspace key on the key pressed in the text box. we would suggest that you can try to do this in the KeyUp event of a text field instead of the KeyDown event. eg:

  <TextBox x:Name="txt" KeyDown="txt_KeyDown" Text="Hello" KeyUp="txt_KeyUp"></TextBox> 

codebehind:

  private void txt_KeyUp(object sender, KeyEventArgs e) { if (e.Key == Key.Back) { MessageBox.Show(this.txt.Text); } } 

or you can do it ... by creating a custom control.

 public partial class Page : UserControl { private TextBox TextBox1; public Page() { InitializeComponent(); TextBox1 = new TextBox(); Width = 300; Height = 100; LayoutRoot.Children.Add(textbox); OnTextChanged(((object)(sender)), ((TextChangedEventArgs)(e))); TextBox1.TextChanged; if (e.Key == Key.Back) { e.Handled = true; } else if (e.Key == Key.Delete) { e.Handled = true; } } } 
+8
source share

I would do something like this (I don't have VS in front of me, so this is pure pseduo code)

 public class SendSMSViewModel : INotifyPropertyChanged { string _text; public string Text { get { return _text; } set { // or allow it and implement IDataErrorInfo to give the user a nifty error message if (value != null & value.Length > 160) return; _text = value; OnPropertyChanged(vm => vm.Text); OnPropertyChanged(vm => vm.NumberOfCharactersRemaining); } } public string NumberOfCharactersRemaining { get { return Text == null ? 160 : 160 - Text.Length; } } } 

.. and then use two-way data binding to your view and remember to use the UpdateSourceTrigger from "PropertyChanged" in your bindings.

0
source share

All Articles