Ett saxat exempel: Löste sig på följande sättSv: High scoore
Example: Saving Text to a File
<code>
SaveFileDialog1.Filter = _
"Text Files|*.txt|All Files|*.*"
SaveFileDialog1.FilterIndex = 0
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
Dim FS As FileStream = SaveFileDialog1.OpenFile
Dim SW As New StreamWriter(FS)
SW.Write(TextBox1.Text)
SW.Close()
FS.Close()
End If
</code>
<code>
OpenFileDialog1.Filter = _
"Text Files|*.txt|All Files|*.*"
OpenFileDialog1.FilterIndex = 0
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
Dim FS As FileStream
FS = OpenFileDialog1.OpenFile
Dim SR As New StreamReader(FS)
TextBox1.Text = SR.ReadToEnd
SR.Close()
FS.Close()
End If
</code>Sv: High scoore
'==================================================
' File Handling Subroutines.
'==================================================
Sub WriteScoreFile(names() As String, scores() As Integer)
Open "highscr.dat" For Binary As #1
Put #1, , names
Put #1, , scores
Close #1
End Sub
Sub ReadScoreFile(names() As String, scores() As Integer)
Open "highscr.dat" For Binary As #1
Get #1, , names
Get #1, , scores
Close #1
End Sub
Sub AddToScoreFile()
Dim names(MAXHIGHSCORES - 1) As String
Dim scores(MAXHIGHSCORES - 1) As Integer
Dim name As String
Dim i As Integer
Dim X As Integer
Dim NewHighScore As Boolean
Dim HighScore As Integer
Dim NameLen As Integer
ReadScoreFile names, scores
X = 0
NewHighScore = False
Do
HighScore = scores(X)
If PlayerScores(Player) > HighScore Then
NewHighScore = True
For i = MAXHIGHSCORES - 2 To X Step -1
names(i + 1) = names(i)
scores(i + 1) = scores(i)
Next i
name = InputBox _
("Please enter your name for the high-score board.", _
"New High Score")
NameLen = Len(name)
If NameLen > 10 Then
name = Left$(name, 10)
NameLen = 10
End If
For i = NameLen To 9
name = name & "."
Next i
name = UCase(name)
names(X) = name
scores(X) = PlayerScores(Player)
WriteScoreFile names, scores
End If
X = X + 1
Loop While ((Not NewHighScore) And (X < MAXHIGHSCORES))
If NewHighScore Then ShowScoreFile (X - 1)
End Sub
Sub ShowScoreFile(NameToHighlight As Integer)
Dim names(MAXHIGHSCORES - 1) As String
Dim scores(MAXHIGHSCORES - 1) As Integer
Dim str As String
Dim X As Integer
ReadScoreFile names, scores
ScoreForm.Left = CardForm.Left + 100
ScoreForm.Top = CardForm.Top + 100
ScoreForm.Show
For X = 0 To MAXHIGHSCORES - 1
If X < 9 Then
str = " " & X + 1
Else
str = X + 1
End If
str = str & ". " & names(X) & scores(X)
ScoreForm.CurrentX = 20
ScoreForm.CurrentY = 20 + X * LINESPACING
If X = NameToHighlight Then ScoreForm.ForeColor = vbBlue
ScoreForm.Print str
ScoreForm.ForeColor = vbBlack
Next X
End Sub
Saxat ur spelprogramering på tre veckor av Clayton Walnum .