【问题标题】:calculating program in visual basicVisual Basic中的计算程序
【发布时间】:2017-03-05 11:26:53
【问题描述】:

我试图在 Text Changed 事件中使用 Visual Basic 编写程序,当在文本框中输入值时,程序将存储该值,当删除该值并输入新值时,它将使用第一个值并与新值进行比较,如果从第一个值开始的新值 > 从第一个值开始减少新值(新值-第一个值),如果新值

Dim f As String
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    If TextBox2.Text > f Then
        TextBox18.Text = TextBox2.Text - f
        f = (TextBox2.Text)
    End If

我让这段代码可以工作,但是当新值是 (10) 或更大时它不起作用

【问题讨论】:

  • 使用Option Strict On,将String 转换为Integer(进行数字比较而不是字典)并且不要使用TextChanged 为此,每次击键时都会与 "uncompleted yet" 值进行比较;更喜欢Validated/Validating 或LostFocus 事件

标签: vb.net


【解决方案1】:

尝试以下方法:

Dim f As String
Private Sub TextBox2_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Leave
If not string.isnullOrEmpty(TextBox2.Text) Then
If CDbl(TextBox2.Text) > CDbl(f) Then
    TextBox18.Text = CStr(CDbl(TextBox2.Text) - CDbl(f))
    f = TextBox2.Text
End If
End If
End Sub

【讨论】:

  • 如果输入的值不是整数怎么办? (例如,如果用户输入 12.34 怎么办?)
  • 使用 CDec() 或 CDbl()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多