dcsimg
Results 1 to 20 of 20

Thread: Loading form or progress bar

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Loading form or progress bar

    Hi

    I have two forms, Form1, Form2

    I use a CommandButton to move from one form to another.

    I use this code:

    Form2.Show

    Me.Hide




    This code works fine but the second form doesn't show up instantly because form2 has to pull some data from the server.

    Is there any way to display for example a progress bar while loading the other form and then show up the form when its load completes.

    Thanks

  2. #2
    PowerPoster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,115

    Re: Loading form or progress bar

    Some kind of splash screen, "Loading - Please Wait" or a marque progress bar might help. While the database has "control" there is not much you can do. Maybe use a background worker but that seems like overkill.
    Please remember next time...elections matter!

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Loading form or progress bar

    I tried the progress bar but I did not get it done

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Loading form or progress bar

    I used this with the progress bar,

    Code:
      ProgressBar1.Increment(10)
            If ProgressBar1.Value = 100 Then
                Timer5.Stop()
                Form3.Show()
                ProgressBar1.Value = 0
    
            End If
    but the problem that the value is already setup by me, while I need it to be automatically based on the loading time

  5. #5
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,142

    Re: Loading form or progress bar

    Hi,

    what is it your loading ?

    to show progress while loading a large Table from Database...
    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim strSql As String
            strSql = "SELECT * From Tabelle1"
            Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\Northwind.mdb")
            Dim cmd As OleDbCommand = New OleDbCommand(strSql, con)
            con.Open()
            Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
            Dim myDataSet As DataSet = New DataSet()
            myDA.Fill(myDataSet, "MyT")
            Dim rowCount As Integer = myDataSet.Tables("MyT").Rows.Count
            ' MsgBox(rowCount) just Check the Number
            TextBox1.Text = rowCount
            Dim i As Integer
            For i = 0 To rowCount - 1  
                ProgressBar1.PerformStep()
                If ProgressBar1.Value = rowCount Then
                End If
    
            Next
            DataGridView1.DataSource = myDataSet.Tables("MyT").DefaultView
    
            ProgressBar1.Value = 0
            con.Close()
            con = Nothing
        End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Loading form or progress bar

    Quote Originally Posted by ChrisE View Post
    Hi,

    what is it your loading ?

    to show progress while loading a large Table from Database...
    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim strSql As String
            strSql = "SELECT * From Tabelle1"
            Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\Northwind.mdb")
            Dim cmd As OleDbCommand = New OleDbCommand(strSql, con)
            con.Open()
            Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
            Dim myDataSet As DataSet = New DataSet()
            myDA.Fill(myDataSet, "MyT")
            Dim rowCount As Integer = myDataSet.Tables("MyT").Rows.Count
            ' MsgBox(rowCount) just Check the Number
            TextBox1.Text = rowCount
            Dim i As Integer
            For i = 0 To rowCount - 1  
                ProgressBar1.PerformStep()
                If ProgressBar1.Value = rowCount Then
                End If
    
            Next
            DataGridView1.DataSource = myDataSet.Tables("MyT").DefaultView
    
            ProgressBar1.Value = 0
            con.Close()
            con = Nothing
        End Sub
    regards
    Chris
    it is not working this way !! I would like to show another form to show please wait message and then once it loads the waiting form will disappear

  7. #7
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,142

    Re: Loading form or progress bar

    still don't know what your loading,
    I don't have a Crystal Ball that I could look into and see what your loading


    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,734

    Re: Loading form or progress bar

    Just use a progress bar in marquee mode and call it a day. That's really the only way it will work effectively. Otherwise you have to get a count of the rows, then load the rows one by one, and increment the pg as you go... but that's just going to slow it down even more.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Loading form or progress bar

    Quote Originally Posted by ChrisE View Post
    still don't know what your loading,
    I don't have a Crystal Ball that I could look into and see what your loading


    regards
    Chris
    sorry I did not notice your question, there is nothing in my loading code, and I am loading a window and this window has a datagrideview which is connected to a Microsoft Access database.

  10. #10
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,142

    Re: Loading form or progress bar

    Hi

    I still don't know what you doing, so I will guess it is showing one form then another

    open a new project and add 2 forms, set Form2 as your start form.
    then add this to form2
    Code:
    'set Form 2 as the startform
    'place all this in Form 2
    
    
    Public Class Form2
    
        ' Load
        Private Sub LoadForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Start()
            Timer1.Interval = 10
        End Sub
        ' Timer 1
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            My.Forms.Form1.Show()
            My.Forms.Form1.WindowState = FormWindowState.Normal
            My.Forms.Form1.Hide()
            Timer2.Start()
            Timer1.Stop()
        End Sub
        ' Timer 2
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            ProgressBar1.Value += 1
            If ProgressBar1.Value = 100 Then
                My.Forms.Form1.BringToFront()
                Me.Hide()
                My.Forms.Form1.Show()
                Timer2.Stop()
            End If
        End Sub
    End Class
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  11. #11

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Loading form or progress bar

    Quote Originally Posted by ChrisE View Post
    Hi

    I still don't know what you doing, so I will guess it is showing one form then another

    open a new project and add 2 forms, set Form2 as your start form.
    then add this to form2
    Code:
    'set Form 2 as the startform
    'place all this in Form 2
    
    
    Public Class Form2
    
        ' Load
        Private Sub LoadForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Start()
            Timer1.Interval = 10
        End Sub
        ' Timer 1
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            My.Forms.Form1.Show()
            My.Forms.Form1.WindowState = FormWindowState.Normal
            My.Forms.Form1.Hide()
            Timer2.Start()
            Timer1.Stop()
        End Sub
        ' Timer 2
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            ProgressBar1.Value += 1
            If ProgressBar1.Value = 100 Then
                My.Forms.Form1.BringToFront()
                Me.Hide()
                My.Forms.Form1.Show()
                Timer2.Stop()
            End If
        End Sub
    End Class
    regards
    Chris
    thanks my friend but I am using this now, I need the value of the progress bar to be filled automatically based on the loading time of the second form.

  12. #12
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,142

    Re: Loading form or progress bar

    Quote Originally Posted by vbuser99 View Post
    thanks my friend but I am using this now, I need the value of the progress bar to be filled automatically based on the loading time of the second form.

    good thing I had my Crystal Ball;-))
    only joking

    regards
    chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,734

    Re: Loading form or progress bar

    Quote Originally Posted by vbuser99 View Post
    thanks my friend but I am using this now, I need the value of the progress bar to be filled automatically based on the loading time of the second form.
    That's simply not going to happen.... HOW are you going to know if it is going to take two seconds or twenty? You don't know. It's going to take as long as it is going to take, there are a number of factors involved. You are tilting at windmills here. That's why I said to just simply put the progress bar into marquee mode and let it do it's thing. It'll just go back and forth like a Cylon eye until you stop it. No messing with a timer. No messing with counts. F'in magic. Couple lines of code, nothing more.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Loading form or progress bar

    Quote Originally Posted by techgnome View Post
    Just use a progress bar in marquee mode and call it a day. That's really the only way it will work effectively. Otherwise you have to get a count of the rows, then load the rows one by one, and increment the pg as you go... but that's just going to slow it down even more.

    -tg
    would you please explain this, I think this might work perfectly?

  15. #15
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,734

    Re: Loading form or progress bar

    Put a Progress Bar on your form... set it to visible = False.... then use this:
    Code:
            ProgressBar1.Visible = True
            ProgressBar1.Style = ProgressBarStyle.Marquee
            Form2.Show()
            Me.Hide()
            ProgressBar1.Style = ProgressBarStyle.Blocks
            ProgressBar1.Visible = False
    That should do it
    I don't have any long loading forms to test... but that should be enough to make it work.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,244

    Re: Loading form or progress bar

    I would ask for a bit more information:

    1) You say that you are getting data into a DGV from an Access database, but are you filling a datatable, or doing something else?

    2) Is the data being loaded based on settings the user makes in the first form?

    3) How long will it be before the user would normally move to form2?

    Depending on the answers to this, there might be a better solution. However, it is the second question that is the key, and the answer you give will probably be the wrong one. If the data you are getting for form2 is dependent on options selected by form1, then you may have no choice other than what TG suggested. However, in some cases there could be a different option. For example, I have a form that displays a whole bunch of data. That data takes a painfully long time to load, but it isn't dependent on anything in any other form, and the user won't normally get to the form for several seconds, or even minutes. So, when the application starts, I launch a thread that fills some datatables with the data the form will show. By the time the user gets to the form, the data has already loaded and is ready to be shown. However, this only works because I don't need any input from the user to decide what data to load, and the user will take a relatively long time getting to the form. If either of those were not true, I wouldn't have been able to use that solution, but it isn't clear what your situation is.
    My usual boring signature: Nothing

  17. #17
    Fanatic Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    856

    Re: Loading form or progress bar

    Quote Originally Posted by vbuser99 View Post
    Hi

    I have two forms, Form1, Form2

    I use a CommandButton to move from one form to another.

    I use this code:

    Form2.Show

    Me.Hide

    This code works fine but the second form doesn't show up instantly because form2 has to pull some data from the server.

    Is there any way to display for example a progress bar while loading the other form and then show up the form when its load completes.

    Thanks
    Edit: I see Shaggy already suggested something like this just above. I hadn't read the whole thread.

    Another option is to just do the data load for Form2 in the background. When my login form starts, it loads all the project info (which takes about 8 seconds) in the background while the user is busy logging in.

    Code:
        Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles Me.Load
    
            Try
                If OpenControl() Then
                    OpenDB()
                    LoadProjects() 'simple BGW
                Else
                    Application.Exit()
                End If
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
                Application.Exit()
            End Try
        End Sub
    If they enter good credentials then I just wait the few extra seconds if they are quick. You can display whatever message or progress bar, etc.

    Code:
             'snipped from btnOK_Click
                        lblStatus.Text = "Initializing projects form..."
                        lblStatus.Refresh()
                        'if bgw not done, add timer to wait to load
                        If projectsLoaded Then
                            Timer1_Tick(sender, e) 'load Projects form now
                        Else
                            MDIMain.Cursor = Cursors.WaitCursor
                            Timer1.Enabled = True
                        End If

  18. #18
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Loading form or progress bar

    Here's the problem, and the solution's a little trickier than you probably want it to be.

    There is one thread, and only one thread, that is allowed to work with UI. That means if you have one form or one hundred, all of them are using the same thread to do their stuff. "Their stuff" includes painting. So if one form is occupying the UI thread, then all of them appear to freeze.

    That means if you display a progress bar on one form, then ask another form to take a long time loading data, the progress bar will freeze, even though it's on another form. Changing its value causes it to ask to repaint, but it can't paint because the other form is busy loading data on the UI thread. After the data loads, the UI thread is free and can catch up.

    So the only way you can do a "splash screen" or "loading indicator" for another form is to make sure the work that form does is on a worker thread. That lets the UI thread keep updating the progress bar. This can get really difficult depending on what the work is, and sometimes it's impossible.

    For example, if you're loading 10,000 database records and it takes 10 or 15 seconds, part of that delay is updating the DataGridView. While you can make the database connection and execute the query on the worker thread, you have to make sure the operations on the databound DataSet happen on the UI thread. (Maybe it handles this, I'm not sure.) Same thing with if you want to update a ListBox: items have to be added on the UI thread, and a re-render happens for each item.

    So for some cases, you need a worker thread. For other cases, you're trying to squeeze blood from a turnip. Somewhere past 1,000 rows, it will take a noticeable delay to load data into a DataGridView. So pick a number 1,000 or lower and only load that many records. If you need for the user to edit them all, you can "page" the data and requery as needed.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  19. #19

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Loading form or progress bar

    I got it solved by showing a pop up form with gif please wait image in picture box
    and in the 2nd form activated event I hide the pop up form,

    but the problem now is that the gif image is not showing while loading the form? it shows me on the border of the form while the gif image itself is not showing even if takes long time it does not show the gif.

    but if I load the gif image in a separated form it works fine!!

  20. #20
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,244

    Re: Loading form or progress bar

    Immediately after you show the form, call .Refresh on it.

    What is happening is that the form isn't getting a chance to paint itself, because the UI thread is too busy. Normally, a form will only invalidate, which means that it paints when it gets around to it. Calling .Refresh forces it to stop and draw immediately. That's not usually a good idea, as it is usually better to let it do the drawing when it gets time for it, but tis is a case where forcing the drawing will work better.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width