What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleDisplay an optional tip of the day when a program starts
DescriptionThis example shows how to display an optional tip of the day when a program starts in Visual Basic 6. The program saves a flag in the registry indicating whether it should display tips at start up.
Keywordstotd, tip of the day, tip-of-the-day
CategoriesFiles and Directories, Software Engineering
When the program starts, it checks the Registry to see if it should display a tip. If so, it displays the tip form modally.
Private Sub Form_Load()
    ' See if we should display a tip.
    If GetSetting("TipOfTheDayOptional", _
        "Parameters", "ShowTip", "False") = "True" _
        ' Display the Tip of the Day form modally.
        frmTOTD.Show vbModal
    End If
End Sub
When the user closes the tip form, it checks the value of its "Show At Startup" check box and saves its state in the Registry.
Private Sub cmdClose_Click()
    ' Save the Show At Startup value.
    If chkShowAtStartup.Value = vbChecked Then
        SaveSetting "TipOfTheDayOptional", _
            "Parameters", "ShowTip", True
        SaveSetting "TipOfTheDayOptional", _
            "Parameters", "ShowTip", False
    End If

    Unload Me
End Sub
The program stores tips in a text file. It stores the byte offset of the next tip in the registry. Initially this should be blank or 1. To load a tip, the program fetches this offset from the registry, reads data from the file starting at that offset, and finds the next vbCrLf to see where the tip ends.
' Load the next tip from the tip file.
Private Function NextTip() As String
Dim offset_string As Integer
Dim offset As Integer
Dim tip_text As String

    ' Read the registry to get the byte offset for
    ' the next tip in the tips file.
    offset_string = GetSetting("TipOfTheDayOptional", _
        "Parameters", "NextOffset", "1")
    offset = CInt(offset_string)

    ' Get the tip.
    tip_text = TipAtOffset(offset)

    ' If the tip is blank, get the first tip.
    If tip_text = "" Then tip_text = TipAtOffset(1)

    NextTip = tip_text
End Function

' Load the indicated tip from the tip file.
Private Function TipAtOffset(ByVal offset As Integer) As _
Const MAX_TIP_LENGTH = 256

Dim fnum As Integer
Dim tip_buffer As String * MAX_TIP_LENGTH
Dim tip_text As String
Dim pos As Integer

    ' Open the tip file and read the next
    ' MAX_TIP_LENGTH bytes.
    fnum = FreeFile
    Open App.Path & "\tips.txt" For Binary As fnum
    Get #fnum, offset, tip_buffer
    Close fnum
    ' Find the end of the tip.
    tip_text = Trim$(tip_buffer)
    pos = InStr(tip_text, vbCrLf)
    If pos = 0 Then
        offset = 1
        tip_text = ""
        tip_text = Left$(tip_text, pos - 1)
        offset = offset + pos + 1
    End If
    TipAtOffset = tip_text

    ' Save the offset for the next tip.
    SaveSetting "TipOfTheDay", _
        "Parameters", "NextOffset", offset
End Function
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.