[VB.NET] Form Switcher Fade Module

AceInfinity

Emeritus, Contributor
Joined
Feb 21, 2012
Posts
1,728
Location
Canada
Here's a cool module I created a few minutes ago. I got the idea with some cool fading effects... Maybe you'll like it :)

If you want something fancy, try this Module I came up with:
Code:
Imports System.Threading

Module FormSwitcher
	<System.Runtime.CompilerServices.Extension()> _
	Public Sub SwitchForm(OriginalForm As Form, NewForm As Form)
		NewForm.Opacity = 0
		NewForm.Show()
		NewForm.Location = OriginalForm.Location

		OriginalForm.MatchSize(NewForm.Size)
		For d As Double = 1.0 To 0.0 Step -0.25
			OriginalForm.Opacity = d
			NewForm.Opacity = 1 - d
			Thread.Sleep(100)
		Next

		OriginalForm.Hide()
		OriginalForm.Opacity = 1
	End Sub

	<System.Runtime.CompilerServices.Extension()> _
	Private Sub MatchSize(OriginalForm As Form, NewSize As Size)
		Dim S As Size = OriginalForm.Size
		Dim Stepper As Integer = 0
		If S.Width <> NewSize.Width Then Stepper = If(S.Width < NewSize.Width, 1, -1)
		If Stepper <> 0 Then
			For i As Integer = S.Width To NewSize.Width Step Stepper
				OriginalForm.Width = i
			Next
		End If

		If S.Height <> NewSize.Height Then Stepper = If(S.Height < NewSize.Height, 1, -1)
		If Stepper <> 0 Then
			For i As Integer = S.Height To NewSize.Height Step Stepper
				OriginalForm.Height = i
			Next
		End If
	End Sub
End Module

Usage:
Code:
Me.SwitchForm(Form2)

PevMs.gif
 
I learned a bit about the height and width of forms with the apps design. Never thought to increment them like that, though. That's a good idea!
 
It's a fancy version I created to switch between forms. It could be modified to close the previous form, but if the main form is closed by default settings in a WinForms project, the application closes. So you'll have to modify the application's config to close when the last form closes instead if you take that route, otherwise, just being conscious of that will suffice, if you'll never be switching the main form out with a new one in the assembly.

Originally I had it so that it'd fade out and bring the new one in, then I thought that it didn't look nice when the forms were not 'in the same location' and one would close out on the left side of my screen while another opened near the bottom or on the right. So with some tweaking I came up with a visual concept to 'shrink' or expand to the new form's size the form which will soon 'disappear'. Then that quick disappear and show a new form thing wasn't doing it for me. So I thought of fading each in and out at the same time. Raising the opacity of one form, and alternating this process to make the other one fade out at relatively the same time 'visually'.
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top