TextBox中,如何判断当前光标所在的索引位置?

时间:2022-02-06 10:31:15
比如:
TextBox.Text = "abcdefg"

问题:
区别 <光标正在字母b后面的位置> 和 <光标正在字母f后面的位置> ,
用代码该怎么表达?

谢谢!

 

5 个解决方案

#1


this.textBox1.SelectionStart;

#2


  if (ctls.GetType().Name == txt)
                {
                    TextBox tb = new TextBox();
                  
                    try
                    {
                        
                        tb = (TextBox)fr.Controls.Find(ctls.Name, true)[0];
                        
                        if (lstControl.IndexOf(tb) < 0)
                        {
                            lstControl.Add(tb);
                            if (tb.Text.Trim().IndexOf(textBox1.Text.Trim()) >= 0)
                            {
                                tb.Focus();
                                int start = tb.Text.IndexOf(textBox1.Text.Trim());
                                int end = textBox1.Text.Trim().Length;
                               
                                tb.Select(start, end);
                                return 1;
                            }
                        }
                        else
                        {
                            return 0;
                        }
                    }
                    catch (Exception ex)
                    { }
                }
里面找!

#3


该回复于2010-11-16 13:51:51被版主删除

#4


LZ试试这个:

this.textBox1.Focus();
            string text = textBox1.Text;
            int cursorIndex = this.textBox1.SelectionStart;

            StringBuilder sbInfo = new StringBuilder("Cursor is ");
            if (cursorIndex <= 0)
            {
                sbInfo.Append("at the beginning of the sentence, ");
            }
            else
            {
                char charBeforeCursor = text[cursorIndex - 1];
                sbInfo.Append(string.Format("behind letter '{0}', ", charBeforeCursor));
            }

            if (cursorIndex >= text.Length) 
            {
                sbInfo.Append("and at the ending of the sentence.");
            }
            else
            {
                char charAfterCursor = text[cursorIndex];
                sbInfo.Append(string.Format("and before letter '{0}'", charAfterCursor));
            }

            MessageBox.Show(sbInfo.ToString());

#5


 TextBox.SelectionStart  // 光标所在位置索引

#1


this.textBox1.SelectionStart;

#2


  if (ctls.GetType().Name == txt)
                {
                    TextBox tb = new TextBox();
                  
                    try
                    {
                        
                        tb = (TextBox)fr.Controls.Find(ctls.Name, true)[0];
                        
                        if (lstControl.IndexOf(tb) < 0)
                        {
                            lstControl.Add(tb);
                            if (tb.Text.Trim().IndexOf(textBox1.Text.Trim()) >= 0)
                            {
                                tb.Focus();
                                int start = tb.Text.IndexOf(textBox1.Text.Trim());
                                int end = textBox1.Text.Trim().Length;
                               
                                tb.Select(start, end);
                                return 1;
                            }
                        }
                        else
                        {
                            return 0;
                        }
                    }
                    catch (Exception ex)
                    { }
                }
里面找!

#3


该回复于2010-11-16 13:51:51被版主删除

#4


LZ试试这个:

this.textBox1.Focus();
            string text = textBox1.Text;
            int cursorIndex = this.textBox1.SelectionStart;

            StringBuilder sbInfo = new StringBuilder("Cursor is ");
            if (cursorIndex <= 0)
            {
                sbInfo.Append("at the beginning of the sentence, ");
            }
            else
            {
                char charBeforeCursor = text[cursorIndex - 1];
                sbInfo.Append(string.Format("behind letter '{0}', ", charBeforeCursor));
            }

            if (cursorIndex >= text.Length) 
            {
                sbInfo.Append("and at the ending of the sentence.");
            }
            else
            {
                char charAfterCursor = text[cursorIndex];
                sbInfo.Append(string.Format("and before letter '{0}'", charAfterCursor));
            }

            MessageBox.Show(sbInfo.ToString());

#5


 TextBox.SelectionStart  // 光标所在位置索引