System.Windows.Forms.TextBoxBase.GetFirstCharIndexFromLine C# (CSharp) Method

GetFirstCharIndexFromLine() public method

public GetFirstCharIndexFromLine ( int lineNumber ) : int
lineNumber int
return int
		public int GetFirstCharIndexFromLine (int lineNumber)
		{
			var lines = Lines;
			if(lines.Length <= lineNumber)
				return -1;
			return Text.IndexOf(lines[lineNumber]);
		}
		//TODO:

Usage Example

Example #1
0
        /// <summary>处理回车,移到行首</summary>
        /// <param name="txt"></param>
        /// <param name="m"></param>
        static void ProcessReturn(TextBoxBase txt, ref String m)
        {
            while (true)
            {
                var p = m.IndexOf('\r');
                if (p < 0)
                {
                    break;
                }

                // 后一个是
                if (p + 1 < m.Length && m[p + 1] == '\n')
                {
                    txt.AppendText(m.Substring(0, p + 2));
                    m = m.Substring(p + 2);
                    continue;
                }

                // 后一个不是\n,移到行首
                if (p >= 0)
                {
                    // 取得最后一行首字符索引
                    var lines = txt.Lines.Length;
                    var last  = lines <= 1 ? 0 : txt.GetFirstCharIndexFromLine(lines - 1);
                    if (last >= 0)
                    {
                        // 最后一行第一个字符,删掉整行
                        txt.Select(last, txt.TextLength - last);
                        txt.SelectedText = null;
                    }
                }

                if (p + 1 == m.Length)
                {
                    m = null;
                    break;
                }
                m = m.Substring(p + 1);
            }
        }