Countdown to disaster - Part 5

INI Files

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

This one is going to have a lot fo code but I am not going to go over it all line by line.  What I will do is to cover each function in basics. 

I will list ALL the new code here in one block, then go over each section one by one below it.

Code: vbscript
  1. Dim BDate,BMonth,BDay
  2. '--  for INI FILE
  3. Dim IniName, IniCnt, IniTitle(100), IniDetails(100)
  4. Const ForWriting =2 : Const ForReading = 1 : Const ForAppending = 8
  5. '--- End INI
  6. Sub Object_OnScriptEnter
  7.     IniName = "CommunityBday.ini" '---- INI FILE NAME
  8.     Call ReadINI() '-- Read INI File
  9.     BMonth = ReadINIValue("Month") '--Set Initial Values
  10.     BDay = ReadINIValue("Day")
  11.     Call SetMode(ReadINIValue("Mode"))
  12.     Call GetBDay
  13.     Object.SetTimer 1, 1000
  14. End Sub
  15. Sub Object_OnTimer1
  16.     CurTime = formatdatetime(now,3)
  17.      i = DateDiff("s", Now, BDate)
  18.   DaysLeft = i \ 86400
  19.   i = i Mod 86400
  20.   HoursLeft = i \ 3600
  21.   i = i Mod 3600
  22.   MinutesLeft = i \ 60
  23.   i = i Mod 60
  24.   SecondsLeft = i
  25.   desktopx.Object("Birthday-CD-Large-days").text = DaysLeft
  26.   desktopx.Object("Birthday-CD-Large-hrs").text = HoursLeft
  27.   desktopx.Object("Birthday-CD-Large-min").text = MinutesLeft
  28.   desktopx.Object("Birthday-CD-Large-sec").text = SecondsLeft
  29. End Sub
  30. Sub Object_OnScriptExit
  31.  object.KillTimer 1
  32. End Sub
  33. Function SetMode(Mode)
  34.     Select Case Mode
  35.         Case "Mini"
  36.       desktopx.object("Birthday-CD-LargeBG").visible = False
  37.         desktopx.object("Birthday-CD-MiniBG").visible = True
  38.         Call SetINIValue("Mode","Mini")
  39.         Case "Large"
  40.         desktopx.object("Birthday-CD-MiniBG").visible = False
  41.       desktopx.object("Birthday-CD-LargeBG").visible = True
  42.       Call SetINIValue("Mode","Large")
  43.     End Select
  44. End Function
  45. Function Object_OnLButtonUpEx(obj,x,y,dragged)
  46.     If Not dragged Then
  47.         Select Case obj.name
  48.        
  49.     Case "Birthday-CD-LargeModeBtn"
  50.       Call SetMode("Large")
  51.     Case "Birthday-CD-MiniModeBTN"
  52.             Cakk SetMode("Large")
  53.     Case "Birthday-CD-Mini-PrefsBTN"
  54.       Call PrefsMenu()
  55.      
  56.     Case "Birthday-CD-Large-PrefsBTN"
  57.       Call PrefsMenu()
  58.         End Select
  59.   End If
  60. End Function
  61. Function PrefsMenu()
  62.     Set frm = DesktopX.CreateForm
  63.     frm.caption = "RSS Feeds"
  64.   frm.AddPreference "B-Month"
  65.   frm.Preference("B-Month").Type = "ComboList"
  66.   For x = 1 To 12
  67.       frm.Preference("B-Month").AddValue x
  68.   Next
  69.   frm.Preference("B-Month").DefaultValue = BMonth
  70.   frm.Preference("B-Month").Caption = "Month"
  71.   frm.AddPreference "B-Day"
  72.   frm.Preference("B-Day").Type = "ComboList"
  73.   For x = 1 To 31
  74.       frm.Preference("B-Day").AddValue x
  75.   Next
  76.   frm.Preference("B-Day").DefaultValue = BDay
  77.   frm.Preference("B-Day").Caption = "Day"
  78.     If frm.prompt Then
  79.         BMonth = frm.Preference("B-Month").value
  80.         BDay = frm.Preference("B-Day").value
  81.         Call SetINIValue("Month",BMonth)
  82.         Call SetINIValue("Day",BDay)
  83.     End If
  84.     Call GetBDay()
  85. End Function
  86. Function GetBDay()
  87.   BdayToday = False
  88.   testDate = BMonth &"/" & BDay &"/" & year(now)
  89.   CheckDate = IsDate(testDate)
  90.   If CheckDate <> True Then
  91.    Msgbox "Date: " & testDate & vbnewline & "is Invalid" & vbnewline & "Please select a new date"
  92.    Call PrefsMenu()
  93.   Else
  94.         s = DateDiff("s", Now, testdate)
  95.         If s > 0 Then
  96.             BDate = cdate(testdate)
  97.         Else
  98.           testDate = BMonth &"/" & BDay &"/" & (year(now)+1)
  99.             BDate = cdate(testdate)
  100.       End If
  101.       d = DateDiff("d", Now, testdate)
  102.       If d= 365 Then        msgbox "HAPPY BIRTHDAY!!"
  103.       desktopx.Object("Birthday-CD-Mini-BDate").text = BDate
  104.   End If
  105. End Function
  106. '---INI FILE FUNCTIONS ----
  107. Function WriteBlankINI()
  108.     '------------------ INI DEFAUTL VALUES HERE -----------------------
  109.     IniCnt = 0
  110.     IniCnt = IniCnt+ 1 : IniTitle(1) = "Mode" : IniDetails(1) = "Mini"
  111.     IniCnt = IniCnt+ 1 : IniTitle(2) = "Month"         : IniDetails(2) = 11
  112.     IniCnt = IniCnt+ 1 : IniTitle(3) = "Day"     : IniDetails(3) = 20
  113.         ' The INI File is Stored in the Directory where the Program is run
  114.     IniContents = ""
  115.     For x = 1 To INICnt   
  116.    IniContents = IniContents & IniTitle(x) & "|" & IniDetails(x) & vbnewline
  117.     Next
  118.     fn = desktopx.ExecutableDirectory & IniName
  119.     System.SimpleWrite fn, IniContents, 1
  120. End Function
  121. Function ReadINI()
  122.     Call CheckForINI()
  123.     fn = desktopx.ExecutableDirectory & IniName
  124.     TextList = System.SimpleRead(fn, 1)
  125.   Lines = Split(TextList, Chr(10))
  126.     IniCnt = 0
  127.     For A = UBound(Lines) To LBound(Lines) Step -1
  128.          If Len(Lines(A)) > 0 Then
  129.           t = instr(Lines(A),"|")
  130.           IniCnt = IniCnt + 1
  131.           IniTitle(IniCnt) = left(Lines(A),t-1)
  132.           IniDetails(IniCnt) = mid(Lines(A),t+1)
  133.           l = len(IniDetails(IniCnt))
  134.           IniDetails(IniCnt) = left(IniDetails(IniCnt),l-1)
  135.       End If
  136.     Next
  137. End Function
  138. Function WriteINI()
  139.     ' The INI File is Stored in the Directory where the Program is run
  140.     IniContents = ""
  141.     For x = 1 To INICnt   
  142.    IniContents = IniContents & IniTitle(x) & "|" & IniDetails(x) & vbnewline
  143.     Next
  144.     fn = desktopx.ExecutableDirectory & IniName
  145.     System.SimpleWrite fn, IniContents, 1
  146. End Function
  147. Function CheckForINI()
  148.     fn = desktopx.ExecutableDirectory & IniName
  149.     Set filesys = CreateObject("Scripting.FileSystemObject")
  150.     temp = filesys.FileExists(fn)
  151.     path = filesys.GetAbsolutePathName(fn)
  152.     If Not filesys.FileExists(fn) Then
  153.        Call WriteBlankINI()
  154.     End If
  155. End Function
  156. Function ReadINIValue(Item)
  157.     ReadINIValue = ""
  158.     For x = 1 To INICnt
  159.         If UCase(IniTitle(x)) = UCase(Item) Then    temp = IniDetails(x)     
  160.     Next
  161.     ReadINIValue = Temp
  162. End Function
  163. Function SetINIValue(Item,value)
  164.     For x = 1 To INICnt
  165.         If UCase(IniTitle(x)) = UCase(Item) Then    IniDetails(x) = Value
  166.     Next
  167.  Call WriteINI()
  168. End Function

