• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

Add progress Bar in vb.net program

I have added the progress bar into my function how ever i dont know how change value efficiently. pls help
My porgram stroring one million records in dataset and the process will take a long time and need to progressbar..

    Private Sub btnExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExcel.Click

        Dim filename As String
        filename = OpenExcel.FileName
        Call MoveNoMatch(filename, ProgressBar1)

    End Sub
    Private Function MoveNoMatch(ByVal FileName As String, ByVal progressbar As ProgressBar) 'Now a function that takes file path for the XL FIle
        progressbar.Value = 100
        Dim DT As New DataTable
        Dim ConnectionString As SqlConnection = New SqlConnection("Server=;User id=sa;pwd=xyz;Database=abc")
        Dim DA As New SqlDataAdapter("Select * From pqr", ConnectionString)

        DA.Fill(DT)   ' Fill the DataTable from SQL so we need not Query the DB again and agian

        Dim XL As New Excel.Application
        Dim CurrWS As Excel.Worksheet
        Dim NewWS As Excel.Worksheet
        ' CurrWS=XL.ActiveSheet 'Now since the same file is used again and again the Active Sheet might not be the same so I am assuming a Sheet called SERIAL is having the data to match
        CurrWS = XL.Sheets("PIDKEY")
        For Each ws As Excel.Worksheet In XL.Worksheets
            If ws.Name = "NOMATCH" Then
                NewWS = ws 'If sheet already there get a reference
                Exit For
            End If

        If NewWS Is Nothing Then
            NewWS = XL.Worksheets.Add ' Add a new sheet if it did not exist earlier
            NewWS.Name = "NOMATCH"
        End If

        Dim Serial As String
        For i As Integer = CurrWS.UsedRange.Rows.Count To 2 Step -1
            Serial = CurrWS.Cells(i, "B").Value
            If DT.Select("SerialNo='" & Serial & "'").Length <= 0 Then
                'If there is no Matching Row in the DB for this Serial number move the row to the new Sheet
                CurrWS.Cells(i, "B").EntireRow.Copy() 'Copy the Row
                NewWS.Cells(NewWS.UsedRange.Rows.Count + 2, "B").EntireRow.Select()  'Go to the Last Row
                NewWS.Paste()  'Paste it
                CurrWS.Cells(i, "B").EntireRow.Delete() ' Now delete row in original sheet
            End If
        XL = Nothing
    End Function
  • 5
  • 3
1 Solution
My FlashScreen class with Progress procedure:
    Public Sub Progress(ByVal nProgressPct As Integer, ByVal strMessage As String)
        Me.ProgressBar.Value() = nProgressPct

        Me.lblStatus.Text = Me.lblStatus.Text & strMessage & vbLf & vbCr

        If nProgressPct = 100 Then
            Me.lblStatus.Text = Me.lblStatus.Text & "Done." & "/n/r"
        End If

    End Sub

Get access to FlashScreen.Progress to handle progress updates:
        Dim objLoader As LYRO.Loader = New Loader

        Dim frmFlash As FlashScreen = New FlashScreen

        AddHandler objLoader.evtLoading, AddressOf frmFlash.Progress

Update calls:
        RaiseEvent evtLoading(0, "Initializing loading sequence...")
            i = i + 1
       RaiseEvent evtLoading(i / strFileNameArr.Length * 100, "Loading " & Path.GetFileName(strFileName))
nyee84Author Commented:
Could you advice me how to  use you code in my pro...

   <STAThread()> _
    Shared Sub Main()

'This is object loads all the components for my app
        Dim objLoader As LYRO.Loader = New Loader

'My flash screen reports the loading progress to the user,
'It is a windows.form object with a progressbar control on it
        Dim frmFlash As FlashScreen = New FlashScreen

'The loader object has an event variable that fires as each component is loaded
'here the Progress function in the FlashScreen object is subscribing to the loader event
'such that every time this event is fired, a message is sent to this function
        AddHandler objLoader.evtLoading, AddressOf frmFlash.Progress

'The function SeekPlayer in the loader object will be a thread, thus the loading of
'of the application and it's progress updates can run concurrently
        Dim loadStarter As New Threading.Thread(AddressOf objLoader.SeekPlayers)



        Application.Run(New LYRO.MainForm)
    End Sub
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Imports System.Reflection
Imports System.IO
Public Class Loader

    Public Event evtLoading(ByVal nProgressPct As Integer, ByVal strMessage As String)

    Public Sub SeekPlayers()

        Dim asm As [Assembly]
        Dim strCurDir As String = Directory.GetCurrentDirectory()

        Dim strFileNameArr() As String = Directory.GetFiles(strCurDir, "*.dll")

        RaiseEvent evtLoading(0, "Initializing loading sequence...")

        Dim i As Integer = 0
        Dim strFileName As String

        For Each strFileName In strFileNameArr
            asm = System.Reflection.Assembly.LoadFrom(strFileName)
            Dim types As Type
            For Each types In asm.GetTypes()
                If Not types.GetInterface("LYRO.iPlayer") Is Nothing Then
                    'ToDo: Add Players in datastore to be loaded for selection in main Gui
                    'ToDo: Once player retrieve load file extensions and player name dynamically
                    'from datastore
                End If
            i = i + 1
            RaiseEvent evtLoading(i / strFileNameArr.Length * 100, "Loading " & Path.GetFileName(strFileName))
    End Sub

End Class
nyee84Author Commented:
Since i am very new to programming especially .net, i wont able to digest your code and reuse in my program. Kindly help me to add the progess bar option in my program Thanks..
I'm sorry.

Would you care to further specify your difficulty with the progressbar control in your code.
nyee84Author Commented:
Dont know where i need to put the progress bar values.

Actually my program will let the user to select an excel file and once i  get the excel filename, each  rows PIDKEY worksheet will compare with my database field SERIAL. My database field got 1 million records and when i fill the dataset, the program hang(freeze) for while untill the dataset is completely filled data, so i thought of using a progress bar so that user will think that program is still processing and i dont know is there any otherway to resolve this issue rather using progressabar. Pls help me to add progressbar values then and there in my program

Sorry, I didn't get this meesage until now.

Do you still need help?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now