在c# 自定义控件TextBox里,如何在DELETE之后光标停在删除位置(我遇到的问题是DELETE之后光标自动跳到末尾去了)?
跪求高人解决!
谢谢!

解决方案 »

  1.   

    namespace MoneyText
    {
        public partial class MoneyTextBox : TextBox
        {
            public MoneyTextBox()
            {
                InitializeComponent();
            }        private int _maxByteLength = 32767;        private Boolean _checkMinusDataEnable = true;        private Color _minusDataColor = Color.Red;        private Boolean _changeForeColorEnable = true;        private Color _customForeColor = Color.Red;        private Boolean _changeBackColorEnable = false;        private Color _customBackColor = Color.FromArgb(255, 255, 128);        public Color MinusDataColor
            {
                get
                {
                    return _minusDataColor;
                }
                set
                {
                    _minusDataColor = value;
                }
            }        public Boolean CheckMinusDataEnable
            {
                set
                {
                    _checkMinusDataEnable = value;
                }
            }
            public int MaxByteLength
            {
                get
                {
                    return _maxByteLength;
                }
                set
                {
                    _maxByteLength = value;
                }
            }        public Boolean ChangeBackColorEnable
            {
                set
                {
                    _changeBackColorEnable = value;
                }
            }        public Color CustomForeColor
            {
                get
                {
                    return _customForeColor;
                }
                set
                {
                    _customForeColor = value;
                }
            }
            public Color CustomBackColor
            {
                get
                {
                    return _customBackColor;
                }
                set
                {
                    _customBackColor = value;
                }
            }
            public Boolean ChangeForeColorEnable
            {
                set
                {
                    _changeForeColorEnable = value;
                }
            }
      

  2.   

    在删除的事件里:控件.Focus()
      

  3.   

    LZ贴这么大段属性代码上来我猜测是你在KeyDown时判断Del键按下,然后重新设置了TextBox的Text属性,这个时候你需要通过SelectionStart属性来设置光标的位置
      

  4.   

    主要是这样的问题:在这个自定义的TextBox控件里,当输入半角的数字金额(在最前面可以带半角负号)时,每三位要自动用半角逗号隔开。这就造成一个问题:当用DELETE删除的光标位于逗号前面时,它就不能进行删除操作(而是跳到数据最末尾了)。这个问题让我很郁闷啊!希望各位大虾帮我解决一下。
      

  5.   

    很奇怪的现象,我遇到一个类似的问题,就是TxtBox中只输入数据,如果用全角输入1234,框中显示半角的4321,至今没解决,我想这两个问题的机制类似吧?
      

  6.   

    楼主你贴这些属性看不出来啊,你应该是在keypress或其他什么事件里修改的textbox的文本吧。
    如果等用户输入完成后,在textchange事件加千位分隔。用户做修改删除都没问题,等操作完成后才处理。
    贴你的事件处理代码看看吧。
      

  7.   

      private string ToMoneyFormat(Decimal strMoneyTextBox)
            {
                return strMoneyTextBox.ToString("#,#");
            }        private void MoneyTextBox_TextChanged(object sender, EventArgs e)
            {
                this.SelectionStart = this.Text.Length;
                Check();
                string money = this.Text.ToString();            if (this.Text.Length > 1)
                {
                    this.Text = ToMoneyFormat(Convert.ToDecimal(this.Text));
                }
                if (System.Text.Encoding.GetEncoding(54936).GetByteCount(money) > 32767)
                //if (Convert.ToDecimal(System.Text.Encoding.Unicode.GetCharCount(money)) > 32767)
                {
                    this.Enabled = false;
                }
            }        private void MoneyTextBox_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (this.Text == "0")
                {
                    this.Text = "";
                }            if (e.KeyChar > 31 && e.KeyChar != 45 && e.KeyChar < '0' || e.KeyChar > '9')
                {
                    if (this.Text.Length == 0)
                    {
                        this.ForeColor = Color.Black;
                        this.BackColor = Color.White;
                        this.Text = "0";
                    }
                    e.Handled = true;
                }
                else
                {
                    this.ForeColor = CustomForeColor;
                    this.BackColor = CustomBackColor;
                }
                if ((e.KeyChar == 45 && this.SelectionStart != 0))
                {
                    e.Handled = true;
                }
            }        private void MoneyTextBox_KeyUp(object sender, KeyEventArgs e)
            {
                Check();
                if (this.Text.Length == 0)
                {
                    this.Text = "0";
                }
            }        private void Check()
            {            this.ForeColor = CustomForeColor;
                this.BackColor = CustomBackColor;
                if (this.Text == "" || this.Text == "0")
                {
                    this.ForeColor = Color.Black;
                    this.BackColor = Color.White;
                }
                else
                {
                    try
                    {
                        if (this.Text.ToString().StartsWith(","))
                        {
                            this.Text = this.Text.Substring(1, this.Text.Length - 1);
                        }
                        if (this.Text != "-")
                        {
                            Convert.ToDecimal(this.Text);
                        }                }
                    catch
                    {
                        this.ForeColor = Color.Black;
                        this.BackColor = Color.White;
                        this.Text = "0";
                    }
                }
            }        protected override void WndProc(ref Message m)
            {
                MaxLength = this._maxByteLength;
                const int mPaste = 0x0302;
                if (m.Msg == mPaste)
                {
                    IDataObject iData = Clipboard.GetDataObject();         //取剪贴板对象
                    if (iData.GetDataPresent(DataFormats.Text))            //判断是否是Text
                    {
                        string text = (string)iData.GetData(DataFormats.Text);//取数据
                        char[] str = text.ToCharArray();
                        if (CheckByteLengthFlow(text))
                        {
                            int len = GetByteLength(text);                  //输入的字符的长度
                            int tlen = GetByteLength(this.Text);            //文本框原有文本的长度
                            int slen = GetByteLength(this.SelectedText);    //文本框选中文本的长度
                            char[] c = text.ToCharArray();
                            int g;
                            int f = 0;
                            int count = 0;
                            for (g = 0; g < c.Length; g++)
                            {
                                count += GetByteLength(c[g].ToString());
                                if (_maxByteLength < (tlen - slen + count))
                                { f = g; break; }
                            }
                            int j = this.Text.ToCharArray().Length;                        int k = this.SelectedText.ToCharArray().Length;
                            this.MaxLength = j + f - k;
                            //}
                        }
                    }
                }
                base.WndProc(ref m);
            }        private bool CheckByteLengthFlow(string text)
            {
                int len = GetByteLength(text);                              //输入的字符的长度
                int tlen = GetByteLength(this.Text);                        //文本框原有文本的长度
                int slen = GetByteLength(this.SelectedText);                //文本框选中文本的长度
                return (MaxLength < len + tlen - slen);
            }        private int GetByteLength(string vtext)
            {
                return System.Text.Encoding.GetEncoding("shift_jis").GetByteCount(vtext);
            }
      

  8.   

    http://msdn.microsoft.com/zh-cn/developercenters