I know its a lot of code but most of it is to read/write/create the INI file, its a group of functions that can be used over and over, I like to use this set because its simple, and VERY customizable, and much easier to not have to re-write code over and over.

 

---- PART 1 ---
INI File info and basic reading

Code: vbscript
  1. Sub Object_OnScriptEnter
  2.     IniName = "CommunityBday.ini" '---- INI FILE NAME
  3.     Call ReadINI() '-- Read INI File
  4.     BMonth = ReadINIValue("Month") '--Set Initial Values
  5.     BDay = ReadINIValue("Day")
  6.     Call SetMode(ReadINIValue("Mode"))
  7.     Call GetBDay
  8.     Object.SetTimer 1, 1000
  9. End Sub

We set the ININame to the name of the ini file we want to use, in this case "CommunityBDay.ini".
Then we call the ReadINI function (see later in this article). This loads all the ini values and then allows us to read/write these as needed.  You can see the calls to ReadINIValue for each item in the ini file.

Next Lets cover the basic operation of the INI Functions.

Code: vbscript
  1. Function WriteBlankINI()
  2.     '------------------ INI DEFAUTL VALUES HERE -----------------------
  3.     IniCnt = 0
  4.     IniCnt = IniCnt+ 1 : IniTitle(1) = "Mode" : IniDetails(1) = "Mini"
  5.     IniCnt = IniCnt+ 1 : IniTitle(2) = "Month"         : IniDetails(2) = 11
  6.     IniCnt = IniCnt+ 1 : IniTitle(3) = "Day"     : IniDetails(3) = 20
  7.         ' The INI File is Stored in the Directory where the Program is run
  8.     IniContents = ""
  9.     For x = 1 To INICnt   
  10.    IniContents = IniContents & IniTitle(x) & "|" & IniDetails(x) & vbnewline
  11.     Next
  12.     fn = desktopx.ExecutableDirectory & IniName
  13.     System.SimpleWrite fn, IniContents, 1
  14. End Function

