Terve!
Eli olis sellanen ongelma, että pitäis saada Listboxissa olevat numerot
suuruusjärjestykseen.
Ja niiden numeroiden määrää ei tiedetä. (Highscore taulukko.)
Elikkä jos Listboxissa on esim. numerot: 1, 23, 3, 17, 80.
Listaus on seuraava:
1
17
23
3
80
eli listaa ensimmäisen numeron mukaan.
vaikka pitäis olla:
1
3
17
23
80
Listboxissa on kyllä "Sorted" ominaisuus mutta... ei niin ei.
Toivottavasti tämä auttaa:)
'Listaan on helppo lisätä; Integer, jotta järjestyy oikein
Dim tmp As New List(Of Integer)
'Lisätään tmp:hen
For i = 0 To Score.Items.Count - 1
tmp.Add(Score.Items(i))
Next
'Järjestetään
tmp.Sort()
'Vaihdetaan järjetettyyn
Score.DataSource = tmpMoikka Happy!
esim. näin...
Public Partial Class MainForm
Private HighScore As New List(Of Integer)
Public Sub New()
Me.InitializeComponent()
End Sub
Sub Button1_Click(sender As Object, e As EventArgs)
'Testi...
Dim values() As String = Split("1, 23, 3, 17, 80", ",")
For i = 0 To values.Length -1
HighScore.Add(CType(values(i), Integer))
Next
SortByValues(HighScore, listBox1)
End Sub
Public Sub SortByValues( _
lst As List(Of Integer), lstBox As ListBox)
lst.Sort
lstBox.Sorted = False
lstBox.DataSource = lst
End Sub
End ClassVau...
kyllä on helppoa ku sen osaa - HENNKKA.
KIITOKSIA VAAN PALJON!!!, oon takunnu tän kanssa jo viikon :(
Kiitti kans Nea:lle mut tolla Hennkan koodilla pääs vähän helpomalla.
Tossa vielä toi mun "viritys"
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' luetaan .txt tiedostosta pistelista
Using sr As StreamReader = New StreamReader("C:\Scores.txt")
While (sr.Peek() > -1)
ListBox1.Items.Add(sr.ReadLine)
End While
sr.Close()
End Using
Dim tmp As New List(Of Integer)
'Lisätään tmp:hen
For i = 0 To ListBox1.Items.Count - 1
tmp.Add(ListBox1.Items(i))
Next
'Järjestetään
tmp.Sort()
'Vaihdetaan järjetettyyn
ListBox1.DataSource = tmp
End Sub
End ClassKiitti vielä kerran.
-Happy-
Heippa taas!
tässä vielä yhdenlainen viritelmä...
Sub MainForm_Load(sender As Object, e As EventArgs) FileOpen(1, "C:\Scores.txt", OpenMode.Input) Dim values() As Integer = _ Array.ConvertAll(Split(InputString(1, _ CType(LOF(1), Integer)), Environment.NewLine), _ New Converter(Of String, Integer) _ (AddressOf ConvertStringToInt)) FileClose(1) SortByValues(listBox1, values) values = Nothing End Sub Sub SortByValues(lstBox As ListBox, lst() As Integer) Dim temp As New List(Of Integer) temp.AddRange(lst) temp.Sort: lstbox.Sorted = False lstbox.DataSource = temp temp = Nothing End Sub Public Shared Function ConvertStringToInt( _ ByVal str As String) As Integer Return CInt(str) End Function
Terve taas.
Tuli tossa mieleen että pelkkien pisteiden sorttaus
on vähän niiku turhaa jos ei tiedetä kenenkä pisteistä on kyse.
eli tarttis varmaan tehdä joku multidimensional array juttu,
mutta ListBox ei vissiinkään pysty käsittelemään sellasia, Vai???
ListBoxissa on kyllä MultiColumn ominaisuus,
mut ei oo hajuakaan kuinka moinen toimii..
eli texti filu ois vähän niinu tällänen...
Kalle 2345
Sami 8573
Jukka 7372
Pasi 3237
Kia 4739
jne.
jne.
Pitäs siis saada haettua "pisteiden perusteella" kuka johtaa pisteissä,
ja vielä sitte "nimen perusteella" pisteensä..
Onnistuuko tällänen vai pitääkö siirtyä johonkin DataGrid hässäkkään.
- Happy _ ???
Heippa taas!
tässä vielä toisenlainen viritelmä...
Sub Button1_Click(sender As Object, e As EventArgs)
FileOpen(1, "C:\Scores2.txt", OpenMode.Input)
Dim strLines() As string = _
Split(InputString(1, _
CType(LOF(1), Integer)),Environment.NewLine)
FileClose(1)
Redim Preserve HighScore(strLines.length -1)
Dim temp(strLines.length -1) As integer
For i As integer = 0 To strLines.Length - 1
Dim helpArray() As string = Split(strLines(i), " ")
HighScore(i).name = helpArray(0)
HighScore(i).score = CType(helpArray(1), Integer)
temp(i) = CType(helpArray(1), Integer)
helpArray = Nothing
Next
strLines = Nothing
Dim lstInt As New List(Of Integer)
lstInt.AddRange(temp)
temp = Nothing: lstInt.Sort
listBox1.Items.Clear
listBox1.Sorted = False
For i As integer = lstInt.count -1 To 0 Step -1
For j As Integer = 0 To HighScore.Length - 1
If lstInt.Item(i) = HighScore(j).Score Then
listBox1.Items.Add( HighScore(j).name _
& " " & Ctype(HighScore(j).score, String))
End If
Next j
Next i
lstInt = Nothing
End SubHeippa taas!
mikäli listboxiin halutaan 'sarakejako' niin...
Private Structure ScoreStruct
Dim name As String
Dim value As Integer
End Structure
Dim Scores() As ScoreStruct
Sub Button1_Click(sender As Object, e As EventArgs)
FileOpen(1, "C:\Scores.txt", OpenMode.Input)
Dim strLines() As string = _
Split(InputString(1, _
CType(LOF(1), Integer)), Environment.NewLine)
FileClose(1)
Redim Scores(strLines.length - 1)
Dim temp(strLines.length - 1) As integer
Dim slen As Integer
For i As integer = 0 To strLines.Length - 1
Dim helpArray() As string = Split(strLines(i), " ")
Scores(i).name = helpArray(0)
If slen < (helpArray(0).Length + _
helpArray(1).ToString.Length) Then
slen = helpArray(0).Length + _
helpArray(1).ToString.Length
End If
Scores(i).value = CType(helpArray(1), Integer)
temp(i) = CType(helpArray(1), Integer)
helpArray = Nothing
Next
strLines = Nothing
Dim lstInt As New List(Of Integer)
lstInt.AddRange(temp)
temp = Nothing: lstInt.Sort
listBox1.Font = New Font("Courier New", 8.25!, _
FontStyle.Regular, GraphicsUnit.Point, CType(0,Byte))
listBox1.Items.Clear
listBox1.Sorted = False
For i As integer = lstInt.count -1 To 0 Step -1
For j As Integer = 0 To Scores.Length - 1
If lstInt.Item(i) = Scores(j).value Then
Dim thespace As String = " "
If slen > (Scores(j).name.Length _
+ Scores(j).value.ToString.Length) Then
Dim thelen As Integer = _
slen - (Scores(j).name.Length _
+ Scores(j).value.ToString.Length)
thespace += New String(CType(" ", Char), thelen)
End If
listBox1.Items.Add(Scores(j).name _
& thespace & Ctype(Scores(j).value, String))
End If
Next j
Next i
lstInt = Nothing
End Sub
Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim sarray() As String = _
Split(ListBox1.SelectedItem.ToString, " ")
MsgBox(CStr(listBox1.SelectedIndex + 1) & _
". sija: " & sarray(0) & " - pisteet: " & _
sarray(sarray.GetUpperBound(0)).ToString)
sarray = Nothing
End SubKiitti Nea.
Nyt toimii niinku pitääkin.
For i As Integer = lstInt.Count - 1 To 0 Step -1
For j As Integer = 0 To Scores.Length - 1
If lstInt.Item(i) = Scores(j).score Then
Dim thespace As String = " "
If slen > (Scores(j).name.Length _
+ Scores(j).score.ToString.Length) Then
Dim thelen As Integer = _
slen - (Scores(j).name.Length _
+ Scores(j).score.ToString.Length)
thespace += New String(CType(" ", Char), thelen)
End If
ListBox1.Items.Add(Scores(j).name _
& thespace & CType(Scores(j).score, String))
End If
Next j
Next iTohon kohtaan piti vaan vaihtaa...
For i As Integer = lstInt.Count - 1 To 0 Step -1
For j As Integer = 0 To Scores.Length - 1
If lstInt.Item(i) = Scores(j).value Then
Dim thespace As String = " "
If slen > (Scores(j).name.Length _
+ Scores(j).value.ToString.Length) Then
Dim thelen As Integer = _
slen - (Scores(j).name.Length _
+ Scores(j).value.ToString.Length)
thespace += New String(CType(" ", Char), thelen)
End If
ListBox1.Items.Add(Scores(j).name _
& thespace & CType(Scores(j).value, String))
End If
Next j
Next iKiitoksia taas kerran..
- Happy -
Edit. Ois pitäny lukea uudemman kerran noi koodit,
Nea olikin jo kerinny vaihtamaan .score homman tohon .value jutskaa...
Heippa taas!
tässä vielä kolmannenlainen highscore viritelmä...
Public Partial Class MainForm
Private Structure ScoreStruct
Dim name As String
Dim value As Integer
End Structure
Private HighScore(9) As ScoreStruct 'Top Ten
Private ScoreChaned As Boolean = False
Private fullPath As String = String.Empty
Private PlayerName As String = String.Empty
Public Sub New()
Me.InitializeComponent()
End Sub
Sub MainForm_Load(sender As Object, e As EventArgs)
fullPath = Application.StartupPath + "\HighScore.dat"
If Dir(fullPath) <> String.Empty Then
jmpBack:
PlayerName = InputBox("Player Name:")
If PlayerName = String.Empty Then
GoTo jmpBack
ElseIf IsNumeric(PlayerName.Substring(0, 1)) Then
MsgBox("Nimen pitää alkaa kirjaimella")
PlayerName = String.Empty: GoTo jmpBack
End If
PlayerName = PlayerName.Substring(0, 1).ToUpper _
+ PlayerName.Substring(1, PlayerName.Length - 1)
FileOpen(1, fullPath, OpenMode.Binary, OpenAccess.Read)
FileGet(1, HighScore): FileClose(1)
FillListBox(listBox1)
End If
End Sub
Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs)
If ScoreChaned Then
If Dir(fullPath) <> String.Empty Then
Kill(fullPath)
End If
FileOpen(1,fullPath, OpenMode.Binary, OpenAccess.Write)
FilePut(1, HighScore): FileClose(1)
End If
End Sub
Sub CheckHighScore(ByVal result As Integer)
Dim NewScore As Boolean = False
Dim NewScoreIndex As Integer
For i As Integer = 0 To HighScore.Length - 1
If HighScore(i).value < result Then
NewScore = True
NewScoreIndex = i
Exit For
End If
Next
If NewScore Then
For i As Integer = HighScore.Length - 2 To NewScoreIndex Step - 1
HighScore(i + 1).name = HighScore(i).name
HighScore(i + 1).value = HighScore(i).value
Next
HighScore(NewScoreIndex).name = PlayerName
HighScore(NewScoreIndex).value = result
ScoreChaned = True
FillListBox(listBox1)
End If
End Sub
Sub FillListBox(lstBox As ListBox)
lstBox.Items.Clear
lstBox.Font = New Font( _
"Courier New", 8.25!, _
FontStyle.Regular, _
GraphicsUnit.Point, _
CType(0,Byte))
lstBox.Sorted = False
Dim strLen As Integer
For i As Integer = 0 To HighScore.Length - 1
If strLen < (HighScore(i).name.Length + _
HighScore(i).value.ToString.Length) Then
strLen = HighScore(i).name.Length + _
HighScore(i).value.ToString.Length
End If
Next
For i As Integer = 0 To HighScore.Length - 1
Dim theSpace As String = " "
If strLen > (HighScore(i).name.Length _
+ HighScore(i).value.ToString.Length) Then
Dim theLen As Integer = _
strLen - (HighScore(i).name.Length _
+ HighScore(i).value.ToString.Length)
theSpace += New String(CType(" ", Char), theLen)
End If
lstBox.Items.Add(HighScore(i).name _
& theSpace & HighScore(i).value)
Next
End Sub
Sub Button1_Click(sender As Object, e As EventArgs)
'Testi:
Dim value As Integer = CType((100 * Rnd(100) * 100), Integer)
MsgBox(value)
CheckHighScore(value)
End Sub
End ClassAihe on jo aika vanha, joten et voi enää vastata siihen.