• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • 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

        OpenExcel.ShowDialog()
        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=192.168.3.56;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
        XL.Workbooks.Open(FileName)  
        ' 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
        Next

        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
        Next
        XL.ActiveWorkbook.Save()
        XL.ActiveWorkbook.Close()
        XL.Application.Quit()
        XL = Nothing
        Application.DoEvents()
        MsgBox("Completed")
    End Function
0
nyee84
Asked:
nyee84
  • 5
  • 3
1 Solution
 
TertioptusCommented:
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"
            System.Threading.Thread.Sleep(1000)
            Me.Close()
        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))
0
 
nyee84Author Commented:
Could you advice me how to  use you code in my pro...

0
 
TertioptusCommented:
   <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)

        loadStarter.Start()

        frmFlash.ShowDialog()

        Application.Run(New LYRO.MainForm)
    End Sub
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
TertioptusCommented:
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
            Next
            i = i + 1
            RaiseEvent evtLoading(i / strFileNameArr.Length * 100, "Loading " & Path.GetFileName(strFileName))
        Next
    End Sub

End Class
0
 
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..
0
 
TertioptusCommented:
I'm sorry.

Would you care to further specify your difficulty with the progressbar control in your code.
0
 
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

Thanks
0
 
TertioptusCommented:
Sorry, I didn't get this meesage until now.

Do you still need help?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

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