Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 131,733 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,214 people online right now. Registration is fast and FREE... Join Now!




How can I Optimize Loop through Lines of MultiLine Textbox

 
Reply to this topicStart new topic

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

Rating  5
RuleS
post 10 Oct, 2008 - 11:27 PM
Post #1


New D.I.C Head

*
Joined: 28 Mar, 2008
Posts: 5

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.

CODE

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: 10 Oct, 2008 - 11:34 PM
User is offlineProfile CardPM

Go to the top of the page

Damage
post 11 Oct, 2008 - 03:54 AM
Post #2


D.I.C Addict

Group Icon
Joined: 5 Jun, 2008
Posts: 728



Thanked 7 times

Dream Kudos: 75
My Contributions


dude, 50,000 lines is always going to take some time, no matter what you do
User is offlineProfile CardPM

Go to the top of the page

magicmonkey
post 11 Oct, 2008 - 11:45 AM
Post #3


D.I.C Regular

***
Joined: 12 Sep, 2008
Posts: 374



Thanked 64 times
My Contributions


You can make this much faster but show us how you are loading the textfile into the textbox first.
User is online!Profile CardPM

Go to the top of the page

dbasnett
post 12 Oct, 2008 - 04:57 AM
Post #4


D.I.C Head

**
Joined: 1 Oct, 2008
Posts: 131



Thanked 5 times
My Contributions


i have found that RichTextBox is faster than TextBox.
User is online!Profile CardPM

Go to the top of the page

RuleS
post 12 Oct, 2008 - 06:04 PM
Post #5


New D.I.C Head

*
Joined: 28 Mar, 2008
Posts: 5

Hi Dude,
I load my textfile with copying the content of text file and paste it in textbox control or with this code:
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
User is offlineProfile CardPM

Go to the top of the page

magicmonkey
post 13 Oct, 2008 - 11:13 AM
Post #6


D.I.C Regular

***
Joined: 12 Sep, 2008
Posts: 374



Thanked 64 times
My Contributions


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.

vb

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
User is online!Profile CardPM

Go to the top of the page

RuleS
post 15 Oct, 2008 - 04:31 PM
Post #7


New D.I.C Head

*
Joined: 28 Mar, 2008
Posts: 5

Thank you very much MagicMonkey, I tried your code and really it is much faster. It really ease my work.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 10:41AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month