Results 1 to 28 of 28

Thread: ProgressBar question

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    22

    ProgressBar question

    Hello, I'm new to VB and I'm making my first Application.
    I want my ProgressBar to start loading when MessageBoxButton.OK is pressed.
    This is my code:
    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MessageBox.Show("My Text", "My Title", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)
            If AcceptButton Then
                ProgressBar1.Start()
            End If
    
        End Sub

    This is the part I'm having problems with.
    Code:
    If AcceptButton Then
         ProgressBar1.Start()
    Also, I'd like the ProgressBar1 to finish after a certain time after it started. How do I do that?

  2. #2
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: ProgressBar question

    you move the progressbar by using ProgressBar1.Value, this has to be between the min and max levels.

    If the progressbar is timer based setup a timer or a stopwatch on the form and use that to set the value (having setup already the max and min values)
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    22

    Re: ProgressBar question

    For example, I want my progress bar to start when OK Button from a MsgTextbox and load up to 100% in 5 minutes, which would be the code?

  4. #4
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: ProgressBar question

    post removed as explained reason why in following post...
    Last edited by Megalith; Dec 14th, 2009 at 02:38 PM.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  5. #5
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: ProgressBar question

    Quote Originally Posted by hotlip1 View Post
    For example, I want my progress bar to start when OK Button from a MsgTextbox and load up to 100% in 5 minutes, which would be the code?
    We'll explain the steps you need to take and allow you to figure it out on your own. This method will allow you to learn better.

    If you don't understand, then post the code you've tried and we'll help figure it out.

    First, forget about the MessageBox. Figure out how the ProgressBar works and then move on to implementing it with the MessageBox. The MessageBox will be the easiest part.

    There are few things you need to know about the ProgressBar. You'll need to use Timer with what you're wanting. You'll need to set the timer's interval to a specific time so that it ticks when you want. I imagine you'll probably want it to tick every second. So, in the properties pane, set the Timer's Interval property to 1000, which is one second. You'll need to set the Enabled property of the Timer to True as it is False by default.

    Then you'll need to set the Minimum and Maximum vale of the ProgressBar. You'll also need to set the Step property, which controls how much the ProgressBar increases when you call PerformStep. You can do this in the Properties pane or in code like this:

    vb Code:
    1. ProgressBar1.Maximum = 300
    2. ProgressBar1.Minimum = 0
    3. ProgressBar1.Step = 1

    Since you want the ProgressBar to finish after 5 minutes, I set the maximum property to 300. The timer will tick every second and after 300 seconds, we'll know that 5 minutes has elapsed.

    You can set all of this code in the Form's Load event.

    Now all you'll need to do is call ProgressBar1.PerformStep in the Timer's Tick Event.

    After you get this working, we'll show you how to do it when the MessageBox's "OK" button has been pressed.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  6. #6
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: ProgressBar question

    oh yeah forgot about ProgressBar1.PerformStep better than ProgressBar1.value += 1
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    22

    Re: ProgressBar question

    THAAANKS! I got it to work!
    This is my code:
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MessageBox.Show("", " ", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)
        End Sub
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            ProgressBar1.PerformStep()
        End Sub
    End Class

    EDIT: I guessNow I have to do something like
    Code:
    If MessageBoxButton.OK Then
       <Somethingto start the clock>
    End If
    Last edited by hotlip1; Dec 14th, 2009 at 03:13 PM.

  8. #8
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: ProgressBar question

    Correct you need Timer1.Enabled = True but you also need to ensure the progressbar1 properties mentioned by weirddemon are setup i.e. min max and step, this could be either in the form load event or you could use the button click
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    22

    Re: ProgressBar question

    Quote Originally Posted by Megalith View Post
    Correct you need Timer1.Enabled = True but you also need to ensure the progressbar1 properties mentioned by weirddemon are setup i.e. min max and step, this could be either in the form load event or you could use the button click
    Yep, already set that.
    And were thoes the code to start the timer go?

    Is this ok?
    Code:
    If MessageBoxButton.OK Then
       Timer1.Enabled 
    End If

  10. #10
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: ProgressBar question

    After you enable the Timer, you need to start it.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    22

    Re: ProgressBar question

    Quote Originally Posted by weirddemon View Post
    After you enable the Timer, you need to start it.
    Can I just set Enabled as true in its properties then do Timer1.Start()?

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    22

    Re: ProgressBar question

    Also, I get MessageBoxButton.OK is not declared.
    How should I refer to it?

  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: ProgressBar question

    They are equivalent. Timer.Start and Timer.Enabled = True do the same thing.

    http://msdn.microsoft.com/en-us/libr...rs(VS.71).aspx
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  14. #14
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    22

    Re: ProgressBar question

    Alright, I got it to work.
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MessageBox.Show("", " ", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)
            If MessageBoxButtons.OKCanel Then
                Timer1.Start()
            End If
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            ProgressBar1.PerformStep()
        End Sub
    
    End Class
    The only problem is that if I press cancel, the ProgressBar starts loading too.
    I got to replace MessageBoxButtoms.OKCancel (highlighted it in my code), I know. But with what?

  16. #16
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: ProgressBar question

    The link I posted shows how to use the messagebox.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    22

    Re: ProgressBar question

    Quote Originally Posted by dbasnett View Post
    The link I posted shows how to use the messagebox.
    Still, I don't know how to call the OK buttom from the msgbox.

  18. #18
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: ProgressBar question

    from the link i posted

    Code:
            Dim Result As DialogResult
    
            'Displays the MessageBox
    
            Result = MessageBox.Show(Message, Caption, Buttons)
    
            ' Gets the result of the MessageBox display.  Maybe one for OK????????????
    
            If Result = System.Windows.Forms.DialogResult.Yes Then
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  19. #19
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: ProgressBar question

    Here how I do it:

    vb Code:
    1. If MessageBox.Show("TEST", "CAPTION", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly) = Windows.Forms.DialogResult.OK Then
    2.             Label2.Text = "OK"
    3.         Else
    4.             Label2.Text = "NOT OK"
    5.  
    6.         End If

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    22

    Re: ProgressBar question

    DONE! THANKS TO EVERYBODY WHO HELPED ME.!
    My code:
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MessageBox.Show("", " ", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
            If System.Windows.Forms.DialogResult.OK Then
                Timer1.Start()
            End If
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            ProgressBar1.PerformStep()
        End Sub
    End Class

  21. #21
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: ProgressBar question

    nice solution
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  22. #22
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: ProgressBar question

    This "If System.Windows.Forms.DialogResult.OK Then" tells me that you do not have Option Strict On, not to mention there is not a comparison. It will always evaluate to true, regardless of which button you click.

    It should look something like this:

    Code:
            Dim dRslt As DialogResult
            dRslt = MessageBox.Show("OK or Cancel", "Test", MessageBoxButtons.OKCancel)
            If dRslt = Windows.Forms.DialogResult.OK Then
                'ok
            Else
                'cancel
            End If
    Last edited by dbasnett; Dec 14th, 2009 at 05:39 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    22

    Re: ProgressBar question

    bleh no, false, i didn't fix it omg.
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MessageBox.Show("", " ", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
            If System.Windows.Forms.DialogResult.Yes Then
                Timer1.Start()
            End If
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            ProgressBar1.PerformStep()
        End Sub
    End Class
    Doesn't matter if I press Yes or No, the ProgressBar will start, although in my code it says only if DialogResult.Yes

  24. #24
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: ProgressBar question

    That is what I said. As mentioned before, go here http://msdn.microsoft.com/en-us/libr...essagebox.aspx
    and look for
    "The following code example shows how to ask the user a yes or no question and make a decision based on the response."
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  25. #25

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    22

    Re: ProgressBar question

    Then what's wrong here?:
    Code:
    MessageBox.Show("", " ", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                Dim Result As DialogResult
                If Result = System.Windows.Forms.DialogResult.Yes Then
                    Timer1.Start()
                End If

  26. #26
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: ProgressBar question

    Quote Originally Posted by hotlip1 View Post
    Then what's wrong here?:
    Code:
    MessageBox.Show("", " ", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                Dim Result As DialogResult
                If Result = System.Windows.Forms.DialogResult.Yes Then
                    Timer1.Start()
                End If
    Compare that to this http://www.vbforums.com/showpost.php...9&postcount=22
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    22

    Re: ProgressBar question

    Lol, sorry didn't read that post before :P

    Now it worked, finally, without issues.
    I've learned alot today! THANKS!

    1 more thing:
    I want to block a button after a desired time, and that after I reopen the application it is still blocked. How do I do that?
    Last edited by hotlip1; Dec 14th, 2009 at 06:45 PM.

  28. #28
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: ProgressBar question

    similar but you are looking at button1.disable, you should know where to place this now, only issue is counting the ticks but you could use progressbar1.value to get the elapsed seconds
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

Posting Permissions

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



Click Here to Expand Forum to Full Width