Question

I have large text in System.Windows.Forms.TextBox control in my form (winforms), vs 2008.

I want find a text, and select the line number where I've found that text.

Sample,

I have fat big text, and I find "ERROR en línea", and I want select the line number in textbox multiline.

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

Copyright (c) 1982, 2005, Oracle. All rights reserved.

******** MORE TEXT ************

Conectado a: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production With the Partitioning, Data Mining and Real Application Testing options

WHERE LAVECODIGO = 'CO_PREANUL'

ERROR en línea 2:

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

INSERT INTO COM_CODIGOS

ERROR en línea 1:

ORA-00001: restricción única (XACO.INX_COM_CODIGOS_PK) violada";

******** MORE TEXT ************

Any sample code about it ?

Was it helpful?

Solution

You might want to look at TextBoxBase.GetLineFromCharIndex method. This method retrieves the line number of character position within the textbox.

string str = textBox2.Text;

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

            if (index !=-1)
            {                

              int  lineNo = textBox1.GetLineFromCharIndex(index);
            }

"This method enables you to determine the line number based on the character index specified in the index parameter of the method. The first line of text in the control returns the value zero. The GetLineFromCharIndex method returns the physical line number where the indexed character is located within the control."

OTHER TIPS

EDIT: This only finds the occurrences of the searched text. To compute the line numbers use Fredrik's answer.

 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);
   }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top