WriteBlankINI - is used to create a blank (well it will have default values) INI file.
The first section is used to setup the defaults.
We clear the ini count, then we set the initial values for each item, in this case Month/Day/Mode.
Then it cycles thru these and writes them to the ini file.

Code: vbscript
  1. Function ReadINI()
  2.     Call CheckForINI()
  3.     fn = desktopx.ExecutableDirectory & IniName
  4.     TextList = System.SimpleRead(fn, 1)
  5.   Lines = Split(TextList, Chr(10))
  6.     IniCnt = 0
  7.     For A = UBound(Lines) To LBound(Lines) Step -1
  8.          If Len(Lines(A)) > 0 Then
  9.           t = instr(Lines(A),"|")
  10.           IniCnt = IniCnt + 1
  11.           IniTitle(IniCnt) = left(Lines(A),t-1)
  12.           IniDetails(IniCnt) = mid(Lines(A),t+1)
  13.           l = len(IniDetails(IniCnt))
  14.           IniDetails(IniCnt) = left(IniDetails(IniCnt),l-1)
  15.       End If
  16.     Next
  17. End Function

ReadINI - Reads the ini file loads them into a array to be used by the other functions.

Code: vbscript
  1. Function WriteINI()
  2.     ' The INI File is Stored in the Directory where the Program is run
  3.     IniContents = ""
  4.     For x = 1 To INICnt   
  5.    IniContents = IniContents & IniTitle(x) & "|" & IniDetails(x) & vbnewline
  6.     Next
  7.     fn = desktopx.ExecutableDirectory & IniName
  8.     System.SimpleWrite fn, IniContents, 1
  9. End Function

WriteINI - Writes out the INI File info to the file you put in as the name at the top of the code.

Code: vbscript
  1. Function CheckForINI()
  2.     fn = desktopx.ExecutableDirectory & IniName
  3.     Set filesys = CreateObject("Scripting.FileSystemObject")
  4.     temp = filesys.FileExists(fn)
  5.     path = filesys.GetAbsolutePathName(fn)
  6.     If Not filesys.FileExists(fn) Then
  7.        Call WriteBlankINI()
  8.     End If
  9. End Function

CheckForINI- Checks to see if the INI file exists, if it doesnt it calls the WriteBlankINI function to create the file with the defaults.

Code: vbscript
  1. Function ReadINIValue(Item)
  2.     ReadINIValue = ""
  3.     For x = 1 To INICnt
  4.         If UCase(IniTitle(x)) = UCase(Item) Then    temp = IniDetails(x)     
  5.     Next
  6.     ReadINIValue = Temp
  7. End Function

ReadINIValue - Searches the array (created by the ReadINI function) for the ITEM you have asked it to find.
It returns the value of the ITEM.  Its a wonderful piece of script that makes reading an INI file VERY simple!

Code: vbscript
  1. Function SetINIValue(Item,value)
  2.     For x = 1 To INICnt
  3.         If UCase(IniTitle(x)) = UCase(Item) Then    IniDetails(x) = Value
  4.     Next
  5.  Call WriteINI()
  6. End Function

SetINIValue - Updates the Array with the item and value you send it, then it calls the WriteINI function to write out the updated INI file.  Soooo smooth.

