上的KeyDown检测Backspace键 [英] Detection of Backspace on KeyDown

查看:280
本文介绍了上的KeyDown检测Backspace键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Silverlight Web应用程序。它又与发送短信的一个模块。我想在文本限制到160,并显示一个计数器。我这样做是这样的:

I am working on a silverlight web app. It interacts with a module that sends SMS's. 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();
    }
}

这工作正常的所有​​键,除了退格键和删除。当然,它是由的功能就像这样。我挖了这个,并试图重写keydown事件,所以我增加了以下code片断:

This works fine for all the keys except backspace and delete. Of course it is made to function like this. i dug more on this and tried overriding 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
    }
}

在的onkeydown功能我得到的所有的击键记录。处理程序设置为false,在这里犯规帮助,我还是不能让退格键来触发tbMessage_KeyDow。

In OnKeyDown function i get all the key strokes registered. Setting Handler to false here doesnt help and still i cant get backspace to trigger tbMessage_KeyDow.

我想以某种方式调用tbMessage_KeyDow函数从//这个地方强行从那里退格键。

I want to somehow call the tbMessage_KeyDow function from //this place forcefully from there for backspace.

我搜索MSDN,并发现IsInputKey可以被重写,这样的onkeydown响应它,以及返回true,但我的框架,既没有IsInputKey也不previewKey preSS。有一种解决方法获取backspace键注册为输入键,或调用tbMessage_KeyDow [这是非常粗糙的方法]?请大家帮帮忙。

I searched MSDN, and found that IsInputKey can be overriden to return true so that onKeyDown responds to it as well, but My framework neither has IsInputKey nor PreviewKeyPress. Is there a workaround for getting backspace key registered as input key, or to call tbMessage_KeyDow [which is very crude approach] ? Please help.

推荐答案

试试这个......

try this ....

。我们建议你可以尝试做在文本框中的KeyUp事件,而不是KeyDown事件。 例如:

If you want to detect the backspace key on the key pressed in a textbox. we would suggest that you can try to do in the textbox's KeyUp event instead of the KeyDown event. for example:

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

在codebehind:

the codebehind:

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

或者你可以做这样的...通过创建一个用户控件....

or you can do like this...by creating a user 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;
        }
    }
}

这篇关于上的KeyDown检测Backspace键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