简体   繁体   English

自定义控件的错误

[英]bug with custom control

I have a custom control that inherits from the textbox control. 我有一个自定义控件,它继承自文本框控件。 It works as expected except for one thing. 除了一件事,它按预期工作。 When a user enters a special character, the cursor returns to the first position of the textbox instead of staying on the position where the special character was stripped out. 当用户输入特殊字符时,光标将返回到文本框的第一个位置,而不是停留在剥离特殊字符的位置。 Any ideas on how to fix this? 有想法该怎么解决这个吗?

Here is the entire code for the control: 这是控件的完整代码:

using System;
using System.Windows.Controls;
using System.Windows.Input;

namespace GamePresenter.XamlControls
{
    class CleanTextBox : TextBox
    {
        #region Constructors
        public CleanTextBox()
        {
            TextChanged += new TextChangedEventHandler(OnTextChanged);
            KeyDown += new KeyEventHandler(OnKeyDown);
        }
        #endregion

        #region Properties
        new public String Text {
            get { return base.Text; }
            set {
                base.Text = LeaveOnlyAlphaNumereicAndSpace(value);
            }
        }
        #endregion

        #region Functions
        private bool isAlphaNumericOrKeypad(Key inKey)
        {
            //It will only enter the if statement if we are NOT dealing with alphanumeric
            if ((inKey < Key.D0 || inKey > Key.D9) && (inKey < Key.A || inKey > Key.Z) && (inKey != Key.Space))
            {
                //Now check keypad keys.
                if (inKey < Key.NumPad0 || inKey > Key.NumPad9)
                {
                    return false;
                }
            }
            return true;
        }

        private bool IsActionKey(Key inKey)
        {
            return inKey == Key.Delete || inKey == Key.Back || inKey == Key.Tab || inKey == Key.Return || Keyboard.Modifiers.HasFlag(ModifierKeys.Alt);
        }

        private string LeaveOnlyAlphaNumereicAndSpace(String inString)
        {
            String tmp = inString;
            foreach (char c in inString.ToCharArray())
            {
                if (!isAlphaNumeric(c))
                {
                    tmp = tmp.Replace(c.ToString(), "");
                }
            }
            return tmp;
        }

        public bool isAlphaNumeric(char c)
        {
            bool validFlag = false;

            if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c == ' '))
            {
                validFlag = true;
            }
            else
            {
                validFlag = false;
            }
            return validFlag;
        }
        #endregion

        #region Event Functions
        protected void OnKeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = !isAlphaNumericOrKeypad(e.Key) && !IsActionKey(e.Key);
        }

        protected void OnTextChanged(object sender, TextChangedEventArgs e)
        {
            base.Text = LeaveOnlyAlphaNumereicAndSpace(Text);
        }
        #endregion
    }
}

There is a simple way to fix your problem, without changing much of your code. 有一种简单的方法可以解决您的问题,而无需更改很多代码。 Reset the caret index as shown below in your textchanged event. 如下所示,在textchanged事件中重置插入符号索引。

 protected void OnTextChanged(object sender, TextChangedEventArgs e)
 {
         //GET YOUR CARET INDEX
         int cIndex = textbox.CaretIndex;

         textbox.Text = LeaveOnlyAlphaNumereicAndSpace(textbox.Text);

         //SET YOUR CARET INDEX
         textbox.CaretIndex = cIndex;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM