Results 1 to 12 of 12

Thread: [2008] Forms

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    [2008] Forms

    I have set up a questionaire as a test program. It works perfectly as far as functionality, however there is one issue that is just annoying. I have a person select a radiobox for there answer. If there answer is correct another form pops up right above the time that says you got it right and slowly disappears until it is closed. It works properly but when it goes to the next question you have to wait until the form closes to answer the question. Is there a way to make it just pop up without having to wait for it to close before continuing.

    p.s all this is happening on the button click event. dont know if that makes a difference or not.

    edit: the fading form has this code:

    Code:
    Me.Top = Screen.PrimaryScreen.WorkingArea.Height - Me.Height
            Me.Left = Screen.PrimaryScreen.WorkingArea.Width - Me.Width
            Dim sngOpacity As Single
    
            For sngOpacity = 1 To 0 Step -0.05
                Me.Opacity = sngOpacity
                Me.Refresh()
                System.Threading.Thread.Sleep(200)
    
            Next sngOpacity
            Me.Close()
    Last edited by ngreenwood6; Jan 15th, 2009 at 04:13 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [2008] Forms

    How are you showing the form. If you are using ShowDialog, it will block until the form closes. If you use Show, then it won't. That might be enough of a solution, but without seeing how the form is displayed, it may be inadequate.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: [2008] Forms

    I am just using show() like this:
    Code:
    FadingForm.Show()
    Does it have something to do with the sleep function? Does that pause the whole application? Is there a workaround?

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2008] Forms

    Yes a sleep call will sleep the current thread (your UI thread in this case).

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] Forms

    That's because you're using default form instance. You should create a new instance of the form instead and that way, the next instance showing up doesn't affect the last one's disappearance.
    Try replace this line
    Code:
    FadingForm.Show()
    With these
    Code:
    Dim frmFading As New FadingForm()
    frmFading.Show()
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  6. #6
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2008] Forms

    Well the Thread sleep stops the UI thread for the whole application, so if he wants to fade the form using that code, he will have to use a new thread. I would recommend a background worker to do the work:

    Code:
         Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            BackgroundWorker1.RunWorkerAsync()
        End Sub
    
    
        Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            Dim sngOpacity As Single
    
            For sngOpacity = 1 To 0 Step -0.05
    
                Me.Invoke(New FormRefreshInvoker(AddressOf FormRefresh), sngOpacity)
                System.Threading.Thread.Sleep(200)
    
            Next sngOpacity
    
        End Sub
    
        Private Delegate Sub FormRefreshInvoker(ByVal Opacity As Single)
    
        Private Sub FormRefresh(ByVal opacity As Single)
            Me.Opacity = opacity
            Me.Refresh()
        End Sub
    
        Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
            Me.Close()
        End Sub

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: [2008] Forms

    stanav I tried using your code but it gave me the same results. Negative 0 I really don't understand what is going on in your code. I only want to use code that I understand. Isn't there something easier?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: [2008] Forms

    Is there a way to make it simulate a sleep without using the sleep?

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Forms

    Just stick a timer on your form that you want to fade out and make sure you still use stanavs method of creating a new instance of the form.

    When you want the form to fade out, set the timer's Enabled property to true like this:

    vb Code:
    1. Timer1.Enabled = True

    Now, in the timer's tick event (double click the timer component that sits below your form in the designer after you have dragged it in from the toolbox, to be taken to the tick event) just enter this:

    vb Code:
    1. If Me.Opacity = 0 Then
    2.   Timer1.Enabled = False
    3.   Me.Close()
    4. Else
    5.   Me.Opacity -= 0.1
    6. End If

    Each time the timer ticks (every 100 miliseconds by default if you dont change the Interval property) this event will be fired, so each time we check to see if the form is invisible (opacity = 0) and if not then we decrease the opacity a little. So then the next time this event fires 100 milliseconds from now we do the same thing until eventually we reach 0 and the form is invisible so we disable the timer and close the form.

    If you want to make the form fade out smoother then you can increase the timer's interval property but I find it works fine set to about 300 (miliseconds). Of course you can also play around with the values that I am reducing the opacity by, ie changing it to Me.Opacity -= 0.05 may be closer to what you are after going by your original code.
    If your wondering what the -= is for, its an operator that means "take this value away from the existing value" and similarly += adds to an existing value. So this:
    vb Code:
    1. MyVariable -= 1
    2. ' is the same as this:
    3. MyVariable = MyVariable - 1
    Last edited by chris128; Jan 15th, 2009 at 06:50 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [2008] Forms

    While it's true that the sleep, as it now stands, will block the UI, the sleep interval is 0.2 seconds. That's long enough for a person to actually notice it, but doesn't seem like it would really cause this problem. What is causing the problem is the loop itself, not the sleep. Once that loop is entered, the process blocks (I should have seen that from the start) until the loop completes. Using a timer to do this will solve that problem, and there will be no need for the sleep, or a background thread.

    The more I read, the more I realize that if you are putting Thread.Sleep in your code ANYWHERE, you have a flaw in your design. There is always a different way, and almost always a better way (though there are cases where Sleep is the easiest solution for a background thread).
    My usual boring signature: Nothing

  11. #11
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Forms

    I agree, I dont really know why but I always thought of Thread.Sleep as a bad way of doing things, kind of cheating if you like as you should never really need your program to just stop dead so if you are doing that then there is either a problem with your design or a problem with your code (like you said) ...
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: [2008] Forms

    Thank you chris once again. I used the timer instead of the sleep option and it worked like a charm. I never even thought about that. It's great that you guys are here and such a help.

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