Code Snippets

  

VB.NET Source Code



FadeTo (Extension Method)

Adds a fading ability to windows forms.

Submitted By: AdamSpeight2008
Actions:
Rating:
Views: 999

Language: VB.NET

Last Modified: August 31, 2010

Snippet


  1. Public Module Exts_Form
  2.   ''' <summary>
  3.   ''' Changes the opacity of the specified form to the specified opacity, over a specified time.
  4.   ''' </summary>
  5.   ''' <param name="Form">Which form to fade.</param>
  6.   ''' <param name="ToOpacity">To what opacity (0->Transparent - 1->Opaque) </param>
  7.   ''' <param name="OverMs">Time im Miliseconds to fade over.</param>
  8.   ''' <remarks></remarks>
  9.   <Runtime.CompilerServices.Extension()>
  10.   Public Sub FadeTo(ByVal [Form] As Form, ByVal ToOpacity As Double, ByVal OverMs As Integer)
  11.     If [Form] Is Nothing Then Throw New ArgumentNullException("[Form] can not be null")
  12.     If ToOpacity < 0 OrElse ToOpacity > 1 Then Throw New ArgumentOutOfRangeException(String.Format("ToOpacity value ({0}) is outside range 0-1", ToOpacity))
  13.     ' Is the speed quicker then a person can see.
  14.     Const MinMs As Integer = 40
  15.     If OverMs < MinMs Then OverMs = MinMs
  16.     Dim NumberOfSteps = Math.Ceiling(OverMs / MinMs)
  17.     Dim StepDelta = (ToOpacity - [Form].Opacity) / NumberOfSteps
  18.     If [Form].InvokeRequired Then
  19.       [Form].Invoke(New Del_FadeTo(AddressOf FadeTo), New Object() {[Form], ToOpacity, OverMs})
  20.     Else
  21.       While If(StepDelta < 0, [Form].Opacity > ToOpacity, [Form].Opacity < ToOpacity)
  22.         [Form].Opacity += StepDelta
  23.         Threading.Thread.Sleep(MinMs)
  24.       End While
  25.     End If
  26.   End Sub
  27.   Public Delegate Sub Del_FadeTo(ByVal [Form] As Form, ByVal ToOpacity As Double, ByVal OverMs As Integer)
  28.  
  29. End Module
  30.  

Copy & Paste


Comments

There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.