Countdown to disaster - Part 4

Preferences

Thursday, October 30, 2008 by RomanDA | Discussion: DesktopX Tutorials

Welcome back.  I hope someone is getting something out of all this, thanks for the great comments.

Moving on.  In order to make this useable for everyone, and not just for ME (IE My Birthday!), we want to offer the user a way to change the birthday (Month/Day).  To do this we want to add a button to the object that calls up a custom preferences window where the user can select the day/month for their birthday.

One thing that we will be changing here to the origional code is that we now need to use a month/day for the birthday not a "hard coded" date.  The reason for using the month/day is so that it will "roll-over" to the next year when it loads.  In order to make this work we are changing the top part of the code to:

Code: vbscript
  1. Dim BDate,BMonth,BDay
  2. Sub Object_OnScriptEnter
  3.   BMonth = 11
  4.   BDay = 20
  5.   Call GetBDay
  6.   Object.SetTimer 1, 1000
  7. End Sub

You will see that I added (2) more vars to the top so that they can be passed to all the other funtions.
Also added is the code to SET a default month/day.  This will be changed in the coming parts to allow it to read this from an INI file.  (I like INI's better than setting things in the REG, it is easier to debug later)

ADDED
BMonth = 11
BDay = 20

These setup the defaults

Also added is a call to "GetBDay"  this function is shown here:

Code: vbscript
  1. Function GetBDay()
  2.   BdayToday = False
  3.   testDate = BMonth &"/" & BDay &"/" & year(now)
  4.     s = DateDiff("s", Now, testdate)
  5.     If s > 0 Then
  6.         BDate = cdate(testdate)
  7.     Else
  8.       testDate = BMonth &"/" & BDay &"/" & (year(now)+1)
  9.         BDate = cdate(testdate)
  10.   End If
  11.   d = DateDiff("d", Now, testdate)
  12.   If d= 365 Then        msgbox "HAPPY BIRTHDAY!!"
  13. End Function

The GetBDay Function above takes and "builds" a date from the supplied month/day + the current Year.

Code: vbscript
  1. testDate = BMonth &"/" & BDay &"/" & year(now)


It then checks to see if this date has already passed or if its still in the future.
Doing this via a check to see how many "seconds" have passed or will pass between NOW and the Date it made from the Month/Day.

If the # of seconds (s) is > 0 this means that the date has yet to come to pass, so keeping this year will work fine.
Else it sets the date to next year.
The CDate function converts a valid date and time expression to type Date, and returns the result.
I use this as a "test" for the date.

Code: vbscript
  1.     s = DateDiff("s", Now, testdate)
  2.     If s > 0 Then
  3.         BDate = cdate(testdate)
  4.     Else
  5.       testDate = BMonth &"/" & BDay &"/" & (year(now)+1)
  6.         BDate = cdate(testdate)
  7.   End If

One other thing I check is to see if the # of days between NOW and the BDate is 365 (ie 1 year from today, so that makes today your birthday!). All it does for now is show a message box that says "HAPPY BIRTHDAY".

Code: vbscript
  1.   d = DateDiff("d", Now, testdate)
  2.   If d= 365 Then        msgbox "HAPPY BIRTHDAY!!"

---- Adding the Preference Button ----

If you look at the image above you will see I have added a ( ) button next to the previous one we made to change the state to LARGE mode (from MINI).  This button is a CLONE of the other button, but I changed the images to that circle.  Again, all these images could change easily.  From this button we will call up the Preferences window.  One thing else that was changed was the TOOL TIP to show that your calling the pref's window.

In order to make this work we have to change the code for the "left buttin" function

Code: vbscript
  1. Function Object_OnLButtonUpEx(obj,x,y,dragged)
  2.     If Not dragged Then
  3.         Select Case obj.name
  4.        
  5.     Case "Birthday-CD-LargeModeBtn"
  6.       desktopx.object("Birthday-CD-MiniBG").visible = False
  7.       desktopx.object("Birthday-CD-LargeBG").visible = True
  8.     Case "Birthday-CD-MiniModeBTN"
  9.       desktopx.object("Birthday-CD-LargeBG").visible = False
  10.       desktopx.object("Birthday-CD-MiniBG").visible = True
  11.     Case "Birthday-CD-Mini-PrefsBTN"
  12.       Call PrefsMenu()
  13.         End Select
  14.   End If
  15. End Function

All that was added here was the PrefsBTN part to call a function called PREFSMENU

The PREFSMENU Function is used to call up the preference window and set the month/day.
The code for that is here:

Code: vbscript
  1. Function PrefsMenu()
  2.     Set frm = DesktopX.CreateForm
  3.     frm.caption = "RSS Feeds"
  4.   frm.AddPreference "B-Month"
  5.   frm.Preference("B-Month").Type = "ComboList"
  6.   For x = 1 To 12
  7.       frm.Preference("B-Month").AddValue x
  8.   Next
  9.   frm.Preference("B-Month").DefaultValue = BMonth
  10.   frm.Preference("B-Month").Caption = "Month"
  11.   frm.AddPreference "B-Day"
  12.   frm.Preference("B-Day").Type = "ComboList"
  13.   For x = 1 To 31
  14.       frm.Preference("B-Day").AddValue x
  15.   Next
  16.   frm.Preference("B-Day").DefaultValue = BDay
  17.   frm.Preference("B-Day").Caption = "Day"
  18.     If frm.prompt Then
  19.         BMonth = frm.Preference("B-Month").value
  20.         BDay = frm.Preference("B-Day").value
  21.         Call GetBDay()
  22.     End If
  23. End Function

This is not a really complicated code, but I will break it down here.

Code: vbscript
  1. Set frm = DesktopX.CreateForm

-- Create the Form for us.

Code: vbscript
  1. frm.caption = "RSS Feeds"

-- Caption for the Window

-- Sets the B-Month Drop down, sets the name, calls it as a Combolist, and then adds the values via a simple
-- look so that it will show 1-12 in the drop down
-- It also sets the "default" based on what the default BMonth is set to.

Code: vbscript
  1. frm.AddPreference "B-Month"
  2. frm.Preference("B-Month").Type = "ComboList"
  3. For x = 1 To 12
  4.     frm.Preference("B-Month").AddValue x
  5. Next
  6. frm.Preference("B-Month").DefaultValue = BMonth
  7. frm.Preference("B-Month").Caption = "Month"

-- Same thing for the Day, only we make this 31, since  we cant change this once the window is open there is no way
-- to interact with the previous drop down so we have to use the "MAX" days of 31, so it COULD be possible to
-- select a date that is invalid, we will have to add a check for that in the future.

Code: vbscript
  1. frm.AddPreference "B-Day"
  2. frm.Preference("B-Day").Type = "ComboList"
  3. For x = 1 To 31
  4.     frm.Preference("B-Day").AddValue x
  5. Next
  6. frm.Preference("B-Day").DefaultValue = BDay
  7. frm.Preference("B-Day").Caption = "Day"


-- Once the user selects their settings and picks "OK" it moves on to the next section
-- If they hit Cancel it will ignor the next section
-- It sets the value of the vars to the ones the user selected.
-- HERE is where we would need to do some kind of check for a valid date. (Will look at that later)

Code: vbscript
  1. If frm.prompt Then
  2.    BMonth = frm.Preference("B-Month").value
  3.    BDay = frm.Preference("B-Day").value
  4.    Call GetBDay()
  5. End If

This allows the user to pick a month/day and then it sets it.  This is JUST until the gadget closes, because we are not saving this anywhere at this time.  Will add that in the next step, as well as the check for a valid date.

Enjoy,
RomanDA

RomanDA
Reply #1 Thursday, October 30, 2008 8:57 AM

Quick Update to the code that checks for the birthday.  I added a simple function and check to see if the date selected was a "valid" date.  So far this is all working in the USA, i dont know about other places yet.

New  code:

Code: vbscript
  1. Function GetBDay()
  2.   BdayToday = False
  3.   testDate = BMonth &"/" & BDay &"/" & year(now)
  4.  
  5.   CheckDate = IsDate(testDate)
  6.   If CheckDate <> True Then
  7.    Msgbox "Date: " & testDate & vbnewline & "is Invalid" & vbnewlne & "Please select a new date"
  8.    Call PrefsMenu()
  9.   Else
  10.         s = DateDiff("s", Now, testdate)
  11.         If s > 0 Then
  12.             BDate = cdate(testdate)
  13.         Else
  14.           testDate = BMonth &"/" & BDay &"/" & (year(now)+1)
  15.             BDate = cdate(testdate)
  16.       End If
  17.       d = DateDiff("d", Now, testdate)
  18.       If d= 365 Then        msgbox "HAPPY BIRTHDAY!!"
  19.       desktopx.Object("Birthday-CD-Mini-BDate").text = BDate
  20.   End If
  21. End Function

Look it over, its simple, the IsDate function returns TRUE or FALSE so that is passed onto an IF.
Nothing big.

Please login to comment and/or vote for this skin.

Welcome Guest! Please take the time to register with us.
There are many great features available to you once you register, including:

  • Richer content, access to many features that are disabled for guests like commenting on the forums and downloading skins.
  • Access to a great community, with a massive database of many, many areas of interest.
  • Access to contests & subscription offers like exclusive emails.
  • It's simple, and FREE!



web-wc01