2013-07-10 21 views
-1

我试图删除前导零/“0”来设置飞机的标题和音高,但我会怎么做呢?在前几次尝试中,我使用CInt函数将整数值转换为字符串值并将其转换回整数,但这只会产生错误。任何人都可以帮忙吗?提前致谢。你会如何在vb.net中使用trimstart进行以下操作?

 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

你想从pitchint整数中删除零吗? – Killingsworth

+3

“Pitch_txt.Text”实际包含什么?如果你使用'CInt'出错,什么错误? (“只是发现错误”不是对问题的描述,除非你说出你得到的错误是什么。)如果这是VB.NET,为什么不使用'Int32.TryParse'来进行转换从字符串到整数? –

回答

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 
+0

完美工作,它帮助很多谢谢 –

+0

@OliverHands记得给我一个'剔'我的答案,如果解决您的问题:P – Dennis

相关问题