-1

我正在尝试删除前导零/“0”来设置飞机的航向和俯仰,但我该怎么做呢?在最初的几次尝试中,我使用该CInt函数将整数值转换为字符串值并将其转换回整数,但这只会出现错误。有人可以帮忙吗?提前致谢。

     Dim pitchint As Integer
    pitchint = Pitch_txt.Text
    If pitchint > 720 Then
        pitchint = pitchint - 720
        Pitch_txt.Text = pitchint
    Else
        If pitchint > 360 Then
            pitchint = pitchint - 360
            Pitch_txt.Text = pitchint
        End If
    End If

    If Pitch_txt.Text.Length <> 0 Then
        If Pitchlbl.Text <> Pitch_txt.Text Then
            Pitchlbl.Text = Pitch_txt.Text
        End If
    End If

以下是如果我想将 pitchint 放入字符串然后返回整数我会做的事情

Dim pitchint As String
    pitchint = Pitch_txt.Text
    pitchint = CInt(pitchint.TrimStart("0"))
    If pitchint > 720 Then
        pitchint = pitchint - 720
        Pitch_txt.Text = pitchint
    Else
        If pitchint > 360 Then
            pitchint = pitchint - 360
            Pitch_txt.Text = pitchint
        End If
    End If

    If Pitch_txt.Text.Length <> 0 Then
        If Pitchlbl.Text <> Pitch_txt.Text Then
            Pitchlbl.Text = Pitch_txt.Text
        End If
    End If

除了文本框和标签中返回的值相同之外,没有其他错误,即如果我输入 070,则返回的值是 070 没有变化。

4

1 回答 1

0

您对“070”的问题是因为您只处理 >360 的值,并且永远不会在嵌套 IF 的末尾分配 Pitch_txt.text = pitchint 。你在这里有一个松散的结局

If pitchint > 720 Then
    pitchint = pitchint - 720
    Pitch_txt.Text = pitchint '<--Pitch_txt.Text was assigned
Else
    If pitchint > 360 Then
        pitchint = pitchint - 360
        Pitch_txt.Text = pitchint '<--Pitch_txt.Text was assigned
    End If

    'how about if pitchint=70?
    'Pitch_txt.text was not assign with pitchint integer value 70 and will remained as "070" (string)
End If

我将您的代码重写为以下更简洁的版本:

    Dim pitchint As Integer
    pitchint = Val(Pitch_txt.Text) 'simple way will filter out trailing non-numerics input
    If pitchint > 720 Then
        pitchint -= 720
    ElseIf pitchint > 360 Then
        pitchint -= 360
    End If

    Pitch_txt.Text = pitchint '<--put this line here will solve your "070" issue

    If Pitch_txt.Text.Length <> 0 Then
        If Pitchlbl.Text <> Pitch_txt.Text Then
            Pitchlbl.Text = Pitch_txt.Text
        End If
    End If
于 2013-07-11T18:17:42.707 回答