Wenn Du mit Zahlen arbeitest, benutze numerische Datentypen !!!
Und für die Anzeige Konvertierungsfunktionen (z.B. ToString) !!!
Public Class frmClassification
Dim WithEvents txtInput As New TextBox _
With {.Parent = Me, .Width = 300}
Dim classlabels As New List(Of Label)
Dim classfreq As New List(Of Label)
Dim class_ug As Decimal = -1.2D
Dim class_width As Decimal = 0.3D
Private Sub frmClassification_Load(sender As System.Object, _
e As System.EventArgs) Handles MyBase.Load
Me.Size = New Size(300, 350)
Dim tp As Integer = 50
Dim lh As Integer = 24 'Labelhöhe
Dim lw As Integer = 120 'Labelbreite
'Labels erstellen
classlabels.Add(New Label With _
{.Text = "kleiner gleich -1,2", _
.Parent = Me, .Width = lw, .Top = tp})
classfreq.Add(New Label With _
{.Parent = Me, .Left = lw + 5, .Width = 60, .Top = tp, _
.Text = "0"})
Dim ug As Decimal = class_ug, w As Decimal = class_width
For i As Integer = 1 To 8
classlabels.Add(New Label With _
{.Parent = Me, .Width = lw, .Top = tp + i * lh, _
.Text = ug.ToString("0.00") & " bis " & (ug + w).ToString( _
"0.00")})
classfreq.Add(New Label With _
{.Parent = Me, .Left = lw + 5, .Width = 60, .Top = tp + i * lh, _
.Text = "0"})
ug += w
Next i
classlabels.Add(New Label With _
{.Text = "größer 1,2", _
.Parent = Me, .Width = lw, .Top = tp + lh * 9})
classfreq.Add(New Label With _
{.Parent = Me, .Left = lw + 5, .Width = 60, .Top = tp + 9 * lh, _
.Text = "0"})
End Sub
Private Sub txtInput_KeyDown(sender As Object, _
e As System.Windows.Forms.KeyEventArgs) Handles txtInput.KeyDown
Dim number As Decimal
If e.KeyCode = Keys.Enter Then
If Not Decimal.TryParse(txtInput.Text, number) Then
MsgBox("Keine gültige Zahl") : Exit Sub
End If
number = Math.Round(number, 2)
If number <= -1.2 Then
classfreq(0).Text = (CInt(classfreq(0).Text) + 1).ToString
ElseIf number > 1.2 Then
classfreq(9).Text = (CInt(classfreq(9).Text) + 1).ToString
Else
For i As Integer = 1 To 8
If number <= class_ug + class_width * i Then
classfreq(i).Text = _
(CInt(classfreq(i).Text) + 1).ToString
Exit For
End If
Next i
End If
End If
End Sub
End Class |