简体   繁体   中英

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.

 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;
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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