Thursday, October 8, 2009

Automatically fading buttons in VB.NET

I often find myself obsessing over the finer parts of interface design with my applications. I always thought buttons that innocuously faded from their mouseover color back to their default background color was something really nice and not *too* flashy. Since I couldn't find any material for replicating this in VB.NET I did what all programmers do, I wrote one myself. I hope this code helps anyone attempting to do something similar. Improve upon it if you wish, I am just posting the basic code, it will works best if you encapsulate it in a UserControl...

Public Class Form1
Friend WithEvents btnTimer As New Timer
Dim RedValue As Integer
Dim GreenValue As Integer
Dim BlueValue As Integer
Dim RAdd As Integer = 0
Dim Gadd As Integer = 0
Dim Badd As Integer = 0
Dim MyBackColor As Color



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

btnTimer.Stop()
MyBackColor = Button1.BackColor
End Sub

Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave

RedValue = CInt(MyBackColor.R) - CInt(Button1.FlatAppearance.MouseOverBackColor.R)
GreenValue = CInt(MyBackColor.G) - CInt(Button1.FlatAppearance.MouseOverBackColor.G)
BlueValue = CInt(MyBackColor.B) - CInt(Button1.FlatAppearance.MouseOverBackColor.B)
RAdd = 0
Gadd = 0
Badd = 0
btnTimer.Interval = 1
btnTimer.Start()



End Sub

Private Sub btnTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTimer.Tick
Select Case RedValue
Case Is <>
RAdd = RAdd - 1
RedValue = RedValue + 1
Case Is > 0
RAdd = RAdd + 1
RedValue = RedValue - 1
Case Is = 0
RAdd = RAdd + 0
End Select
Select Case GreenValue
Case Is <>
Gadd = Gadd - 1
GreenValue = GreenValue + 1
Case Is > 0
Gadd = Gadd + 1
GreenValue = GreenValue - 1
Case Is = 0
Gadd = Gadd + 0
End Select
Select Case BlueValue
Case Is <>
Badd = Badd - 1
BlueValue = BlueValue + 1
Case Is > 0
Badd = Badd + 1
BlueValue = BlueValue - 1
Case Is = 0
Badd = Badd + 0
End Select
Button1.BackColor = Color.FromArgb(CInt(Button1.FlatAppearance.MouseOverBackColor.R) + RAdd, CInt(Button1.FlatAppearance.MouseOverBackColor.G) + Gadd, CInt(Button1.FlatAppearance.MouseOverBackColor.B) + Badd)
If BlueValue = 0 And RedValue = 0 And GreenValue = 0 Then
btnTimer.Stop()

End If

Friday, January 23, 2009

Access 2003 Using DateDiff for "Business Days" or Weekdays without using outside module

I see this problem alot among developers and I just thought id share some info. If you are using a very simple DBMS like Access 2003 and can't extend functionality to your standalone application, because access 2003 doesn't let you use UDF (User defined functions) outside of itself. Here is how I came up with determing the amount of WEEKDAYS (M-F) between two dates without having to create a UDF in the DB.
SELECT  * FROM tblExample WHERE datediff('ww',[fldDate],Date(),2) + datediff('ww',[fldDate],date(),3) + datediff('ww',[fldDate],Date(),4) + datediff('ww',[fldDate],Date(),5) + datediff('ww',[fldDate],Date(),6) >= 5
That will return all records where fldDate and Date() are greater than or equal to 5 weekdays. Of course if you can use custom functions in your DBMS, then you wont need this.