检查值是否与数据类型匹配

时间:2018-05-01 23:46:18

标签: .net vb.net winforms visual-studio

我正在尝试创建一个程序,允许用户输入开始和结束值并选择数据类型。如果输入与数据类型范围匹配,程序将从起始值到结束值和时间计数,完成后程序将显示所花费的时间。

我在尝试仅允许数字输入,十进制输入和负值时遇到一些困难。我目前有:

Private Sub txtInputStart_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInputStart.KeyPress
    If Not IsNumeric(e.KeyChar) AndAlso System.Convert.ToByte(e.KeyChar) <> 8 AndAlso Convert.ToByte(e.KeyChar) <> 45 AndAlso System.Convert.ToByte(e.KeyChar) <> 46 Then
        textError.Text = "Please enter numbers, decimals or negative values only"
        e.Handled = True
    End If
End Sub

    Private Sub txtInputFinish_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInputFinish.KeyPress
    If Not IsNumeric(e.KeyChar) AndAlso System.Convert.ToByte(e.KeyChar) <> 8 AndAlso Convert.ToByte(e.KeyChar) <> 45 AndAlso System.Convert.ToByte(e.KeyChar) <> 46 Then
        textError.Text = "Please enter numbers, decimals or negative values only"
        e.Handled = True
    End If
End Sub

我在实现双数据类型和十进制数据类型时遇到了一些麻烦,我不确定如何检查输入是否适合这些数据类型。我目前用于数据类型测试的是:

Select Case (cbDataType.SelectedIndex)
        Case 0 'Byte'

            If (lcValueStart < 0 Or lcValueFinish > 255) Then
                textError.Text = "The selected data type Byte can only hold values from 0 through to 255, please reset the form and than try again"
            Else
                Do Until lcValueStart = lcValueFinish
                    lcValueStart = lcValueStart + 1
                Loop
                myTimer.Stop()
                timeValue = (myTimer.Elapsed.ToString)
                lblTimer.Text = timeValue
                MessageBox.Show("Counting success")
                textInfo.Text = "The data type Byte holds values from 0 up to 255 and can be assigned by: Dim varName As Byte = x"

            End If

        Case 1 'UShort'

            If (lcValueStart < 0 Or lcValueFinish > 65535) Then
                textError.Text = "The selected data type UShort can only hold values from 0 through to 65535, please reset the form and than try again"
            Else
                myTimer.Stop()
                timeValue = (myTimer.Elapsed.ToString)
                lblTimer.Text = timeValue
                MessageBox.Show("Counting success")
                textInfo.Text = "The data type UShort holds values from 0 up to 65535 and can be assigned by: Dim varName As UShort = x"
            End If

这些数据类型很容易,因为它们不带小数。有人能在这里帮忙吗?

1 个答案:

答案 0 :(得分:0)

我不会测试每次按键。我们将在完成输入输入后使用TryParse进行测试。然后单击“确定”按钮。我假设你把你的DataTypes放在一个列表框中。我已经向您展示了Double的示例,您可以编写其余的代码。我没有包括计时器,因为我不太确定你的计时。

Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        Dim strStart As String = txtStart.Text
        Dim strFinish As String = txtFinish.Text
        Debug.Print(lstTypes.SelectedItem.ToString)
        Select Case lstTypes.SelectedItem.ToString
            Case "Byte"
            Case "Double"
                Dim dbl As Double
                If Double.TryParse(strStart, dbl) Then
                    MessageBox.Show("Your Starting value is within the range of a Double")
                Else
                    MessageBox.Show("Sorry, your Starting value is not in the range of a Double")
                End If
End Select