-
May 28th, 2013, 11:50 AM
#1
Need help applying ComboBoxes to my program
I'm creating a diving competion program that
1. keeps score for up to 16 divers.
2. Each diver has 8 dives.
3. There are 8 judges scoring each dives w/scores ranging from 0-10 w/one decimal place.
4. Each dive has a degree of difficulty from 1-4 w/two decimal places.
5. To get their final scores, throw-out the highest & the lowest and then add the remaining 6 and multiply by the degree of difficulty.
I would like to put the scores in a Simple DropDownStyle ComboBox instead of the text file where I have the names as hown below.
Code:
Imports System.IO
Public Class Form1
Shared difficulty As Dictionary(Of String, Decimal)
Dim splashes As List(Of Splash)
Class Splash
Property DiverName As String
Property DiveType As String
Property Scores As List(Of Decimal)
Function FinalScore() As Decimal
'TODO: check there are enough scores for this to not throw an exception
Return CDec(Scores.OrderBy(Function(x) x).Skip(1).Take(Scores.Count() - 2).Average()) * difficulty(Me.DiveType)
End Function
Sub New()
Scores = New List(Of Decimal)
End Sub
End Class
Private Sub SetUpDifficulties()
difficulty = New Dictionary(Of String, Decimal)
difficulty.Add("Bellyflop", 1D)
difficulty.Add("Cannonball", 1.2D)
End Sub
Private Sub LoadData(src As String)
splashes = New List(Of Splash)
Using sr = New StreamReader(src)
While Not sr.EndOfStream()
Dim thisDive = sr.ReadLine
Dim parts = thisDive.Split(","c)
If parts.Length = 10 Then ' checks there is the correct number of elements
Dim thisSplash = New Splash
thisSplash.DiverName = parts(0)
thisSplash.DiveType = parts(1)
Dim scores As New List(Of Decimal)
For i = 2 To 9
scores.Add(CDec(parts(i)))
Next
thisSplash.Scores = scores
splashes.Add(thisSplash)
End If
End While
End Using
End Sub
Private Sub ShowScores(sender As Object, e As EventArgs)
Dim cb = DirectCast(sender, ComboBox)
Dim theDiver = cb.SelectedItem
Dim lines As String = ""
' iterate over the dives for a named diver, ordered by the name of the dive
For Each s In splashes.Where(Function(d) d.DiverName() = CStr(cb.SelectedItem)).OrderBy(Function(d) d.DiveType())
lines &= String.Format("Dive: {0} Score: {1}" & vbCrLf, s.DiveType, s.FinalScore().ToString("0.00"))
Next
TextBox1.Text = lines
End Sub
Private Sub ShowDiversNames()
ComboBox1.Items.AddRange(splashes.Select(Function(s) s.DiverName).Distinct().ToArray())
AddHandler ComboBox1.SelectedValueChanged, AddressOf ShowScores
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SetUpDifficulties()
LoadData("C:\temp\dives.txt")
ShowDiversNames()
End Sub
End Class
I experimented with creating 2 Simple DropDownStyle ComboBoxes in a new form to allow only up to 3 numbers including a period as I need the scores to be 1-10 w/1 decimal place but in doing so, I'm unable to use the backspace or delete buttons. Also, is it possible to force a period so that everytime I type in a number, a period is automatically inserted?
Code:
Public Class form1
Dim DSep As String = "."
Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
If Char.IsNumber(e.KeyChar) = False And Microsoft.VisualBasic.AscW(e.KeyChar) <> 46 Then
If e.KeyChar = CChar(DSep) Then
e.Handled = True
ElseIf Microsoft.VisualBasic.AscW(e.KeyChar) = 13 Then
Me.ComboBox2.Select() ''<---jump to next text box after press Enter
e.Handled = False
Else
e.Handled = True
End If
End If
End Sub
End Class
Any help is greatly appreciated!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|