代码之家  ›  专栏  ›  技术社区  ›  Luke Foust

Silverlight将PlatformKeycode转换为字符代码

  •  2
  • Luke Foust  · 技术社区  · 15 年前

    问题:

    在silverlight中,有没有一种好的方法可以阻止不需要的字符被输入到文本框中?

    背景:

    我有一个允许用户输入文件名的文本框。我想从文本框中排除无效的文件字符。其中几个字符是:

    • “?”
    • ''
    • “&”
    • “&”

    尽管silverlight文本框类不支持按键事件,但它确实有一个keydown和keyup事件,可用于在将键输入文本框时检索字符信息。它将这些作为 钥匙 枚举,或者它可以返回 平台密钥码

    当然,键的范围更大/不同于字符的范围-“f键”就是一个例子。然而,在windows窗体中出现类似于按键事件的事件,表明能够提取特定字符信息是有用的。

    为了证明可以工作,我将平台的不需要的字符的platformkeycode值硬编码到事件处理程序中,然后一切都工作了……但是 当然 这只是我的讲台。我需要确保这个实现与平台无关。下面是演示我希望它如何工作的代码:

        private void theText_KeyDown(object sender, KeyEventArgs e)
        {
            int[] illegals = { 191, 188, 190, 220, 186, 222, 191, 56, 186};
            if (illegals.Any(i => i == e.PlatformKeyCode)) e.Handled = true;
        }
    
    2 回复  |  直到 14 年前
        1
  •  2
  •   Community CDub    8 年前

    两份回复(在评论中) 亨利克 约翰内斯 包含场景的最佳答案。silverlight中的规范方法不是像windows窗体程序员那样思考并捕获特定的键事件,而是使用textchanged事件从文本框中删除不需要的字符。一 similar question 在文本框中只允许数字输入时,会提示使用的解决方案。

    下面的代码示例了我在阅读完注释后所采用的方法,它可以很好地删除不适合输入的字符:

        private void fileNameTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            string illegalChars = @"?<>:""\/*|";
            fileNameTextBox.Text = String.Join("", fileNameTextBox.Text.Split(illegalChars.ToCharArray()));
            fileNameTextBox.SelectionStart = fileNameTextBox.Text.Length;
        }
    
        2
  •  2
  •   Bryan    14 年前
    using System;
    using System.Linq;
    using System.Text.RegularExpressions;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Interactivity;
    
    public class FilterTextBoxBehavior : Behavior<TextBox>
    {
        public readonly static DependencyProperty AllowAlphaCharactersProperty = DependencyProperty.Register("AllowAlphaCharacters", typeof(bool), typeof(FilterTextBoxBehavior), new PropertyMetadata(true));
        public bool AllowAlphaCharacters
        {
            get { return (bool)GetValue(AllowAlphaCharactersProperty); }
            set { SetValue(AllowAlphaCharactersProperty, value); }
        }
    
        public readonly static DependencyProperty AllowNumericCharactersProperty = DependencyProperty.Register("AllowNumericCharacters", typeof(bool), typeof(FilterTextBoxBehavior), new PropertyMetadata(true));
        public bool AllowNumericCharacters
        {
            get { return (bool)GetValue(AllowNumericCharactersProperty); }
            set { SetValue(AllowNumericCharactersProperty, value); }
        }
    
        public readonly static DependencyProperty AllowSpecialCharactersProperty = DependencyProperty.Register("AllowSpecialCharacters", typeof(bool), typeof(FilterTextBoxBehavior), new PropertyMetadata(true));
        public bool AllowSpecialCharacters
        {
            get { return (bool)GetValue(AllowSpecialCharactersProperty); }
            set { SetValue(AllowSpecialCharactersProperty, value); }
        }
    
        public readonly static DependencyProperty DoNotFilterProperty = DependencyProperty.Register("DoNotFilter", typeof(string), typeof(FilterTextBoxBehavior), new PropertyMetadata(default(string)));
        public string DoNotFilter
        {
            get { return (string)GetValue(DoNotFilterProperty); }
            set { SetValue(DoNotFilterProperty, value); }
        }
    
        protected override void OnAttached()
        {
            base.OnAttached();
            if (AssociatedObject == null) { return; }
    
            FilterAssociatedObject();
            AssociatedObject.TextChanged += OnTextChanged;
        }
    
        protected override void OnDetaching()
        {
            base.OnDetaching();
            if (AssociatedObject == null) { return; }
    
            FilterAssociatedObject();
            AssociatedObject.TextChanged -= OnTextChanged;
        }
    
        private void OnTextChanged(object sender, TextChangedEventArgs e) { FilterAssociatedObject(); }
        private void FilterAssociatedObject()
        {
            int cursorLocation = AssociatedObject.SelectionStart;
    
            for (int i = AssociatedObject.Text.Length - 1; i >= 0; i--)
            {
                char c = AssociatedObject.Text[i];
                if (ValidChar(c)) { continue; }
    
                AssociatedObject.Text = AssociatedObject.Text.Remove(i, 1);
                cursorLocation--;
            }
    
            AssociatedObject.SelectionStart = Math.Min(AssociatedObject.Text.Length, Math.Max(0, cursorLocation));
        }
    
        private bool ValidChar(char c)
        {
            if (!string.IsNullOrEmpty(DoNotFilter) && DoNotFilter.Contains(c)) { return true; }
            if (!AllowAlphaCharacters && char.IsLetter(c)) { return false; }
            if (!AllowNumericCharacters && char.IsNumber(c)) { return false; }
            if (!AllowSpecialCharacters && Regex.IsMatch(c.ToString(), @"[\W|_]")) { return false; }
    
            return true;
        }
    }