I use the KeyUp and MouseUp events to trigger a quick lookup to show the current line number where the insertion point is in a multiline textbox. In the following example, my multiline textbox is called txt_FileContents and I wrote a single method (UpdateCurrentLineNumber) that fetches the current line number, the total number of lines in the textbox, and then updates a label (lbl_LineNumber) to show the information. Happy coding!
[csharp]
void txt_FileContents_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
UpdateCurrentLineNumber();
}
void txt_FileContents_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
UpdateCurrentLineNumber();
}
private void UpdateCurrentLineNumber()
{
int index = this.txt_FileContents.SelectionStart;
int line = this.txt_FileContents.GetLineFromCharIndex(index) + 1;
lbl_LineNumber.Text = ” Ln ” + line.ToString(“N0″) + ” / ” + this.txt_FileContents.Lines.Count().ToString(“N0”);
}
[/csharp]