c# - 如何在 Silverlight 中创建数字文本框?

标签 c# .net silverlight textbox silverlight-2.0

正如标题所言。 我看过从 TextBox 继承,但唯一合理的覆盖是“OnKeyDown”,但这只是从 Key 枚举中给了我一个键(无法使用 Char.IsNumeric())。

最佳答案

我接受了 Nidhal 建议的答案并对其进行了一些编辑以处理数字上方字符的移位情况(即 !@#$%^&*()),因为该解决方案仍将允许文本框中的这些字符。

private void NumClient_KeyDown(object sender, KeyEventArgs e)
{       
    // Handle Shift case
    if (Keyboard.Modifiers == ModifierKeys.Shift)
    {
       e.Handled = true;
    }

    // Handle all other cases
    if (!e.Handled && (e.Key < Key.D0 || e.Key > Key.D9))
    {
        if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9)
        {
            if (e.Key != Key.Back)
            {
                e.Handled = true;
            }
        }
    }           
}

关于c# - 如何在 Silverlight 中创建数字文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/268207/

相关文章:

C# Dictionary<T,K> 单写多读线程安全

c# - Silverlight 2 - 下载文件 - IE Blocks 文件下载

silverlight - 部署 Silverlight PivotViewer 的问题

c# - 为什么当我执行 BeginGetRequestStream 时会收到 ProtocolViolationException

c# - Entity Framework 渴望加载多级异常

c# - 使用泛型创建可重用的 Control 基类

.net - 使用线程池和普通线程有什么区别?

c# - "Operation not implemented yet"错误

c# - winform 的动态代码编译在 C# 中给出错误

c# - Entity Framework 、SQLite 和延迟加载