[英]Get TextBox line number
我想在C#
winform應用程序中獲取一個TextBox
行號。 但與這個問題不同的是,我想要真正的行號,無論是換行還是帶有\\r\\n
的新行。
var lines = tb.Lines.Count();
只會得到帶有回車的行號。
那么我怎樣才能得到我正在尋找的東西呢?
所以我在這里找到了答案
我必須創建我自己的TextBox
版本,如下所示:
using System;
using System.Windows.Forms;
namespace TextBoxLines
{
public class TextBoxEx : TextBox
{
private const int EM_GETLINECOUNT = 0xBA;
private const int EM_LINEINDEX = 0xBB;
private const int EM_LINELENGTH = 0xC1;
public TextBoxEx() :base()
{
//Since we'll want multiple lines allowed we'll add that to the constructor
this.Multiline = true;
this.ScrollBars = ScrollBars.Vertical;
}
public int LineCount
{
get
{
Message msg = Message.Create(this.Handle, EM_GETLINECOUNT, IntPtr.Zero, IntPtr.Zero);
base.DefWndProc(ref msg);
return msg.Result.ToInt32();
}
}
public int LineIndex(int Index)
{
Message msg = Message.Create(this.Handle, EM_LINEINDEX, (IntPtr)Index, IntPtr.Zero);
base.DefWndProc(ref msg);
return msg.Result.ToInt32();
}
public int LineLength(int Index)
{
Message msg = Message.Create(this.Handle, EM_LINELENGTH, (IntPtr)Index, IntPtr.Zero);
base.DefWndProc(ref msg);
return msg.Result.ToInt32();
}
}
}
只是美麗。 現在,您已將它們內置到TextBox中。 就像使用TextBox的任何屬性一樣使用它們並獲取TextBox中顯示的行數 - 如果它們是換行或實際行則無關緊要。
我不明白您想要什么,但通常您可以使用此代碼來計算文本框中的行數:
public int LineNumber()
{
int LineNumber;
LineNumber = textBoxNotePad.Lines.Length;
return LineNumber;
}
我希望它有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.