Elliot B. Posted December 22, 2015 Share Posted December 22, 2015 This tutorial shows me how to: Fade the program in when it is opened Fade the program out when a Button on the form is clicked I want to adapt it so that the program fades out when the user clicks Explorer's X (top right). I have set the appropriate code for Timer2 but I just need help with how to apply Timer2 to when the program is closed. Link to comment Share on other sites More sharing options...
0 Circaflex Posted December 23, 2015 Share Posted December 23, 2015 Does this help at all? http://www.freevbcode.com/ShowCode.asp?ID=9231 or maybe this perhaps? http://www.dreamincode.net/forums/topic/177646-fading-form/page__view__findpost__p__1042159 Link to comment Share on other sites More sharing options...
0 The_Decryptor Veteran Posted December 23, 2015 Veteran Share Posted December 23, 2015 I'd recommend not doing this at all, Windows 10 at least already has a window closing animation. But if you need to then the second link in the above post has the right info, watch for the FormClosing event. Andre S. 1 Share Link to comment Share on other sites More sharing options...
0 Elliot B. Posted December 23, 2015 Author Share Posted December 23, 2015 When I double-click on Form1, it brings up the code. At the top, I have: Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.StartPosition = FormStartPosition.CenterParent Me.Opacity = 0 Timer1.Interval = 20 Timer1.Enabled = True Timer2.Interval = 20 Timer2.Enabled = False End Sub Private Sub FadingForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing If Me.Opacity = 1 Then Timer2.Enabled = True End If End Sub At the bottom, I have: Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick If Me.Opacity = 1 Then Timer1.Stop() Else Me.Opacity += 0.04 End If End Sub Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick If Me.Opacity = 0 Then Timer2.Stop() End Else Me.Opacity -= 0.02 End If End Sub The program is fading in just fine. However, closing the window isn't fading out. Any ideas? Link to comment Share on other sites More sharing options...
0 The_Decryptor Veteran Posted December 23, 2015 Veteran Share Posted December 23, 2015 You have to cancel the actual FormClosing event, so your code can then close the window. Apparently you set e.Cancel = true. Edit: And then close the form yourself when the timer finishes, and make sure you don't catch the event again, so you'll need a guard variable. Link to comment Share on other sites More sharing options...
0 simplezz Posted December 23, 2015 Share Posted December 23, 2015 Implementing it using timers seems grossly inefficient to me. When I was toying with the idea, I just did it in a loop: private void fadeOut() { const int steps = 175; float x = 100f; float step = x / steps; for (int i = 0; i < steps; i++) { Opacity = x / 100; this.Refresh(); x -= step; } } Seemed to work fine at the time. Link to comment Share on other sites More sharing options...
0 The_Decryptor Veteran Posted December 24, 2015 Veteran Share Posted December 24, 2015 I'm not sure you could get more efficient that the inbuilt Timer class, they're wrappers around kernel functions iirc. They also let the main thread process messages when they're not running. Link to comment Share on other sites More sharing options...
0 Andre S. Veteran Posted December 26, 2015 Veteran Share Posted December 26, 2015 Doesn't seem worth the loss in responsivity to me. When the user clicks the top-right X, his expectation is that the application closes immediately, unless there's very good reason to delay it (e.g. data loss). Kami- 1 Share Link to comment Share on other sites More sharing options...
0 Elliot B. Posted January 5, 2016 Author Share Posted January 5, 2016 On 23/12/2015 at 1:15 PM, The_Decryptor said: You have to cancel the actual FormClosing event, so your code can then close the window. Apparently you set e.Cancel = true. Edit: And then close the form yourself when the timer finishes, and make sure you don't catch the event again, so you'll need a guard variable. I'm a bit lost without code On 23/12/2015 at 5:30 PM, simplezz said: Implementing it using timers seems grossly inefficient to me. When I was toying with the idea, I just did it in a loop: private void fadeOut() { const int steps = 175; float x = 100f; float step = x / steps; for (int i = 0; i < steps; i++) { Opacity = x / 100; this.Refresh(); x -= step; } } Seemed to work fine at the time. That doesn't seem like the correct language. The program wouldn't run, loads of code errors. Link to comment Share on other sites More sharing options...
0 simplezz Posted January 5, 2016 Share Posted January 5, 2016 6 hours ago, Elliot B. said: I'm a bit lost without code That doesn't seem like the correct language. The program wouldn't run, loads of code errors. It's valid C#. Here I converted it to VB for you: Private Sub fadeOut() Const steps As Integer = 175 Dim x As Single = 100F Dim [step] As Single = x / steps For i As Integer = 0 To steps - 1 Opacity = x / 100 Me.Refresh() x -= [step] Next End Sub I actually used it on the PowerNotifier briefly during testing when I was considering having the Window automatically appear/disappear as the mouse entered/left the system tray. I decided against it in the end though. Link to comment Share on other sites More sharing options...
0 Elliot B. Posted January 5, 2016 Author Share Posted January 5, 2016 I wasn't sure where to put that code so I put it at the top of form1 but it doesn't work The top now reads: Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.StartPosition = FormStartPosition.CenterParent Me.Opacity = 0 Timer1.Interval = 20 Timer1.Enabled = True Timer2.Interval = 20 Timer2.Enabled = False End Sub Private Sub fadeOut() Const steps As Integer = 175 Dim x As Single = 100.0F Dim [step] As Single = x / steps For i As Integer = 0 To steps - 1 Opacity = x / 100 Me.Refresh() x -= [step] Next End Sub Private Sub FadingForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing If Me.Opacity = 1 Then Timer2.Enabled = True End If End Sub Link to comment Share on other sites More sharing options...
0 simplezz Posted January 5, 2016 Share Posted January 5, 2016 1 minute ago, Elliot B. said: I wasn't sure where to put that code so I put it at the top of form1 but it doesn't work You need to call the subroutine at the point which you want to fade. Whether that's in the form closing event or not is up to you. So something like this: Private Sub FadingForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing fadeOut() End Sub Link to comment Share on other sites More sharing options...
0 +Sledge Subscriber² Posted January 5, 2016 Subscriber² Share Posted January 5, 2016 1995 called and asked for it's VB Winforms fadeouts to be left where they belong... in 1995. Leave this to the O\S and spend the time making the app more functional \ stable. Link to comment Share on other sites More sharing options...
0 Unobscured Vision Posted January 6, 2016 Share Posted January 6, 2016 7 hours ago, Sledge said: 1995 called and asked for it's VB Winforms fadeouts to be left where they belong... in 1995. Leave this to the O\S and spend the time making the app more functional \ stable. Easy .. this is how people learn. By doing. We were all new to it once. Cut the OP a little slack? simplezz 1 Share Link to comment Share on other sites More sharing options...
0 Elliot B. Posted January 6, 2016 Author Share Posted January 6, 2016 17 hours ago, simplezz said: You need to call the subroutine at the point which you want to fade. Whether that's in the form closing event or not is up to you. So something like this: Private Sub FadingForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing fadeOut() End Sub Absolute legend, thank you! Link to comment Share on other sites More sharing options...
0 simplezz Posted January 6, 2016 Share Posted January 6, 2016 20 hours ago, Sledge said: 1995 called and asked for it's VB Winforms fadeouts to be left where they belong... in 1995. Leave this to the O\S and spend the time making the app more functional \ stable. For someone who wants control over how and when it fades, it's quite reasonable. The OS doesn't even support what I was using it for Link to comment Share on other sites More sharing options...
Question
Elliot B.
This tutorial shows me how to:
I want to adapt it so that the program fades out when the user clicks Explorer's X (top right).
I have set the appropriate code for Timer2 but I just need help with how to apply Timer2 to when the program is closed.
Link to comment
Share on other sites
15 answers to this question
Recommended Posts