|
-
Jan 15th, 2009, 04:10 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Jan 15th, 2009, 04:25 PM
#2
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
 
-
Jan 15th, 2009, 04:30 PM
#3
Thread Starter
Hyperactive Member
Re: [2008] Forms
I am just using show() like this:
Does it have something to do with the sleep function? Does that pause the whole application? Is there a workaround?
-
Jan 15th, 2009, 04:35 PM
#4
Re: [2008] Forms
Yes a sleep call will sleep the current thread (your UI thread in this case).
-
Jan 15th, 2009, 04:41 PM
#5
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
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 -
-
Jan 15th, 2009, 04:46 PM
#6
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
-
Jan 15th, 2009, 04:59 PM
#7
Thread Starter
Hyperactive Member
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?
-
Jan 15th, 2009, 06:27 PM
#8
Thread Starter
Hyperactive Member
Re: [2008] Forms
Is there a way to make it simulate a sleep without using the sleep?
-
Jan 15th, 2009, 06:42 PM
#9
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:
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:
If Me.Opacity = 0 Then
Timer1.Enabled = False
Me.Close()
Else
Me.Opacity -= 0.1
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:
MyVariable -= 1
' is the same as this:
MyVariable = MyVariable - 1
Last edited by chris128; Jan 15th, 2009 at 06:50 PM.
-
Jan 15th, 2009, 07:25 PM
#10
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
 
-
Jan 15th, 2009, 07:39 PM
#11
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) ...
-
Jan 15th, 2009, 09:18 PM
#12
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|