如何在文本框中选择行号
题
我在system.windows.forms.forms.textbox控件(Winforms)中有大文本(vs 2008)。
我想找到一个文本,然后选择我找到该文本的行号。
样本,
我有胖大的文字,我发现“错误enlínea”,我想在文本框Multiline中选择行号。
string textoLogDeFuenteSQL = @"SQL*Plus: Release 10.1.0.4.2 - Production on Mar Jun 1 14:35:43 2010
版权(c)1982,2005,甲骨文。版权所有。
********更多文字************
Conectado A:Oracle数据库10G Enterprise Edition版本10.2.0.4.0-64位生产,带有分区,数据挖掘和真实的应用程序测试选项
WHERE LAVECODIGO = 'CO_PREANUL'
错误enlínea2:
ORA-00904:“” lavecodigo“”:disideficador no v?lido
INSERT INTO COM_CODIGOS
错误enlínea1:
ORA-00001:restricciónúnica(xaco.inx_com_codigos_pk)vialada;
********更多文字************
关于它的示例代码吗?
解决方案
你可能想看看 textboxbase.getline fromcharindex 方法。此方法检索文本框中字符位置的行数。
string str = textBox2.Text;
int index = textBox1.Text.IndexOf(str);
if (index !=-1)
{
int lineNo = textBox1.GetLineFromCharIndex(index);
}
“此方法使您能够根据该方法的索引参数中指定的字符索引确定行号。控件中的第一行文本返回值零。getlineFromcharindex方法返回索引字符的物理行编号位于控件内。”
其他提示
编辑: 这只会发现搜索文本的出现。计算行号使用 弗雷德里克的答案。
using System.Text.RegularExpressions;
public static void FindErrorInText(string input)
{
Regex rgx = new Regex("ERROR en linea \d*", RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(input);
if (matches.Count > 0)
{
Console.WriteLine("{0} ({1} matches):", input, matches.Count);
foreach (Match match in matches)
Console.WriteLine(" " + match.Value);
}
}
不隶属于 StackOverflow