|
|
![]() |
|
||
|
|
|||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic Projects WritePrivateProfileString: Creating a Quiz Application Step 2: Building the 'TopScores' Form |
|
| Posted: | Tuesday August 24, 1999 |
| Updated: | Monday December 26, 2011 |
| Applies to: | VB4-32, VB5, VB6, and VB3, VB4-16 with appropriate declarations |
| Developed with: | VB6, Windows NT4 |
| OS restrictions: | None |
| Author: | VBnet - Randy Birch |
| Step 1: Introduction and BAS
Module Step 2: Building the 'TopScores' Form Step 3: Building the Quiz Topic Selection Form Step 4: Building the Main Quiz Form Form Illustration Layouts Downloadable KidzQuiz INI file |
|
|
Related: |
WritePrivateProfileString: INI Files - The Basics WritePrivateProfileString: INI Files - Saving Entire Sections |
| Prerequisites |
| Step 1 completed.. |
|
|
This
page concerns itself with he code surrounding the Top Scores form and its child, the Add Name form.
|
| Form Code: frmHiScores |
|
|
| Add a new form to the project and name it frmHiScores. This form has a list (List1), a command button (Command1), and a few labels of any name. The 'Top Scores' name at the top of the form is picture inside an image control; you can replace this with anything you want. Add the following to this form: |
|
|
Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved. ' Some pages may also contain other copyrights by the author. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Distribution: You can freely use this code in your own ' applications, but you may not reproduce ' or publish this code on any web site, ' online service, or distribute as source ' on any media without express permission. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Sub Command1_Click()
'just hide it
Me.Hide
End Sub
Private Sub Form_Activate()
'if GetHiScoreNameFlag flag > 0, in the
'calling procedure show the add name dialog
If GetHiScoreNameFlag > 0 Then
frmHiScoreName.Show 1
End If
End Sub
Private Sub Form_Load()
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
'load the existing high scores data
Dim totalscores As Long
Call LoadTopScores(totalscores)
'if there was a file error, or there is nothing
'yet to show, then just exit at this point
If totalscores <> 0 Then
'must be some data there, so...
Call UpdateList(totalscores)
'pause a second to let Windows display the list
DoEvents
End If
End Sub
Private Sub LoadTopScores(totalscores As Long)
Dim hFile As Long
Dim cnt As Long
hFile = FreeFile
'assure that the specified file exists
If Not FileExists(sHighScoreFile) Then
Select Case MsgBox("The quiz High Scores file was not found!" & _
vbCrLf & vbCrLf & sHighScoreFile & vbCrLf & vbCrLf & _
"Do you want to create a new one now?", _
vbYesNo Or vbQuestion, "Quiz Error")
Case vbYes:
Open sHighScoreFile For Random Access Write As hFile Len = Len(TopScores)
Put #hFile, , TopScores
Close hFile
totalscores = 0
Case Else
Exit Sub
End Select
End If
'open the scores file, and gulp in the whole thing in 1 call
Open sHighScoreFile For Random Access Read As hFile Len = Len(TopScores)
Get #hFile, , TopScores
Close hFile
'because some might be blank, and we don't want
'blank records in the listbox, determine where
'the data ends by finding the first score = 0.
For cnt = 1 To 40
If TopScores.SScores(cnt) = 0 Then Exit For
totalscores = cnt
Next cnt
End Sub
Private Sub UpdateList(totalscores As Long)
Dim r As Long, cnt As Long
'As the data is stored in a random fashion
'(ie in the order of the entry), sort the
'data in descending order form the highest scorer
QuickSortScores TopScores, 1, totalscores
'add the sorted data to the listbox, separating
'each line item with a tab character
For cnt = 1 To totalscores
List1.AddItem Trim$(TopScores.SNames(cnt)) & vbTab & _
TopScores.SScores(cnt) & vbTab & _
Format$(TopScores.SDate(cnt), "MMM DD, YYYY") & _
vbTab & Format$(cnt, "@@")
Next cnt
'set the listbox tabstops for the above info
ReDim Tabs(1 To 3) As Long
Tabs(1) = 77
Tabs(2) = 118
Tabs(3) = 195
'and set the tabs & refresh
Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 3, Tabs(1))
List1.Refresh
End Sub |
| Form Code: frmHiScoreName |
|
|
![]() Add a new form to the project and name it frmHiScoreName. This form has two labels (Label1(0) and Label1(1), a textbox (txtEntry), and two command buttons (cmdSave and cmdCancel). Add the following to this form: |
|
|
Option Explicit
Private Sub Form_Load()
'centre this form on the screen (sort-of)
'but leave most of the highscore list visible
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) / 1.35
'disable the save button until something is entered
cmdSave.Enabled = False
'add the messages
Label1(0).Caption = "Congratulations on reaching " & _
CStr(GetHiScoreNameFlag) & " percent correct!!"
Label1(1).Caption = "Enter your name for the Top of the Class!
End Sub
Private Sub Form_Activate()
'select & highlight the 'your name' phrase in the textbox
txtEntry.SelStart = 0
txtEntry.SelLength = Len(txtEntry.Text)
End Sub
Private Sub cmdCancel_Click()
'prevent a re-showing of this dialog on
'subsequent calls to show the high scores,
'and unload
GetHiScoreNameFlag = False
Unload Me
End Sub
Private Sub cmdSave_Click()
'note: you need to add code to assure that you don't
'attempt to save more than 40 records (as dimmed in the
'Type Array at present.) A better method however might
'be to keep this 40-score limit, and add code to determine
'the file position of the lowest scored member, and
'overwrite that record.
'working variables
Dim hFile As Long
Dim nextFreeEntry As Long
Dim sEntry As String
'the nextFreeEntry is equal to the number of
'current names in the HiScores listbox plus 1
nextFreeEntry = frmHiScores!List1.ListCount + 1
'add the new record to the disk file
TopScores.SNames(nextFreeEntry) = (txtEntry)
TopScores.SDate(nextFreeEntry) = DateValue(Now)
TopScores.SScores(nextFreeEntry) = GetHiScoreNameFlag
'save the name to the high scores file
hFile = FreeFile
Open sHighScoreFile For Random Access Write As hFile Len = Len(TopScores)
Put #hFile, , TopScores
Close hFile
'format a string for entry into the listbox
sEntry = Trim$(TopScores.SNames(nextFreeEntry)) & vbTab & _
TopScores.SScores(nextFreeEntry) & vbTab & _
Format$(TopScores.SDate(nextFreeEntry), "MMM DD, YYYY") & _
vbTab & Format$(nextFreeEntry, "@@")
'and add it to the list
frmHiScores!List1.AddItem sEntry
'finally, clear the existing list and call the exposed
'sub in the frmHiScores to re-sort with the new data
frmHiScores!List1.Clear
frmHiScores.UpdateList nextFreeEntry
'prevent a re-showing of this dialog on
'subsequent calls to show the high scores, and unload
GetHiScoreNameFlag = False
Unload Me
End Sub
Private Sub txtEntry_Change()
'only enable the save button if there is something to save
cmdSave.Enabled = Len(txtEntry.Text) > 0
End Sub |
| Comments |
| Save these two files, and move on to Step 3 - Building the Quiz Topic Selection Form. And don't bother to run the app yet .. all the pieces need to be in place first. |
|
|
|
|
|
|||||
|
|||||
|
|
|||||
|
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |
![]() |