How can I Optimize Loop through Lines of MultiLine Textbox

I want to loop each line of multiline textbox so I can check if all li

Page 1 of 1

7 Replies - 16511 Views - Last Post: 02 December 2009 - 09:35 AM Rate Topic: ***-- 2 Votes

#1 RuleS  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-March 08

How can I Optimize Loop through Lines of MultiLine Textbox

Posted 11 October 2008 - 12:27 AM

Hi All,

I 'm currently developing program which I can check the lines of each textbox if it has same length. The code is working properly in few lines like 2000 lines, but when I try to load a textfile which has 50,000 lines, my program is not responding anymore it takes forever. Is there anything you could help me to make it faster? Pls. its the only logic I could think of.

Private Sub LineChecker()
		Dim counter As Integer = TextBox1.Lines.Length - 1
		Dim intArray(counter) As Integer
		Dim iLine, i As Integer
		For iLine = 0 To counter - 1
			If TextBox1.Lines(iLine).Length <> 534 Then
				listbox1.items.add(iLine)
			End If
		Next iLine
		
		If iLine = TextBox1.Lines.Length Then
			MsgBox("Checking lines complete")
		End If
	End Sub



Any help I will appreciate.

Thanks,
Ruel

This post has been edited by RuleS: 11 October 2008 - 12:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How can I Optimize Loop through Lines of MultiLine Textbox

#2 Damage  Icon User is offline

  • Lord of Schwing
  • member icon

Reputation: 53
  • View blog
  • Posts: 1,186
  • Joined: 05-June 08

Re: How can I Optimize Loop through Lines of MultiLine Textbox

Posted 11 October 2008 - 04:54 AM

dude, 50,000 lines is always going to take some time, no matter what you do
Was This Post Helpful? 0
  • +
  • -

#3 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: How can I Optimize Loop through Lines of MultiLine Textbox

Posted 11 October 2008 - 12:45 PM

You can make this much faster but show us how you are loading the textfile into the textbox first.
Was This Post Helpful? 0
  • +
  • -

#4 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 606
  • Joined: 01-October 08

Re: How can I Optimize Loop through Lines of MultiLine Textbox

Posted 12 October 2008 - 05:57 AM

i have found that RichTextBox is faster than TextBox.
Was This Post Helpful? 0
  • +
  • -

#5 RuleS  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-March 08

Re: How can I Optimize Loop through Lines of MultiLine Textbox

Posted 12 October 2008 - 07:04 PM

Hi Dude,
I load my textfile with copying the content of text file and paste it in textbox control or with this code:
 Dim fName As String
 Dim objStreamReader As StreamReader

Private Sub FileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileOpen.Click
		Try
			If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
				fName = OpenFileDialog1.FileName
				objStreamReader = New StreamReader(OpenFileDialog1.OpenFile)
				TextBox1.AppendText(objStreamReader.ReadToEnd)
			End If
			'End If
		Catch ex As Exception
			MsgBox(ex.Message)
		End Try
	End Sub



I know that 50,000 lines would take time, but as I have observed, when I run this application and fire the event, it take more than 3 hours and still processing......I need to check the lines for at most 30 minutes.

Thanks,
RuleS
Was This Post Helpful? 0
  • +
  • -

#7 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: How can I Optimize Loop through Lines of MultiLine Textbox

Posted 13 October 2008 - 12:13 PM

The textbox control is not meant to handle that amount of data, using the richtextbox control as well as checking for the failed lines as we read from the datafile the code below should accomplish the same task in under 5 seconds. Depending on your needs a cleaner look may be to highlight the lines in the richtextbox that dont meet you length requirements instead of placing them in a list box.

You have to be very careful when manipulating strings in .NET as they are a value type object and almost everytime you access them .Net creates a copy of the value type, that is why .NET has a stringbuilder class, I tried to demonstrate a good way to use it below.

 Try
            Dim OpenFileDialog As New OpenFileDialog
            If OpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then

                Using fileStreamReader = New StreamReader(OpenFileDialog.FileName)
                    Dim fileStringBuilder As New System.Text.StringBuilder
                    Dim failedLines As New List(Of String)
                    Dim readLine As String

                    Do Until fileStreamReader.EndOfStream
                        'Read a line from file
                        readLine = fileStreamReader.ReadLine
                        'Append line to string builder
                        fileStringBuilder.AppendLine(readLine)
                        'Check if line meets conditions
                        If readLine.Length > 50 Then
                            'Add failed line to string collection
                            failedLines.Add(readLine)
                        End If
                    Loop

                    'Populate richtextbox from string builder
                    RichTextBox1.Text = fileStringBuilder.ToString

                    'Append failed lines to listbox
                    ListBox1.BeginUpdate()
                    ListBox1.Items.AddRange(failedLines.ToArray)
                    ListBox1.EndUpdate()
                End Using
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


Was This Post Helpful? 1

#8 RuleS  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-March 08

Re: How can I Optimize Loop through Lines of MultiLine Textbox

Posted 15 October 2008 - 05:31 PM

Thank you very much MagicMonkey, I tried your code and really it is much faster. It really ease my work.
Was This Post Helpful? 0
  • +
  • -

#9 gwalkeriq  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-December 09

Re: How can I Optimize Loop through Lines of MultiLine Textbox

Posted 02 December 2009 - 09:35 AM

Actually, your terribly performance is directly related to the implements of the TextBox, iterating directly through each line of a large textbox is horribly slow. However, there is a simple and CPU efficient work-around that does not require you to restructure code.

The trick is simply to declare a array of strings and assign it using

lines = TextBox1.Lines

Then use the lines array -- incredible performance difference.

Iterating through TextBox1.Lines is O[n^2]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1