所以我在C#中有一个Windows窗体,只有2个富文本框。
我用作文本编辑器的主框和左边的另一个框作为行号列。一切都很好。我想知道如何使行号列富文本框,如果启用自动换行则不打印行号,如果按下回车键则再次开始打印行号。这应该类似于Notepad ++。
1 line 1
2 line 2
3 line 3
4 line 4 - This is a word wrap line that carries on and on. This is a
word wrap line that carries on and on. word wrap line that carries on
and on. This is a word wrap line that carries on and on.
5 line 5
6 line 6
这是我正在使用的代码
public Form1()
{
InitializeComponent();
}
public void AddLineNumbers()
{
richTextBox1.Select();
Point pt = new Point(0, 0);
int First_Index = richTextBox1.GetCharIndexFromPosition(pt);
int First_Line = richTextBox1.GetLineFromCharIndex(First_Index);
pt.X = ClientRectangle.Width;
pt.Y = ClientRectangle.Height;
int Last_Index = richTextBox1.GetCharIndexFromPosition(pt);
int Last_Line = richTextBox1.GetLineFromCharIndex(Last_Index);
richTextBox2.SelectionAlignment = HorizontalAlignment.Center;
richTextBox2.Text = "";
for (int i = First_Line; i <= Last_Line + 1; i++)
{
richTextBox2.Text += i + 1 + "\n";
}
}
private void Form1_Load(object sender, EventArgs e)
{
AddLineNumbers();
}
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
Point pt = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);
if (pt.X == 1)
{
AddLineNumbers();
}
}
private void richTextBox1_VScroll(object sender, EventArgs e)
{
AddLineNumbers();
}