Code: vbscript
  1. Function SetMode(Mode)
  2.     Select Case Mode
  3.         Case "Mini"
  4.       desktopx.object("Birthday-CD-LargeBG").visible = False
  5.         desktopx.object("Birthday-CD-MiniBG").visible = True
  6.         Call SetINIValue("Mode","Mini")
  7.         Case "Large"
  8.         desktopx.object("Birthday-CD-MiniBG").visible = False
  9.       desktopx.object("Birthday-CD-LargeBG").visible = True
  10.       Call SetINIValue("Mode","Large")
  11.     End Select
  12. End Function

SetMode - Sets the visibility of the mini and large items.  before this was in another location, im doing it here to make it so that you can change mode by one simple call.  IE: Call SetMode("Mini").
This might be changed later to allow things to fade, or move around, who knows.  Or it might just stay as is.

There are some other changes to the code to incorporate the INI file, like adding...

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

...to the Prefs Menu, this writes out the updated info to the INI file, as soon as you select it.

There are some other changes made, nothing major.

So far here is what works:

  • Read the settins from an ini file
  • update the ini file when changes are made
  • change between mini and large mode
  • pick a date from the pref's menu, by month/day
  • check this date to see if its a valid date, if not go back to the pref's menu
  • countdown to the above date

We are really close to a working BETA here now.  Im not 100% sure what else needs added, but i am going to pass this around at this point to see what people want done. I think it will need to have some kind of way to make this work in other parts of the world, but im not 100% sure how to do that at this point.

Is anyone still out there?
RomanDA

RomanDA
Reply #1 Thursday, October 30, 2008 6:23 PM

Update, found a bug:

Code: vbscript
  1. Function SetMode(Mode)
  2.     Select Case Mode
  3.         Case "Mini"
  4.       desktopx.object("Birthday-CD-LargeBG").visible = False
  5.         desktopx.object("Birthday-CD-MiniBG").visible = True
  6.         Call SetINIValue("Mode","Mini")
  7.         Case "Large"
  8.         desktopx.object("Birthday-CD-MiniBG").visible = False
  9.       desktopx.object("Birthday-CD-LargeBG").visible = True
  10.       Call SetINIValue("Mode","Large")
  11.     End Select
  12. End Function
  13. Function Object_OnLButtonUpEx(obj,x,y,dragged)
  14.     If Not dragged Then
  15.         Select Case obj.name
  16.        
  17.     Case "Birthday-CD-LargeModeBtn"
  18.       Call SetMode("Large")
  19.     Case "Birthday-CD-MiniModeBTN"
  20.             Call SetMode("Mini")
  21.     Case "Birthday-CD-Mini-PrefsBTN"
  22.       Call PrefsMenu()
  23.      
  24.     Case "Birthday-CD-Large-PrefsBTN"
  25.       Call PrefsMenu()
  26.         End Select
  27.   End If
  28. End Function

Replace these 2 functions in the code above.

RomanDA
Reply #2 Friday, October 31, 2008 7:26 AM

I have some more to do here.. I need to add some animated "flames" to the candles, and then do something unique on the actual birthday, not sure what.

if ANYONE is reading any of this, please leave a comment.. i dont think anyone is, but i will continue for my own future sake.

teddybearcholla
Reply #3 Friday, October 31, 2008 8:06 AM

I'm reading them, I'm sure alot of others are also.  Thanks for doing this!!!

Island Dog
Reply #4 Friday, October 31, 2008 8:35 AM

Thank you for this.  It is just a great tutorial.

 

ZubaZ
Reply #5 Friday, October 31, 2008 8:56 AM

Not sure I'm learning anything (because I'm a bit slow; not because of your awesome tutorials) but I'm reading it all.

Thanks David.

RomanDA
Reply #6 Monday, February 27, 2012 10:37 AM

Dang this is good stuff.. love to do a Google search and my own stuff shows up.. LOL

ZubaZ
Reply #7 Monday, February 27, 2012 11:17 AM

RomanDA
Dang this is good stuff.. love to do a Google search and my own stuff shows up.. LOL
I LOVE when that happens!

Wizard1956
Reply #8 Monday, February 27, 2012 2:03 PM

It's good seeing you in the forums again, RomanDA. 

RomanDA
Reply #9 Monday, February 27, 2012 2:50 PM

Thanks Wiz, Just dont look for me too much.

The project im working on is for work, and my work schedule is a killer (110 hr OT last month, about 75 this one).

I miss messing with DX, and love all the crap it can do, except screw me like it did today.. for that i have a boss.

LOL

Jafo
Reply #10 Monday, February 27, 2012 9:29 PM

RomanDA
The project im working on is for work, and my work schedule is a killer (110 hr OT last month, about 75 this one).

Oh, come on, Dave....when it's 110 hr OT in a WEEK ....then we'll know you're busy....

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