我正在尝试删除前导零/“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没有变化。
答案 0 :(得分:0)
“070”的问题是因为您只处理值&gt; 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