CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2005
    Posts
    20

    Form losing focus stops counter

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim i As Integer = 1
    For i = 1 To 100000
    Me.Label1.Text = Str(i) & " of 100000"
    Me.Refresh()
    Next
    End Sub

    When I click my Button1 and then click on another window, say Windows Explorer or Outlook, etc., my VB.NET form obviously loses focus. When I come back to it it doesn't refresh until the loop is completed.

    Is there anyway to either keep the VB.NET form modal; not just within my VB.NET app but throughout all of the windows that are up on my PC?

    Or is there a way to keep it running in the background or refresh it upon activating? I've tried the activated event, but it still waits for the for loop to finish.

    Any thoughts? Thanks
    Ben

  2. #2
    Join Date
    Apr 2005
    Location
    India
    Posts
    271

    Smile Re: Form losing focus stops counter

    To make your form respond to other events while doing heavy jobs, you can use Application.DoEvents() Method.

    So your early code can be fixed to the below given form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim i As Integer = 1
    For i = 1 To 100000
    Application.DoEvents()
    Me.Label1.Text = Str(i) & " of 100000"
    Me.Refresh()
    Next

    End Sub

    Hope that solves your prob

  3. #3
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Form losing focus stops counter

    Another way is to put your loop on it's own thread.

  4. #4
    Join Date
    Mar 2005
    Posts
    20

    Re: Form losing focus stops counter

    Cool, it works; thanks much

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

Featured