0

現在、シリアル番号に基づいて USB デバイスとやり取りするプログラムをコーディングしようとしています。テキストボックスで適切なシリアル番号を受け入れるように部分的に機能していますが、ボックスに整数以外を入力すると、スローしてエラーになります。どうすればこれを修正できますか? これまでのところ、私はこれを回避しようとして失敗しています。簡単に言えば、プログラムでテキストボックスから 5 桁のシリアルを読み取って接続を試み、整数以外が追加された場合、シリアルが正しくないというメッセージをユーザーにスローするだけです。これが私がこれまでに持っているものです。

'Declares an integer to be used for custom serial numbers incase phidgets need to be swapped.
    Dim MC1Serial As Integer
    'Throws error if there's no real serial in the corresponding box.
    If TextSerial1.Text = 0 Then
        MsgBox("Please ensure you have a proper serial numbers in the textbox, and not 0")
    Else
        MC1Serial = TextSerial1.Text
    End If
    'Creates a new instance of a MC for the first controller.
    MC1 = New Phidget21COM.PhidgetMotorControl
    'Attempts to attach phidget and either checks the box or throws an error.
    MC1.Open(MC1Serial)
    MC1.WaitForAttachment(1000)
    If MC1.IsAttached = True Then
        CheckMC1.Checked = True
        'Enables the timer to allow the position of the buttons to update accordingly.
        TimerJoysticks.Enabled = True
    Else
        MsgBox("There was a problem finding MC1. Check connections and settings.")
    End If
4

1 に答える 1

1

できることはたくさんあります。ValFunctionはその1つです。IsNumericを使用して、入力文字列全体が実際に数値であるかどうかを確認することもできます。

If Not IsNumeric(TextSerial1.Text) or Val(TextSerial1.Text) = 0 Then
    MsgBox("Please ensure you have a proper serial numbers in the textbox, and not 0")
    ' You should exit the sub here so that the code doesn't continue.

Else
    MC1Serial = CInt(TextSerial1.Text)
End If
于 2013-02-10T15:36:12.917 に答える