Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Add Progress bar to my Winform (VB.NET 2003)

Posted on 2010-11-16
8
Medium Priority
?
997 Views
Last Modified: 2012-05-10
Hi Experts,

I have a simple Winform with a Listbox & RichTextBox. The Listbox loads RTF files from DIR into Listbox names... If I click on ListboxItem... it loads contents of that file into the RichTextBox.

Because the RFT files are located on a server, some of the files are 11mb.... & this takes time to streamread it into the RichTextBox... I would like to know how to add a progress bar to show loading progress.

Thanks,
Rob
Option Explicit On 
Imports System.IO
Imports System.Threading
Imports System.Diagnostics

Public Class Form1
    Inherits System.Windows.Forms.Form

Dim aDate As Date
Dim dateString As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        LoadFiles()
        Try
            ' folder exists
            Dim path As String
            Dim di As System.IO.DirectoryInfo

            path = "\\server\share\folderA\"
            di = New System.IO.DirectoryInfo(path)

            If (Not di.Exists) Then
                di.Create()
            End If
        Catch ex As Exception
            MsgBox("Error code 22: The Dir doesn't exist!")
        End Try
        Label3.Text = ListBox1.Items.Count.ToString()
    End Sub

    Private Sub LoadFiles()
        Try
            Dim sourceFolder As String = "\\server\share\folderA\"
            ListBox1.BeginUpdate()
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange((New System.IO.DirectoryInfo(sourceFolder)).GetFiles("*.rtf"))
            ListBox1.EndUpdate()
        Catch ex As Exception
            MsgBox("Error!")
        End Try

    End Sub

Private Sub LoadFiles()
        Try
            Dim sourceFolder As String = "\\server\share\folderA\"
            ListBox1.BeginUpdate()
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange((New System.IO.DirectoryInfo(sourceFolder)).GetFiles("*.rtf"))
            ListBox1.EndUpdate()
        Catch ex As Exception
            MsgBox("Error code 45: The repository is empty!")
        End Try

    End Sub

 Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        
	dateString = aDate.Now.ToString("ddMMyyyy")
        Try
            If Not (ListBox1.SelectedItem Is Nothing) Then
                RichTextBox1.Rtf = CType(ListBox1.SelectedItem, System.IO.FileInfo).OpenText.ReadToEnd
            End If
            
            Dim filename As String = IO.Path.GetFileNameWithoutExtension(ListBox1.Text)
            Label1.Text = filename

        Catch ex As Exception
            MsgBox("Error!")
        End Try
     End Sub

Open in new window

0
Comment
Question by:RobertoFreemano
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 17

Accepted Solution

by:
Zhaolai earned 2000 total points
ID: 34146989
Don't think you can use progressbar unless you load the file line by line. But you can add a busy cursor in the process, like this:

Try
            Cursor.Current = Cursors.WaitCursor
            If Not (ListBox1.SelectedItem Is Nothing) Then
                RichTextBox1.Rtf = CType(ListBox1.SelectedItem, System.IO.FileInfo).OpenText.ReadToEnd
            End If
            
            Dim filename As String = IO.Path.GetFileNameWithoutExtension(ListBox1.Text)
            Label1.Text = filename

        Catch ex As Exception
            MsgBox("Error!")
        Finally
            Cursor.Current = Cursors.Default
        End Try

Open in new window

0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 34147889
Perhaps this thread can help you. I answered a similar question recently by suggesting the use of the BackgroundWorker class to accomplish the same thing. Note that a single thread cannot both download the file and update a progress bar, hence the need for one thread to handle the download and the main thread for the progress bar.

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CPP/Q_26569623.html
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34150471
To be able to show progress while reading a file, you need to read the file in chunks.

The other option is to use a Marquee type progressbar then you would have to load the file in a separate thread so that the UI thread can paint the progressbar.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34150815
He's working in VB.Net 2003 so the BackgroundWorker and Marquee style ProgressBar are out...   =\
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34150888
Yeah but he can use threads and a custom progressbar.
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 34151912
@Idle_Mind,
Why do you say BackgroundWorker not an option for VS 2003? I use VS 2003 and the BackgroundWorker class.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34151947
...because it wasn't added until .Net 2.0 in VS2005:
http://msdn.microsoft.com/en-us/library/35f2fe4h.aspx
http://msdn.microsoft.com/en-us/library/t357fb32.aspx

Not sure what you're using in 2003?!...
0
 

Author Closing Comment

by:RobertoFreemano
ID: 34154621
Zhaolai,
Your "Cursor.Current = Cursors.WaitCursor" seems to work for me...

Thanks ;)
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question