3

我的表单(winforms)中的 System.Windows.Forms.TextBox 控件中有大文本,与 2008 相比。

我想找到一个文本,然后选择我找到该文本的行号。

样本,

我有大文本,我发现“ERROR en línea”,我想在文本框多行中选择行号。

string textoLogDeFuenteSQL = @"SQL*Plus: Release 10.1.0.4.2 - Production on Mar Jun 1 14:35:43 2010

版权所有 (c) 1982, 2005, Oracle。版权所有。

******** 更多文字 ********

Conectado a:Oracle 数据库 10g 企业版版本 10.2.0.4.0 - 64 位生产,具有分区、数据挖掘和实际应用程序测试选项

WHERE LAVECODIGO = 'CO_PREANUL'

错误在第 2 行:

ORA-00904: ""LAVECODIGO"": identificador no v?lido

INSERT INTO COM_CODIGOS

错误在第 1 行:

ORA-00001: 限制 única (XACO.INX_COM_CODIGOS_PK) violada";

******** 更多文字 ********

关于它的任何示例代码?

4

2 回答 2

4

您可能想查看TextBoxBase.GetLineFromCharIndex方法。此方法检索文本框中字符位置的行号。

string str = textBox2.Text;

            int index = textBox1.Text.IndexOf(str);

            if (index !=-1)
            {                

              int  lineNo = textBox1.GetLineFromCharIndex(index);
            }

"此方法使您可以根据方法的索引参数中指定的字符索引确定行号。控件中的第一行文本返回值零。GetLineFromCharIndex 方法返回索引字符所在的物理行号位于控件内。”

于 2010-06-02T08:53:31.543 回答
1

编辑:这只会找到搜索文本的出现。要计算行号,请使用Fredrik的答案。

 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);
   }
 }
于 2010-06-02T07:12:20.080 回答