乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 180|回复: 12

有没有更好的方法来评估文本框值

[复制链接]

16

主题

168

帖子

39

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
197
发表于 2007-5-7 15:15:06 | 显示全部楼层 |阅读模式
有没有更好的方法来简化此代码
  1.       If IsNumeric(txtLengthFt) Then
  2.             If txtLengthFt.Value Mod 1 = 0 Then
  3.                   intL = CInt(txtLengthFt) * 12
  4.             Else
  5.                   MsgBox "Enter a valid Length"
  6.                   Me.Show
  7.             End If
  8.       Else
  9.             MsgBox "Enter a valid Length"
  10.             Me.Show
  11.       End If
  12.             If IsNumeric(txtLengthIn) Then
  13.             If txtLengthIn.Value Mod 1 = 0 Then
  14.                   intL = intL + CInt(txtLengthIn)
  15.             Else
  16.                   MsgBox "Enter a valid Length"
  17.                   Me.Show
  18.             End If
  19.       Else
  20.             MsgBox "Enter a valid Length"
  21.             Me.Show
  22.       End If

我已经制定了逻辑来确定用户是否输入了数值,但我不知道这是否是最好、最有效的方法

本帖以下内容被隐藏保护;需要你回复后,才能看到!

游客,如果您要查看本帖隐藏内容请回复
回复

使用道具 举报

85

主题

404

帖子

7

银币

中流砥柱

Rank: 25

铜币
751
发表于 2007-5-7 15:28:54 | 显示全部楼层
您可以简单地将textbox限制为只允许数字,从而避免检查值是否是数字。
  1. Private Sub txtLengthFt_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  2.     Select Case KeyAscii
  3.         Case Asc("0") To Asc("9")
  4.         Case Else
  5.             KeyAscii = 0
  6.     End Select
  7. End Sub

回复

使用道具 举报

170

主题

1424

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
2119
发表于 2007-5-7 15:30:48 | 显示全部楼层
谢谢马特!
我也学到了一些东西
回复

使用道具 举报

85

主题

404

帖子

7

银币

中流砥柱

Rank: 25

铜币
751
发表于 2007-5-7 15:41:00 | 显示全部楼层
谢谢马特!
如果他们按下一个非数字,什么都不按,会怎么样?我从来没有使用过,所以我现在正在尝试,但我想我会问,看看是否有任何我应该知道的问题。
回复

使用道具 举报

170

主题

1424

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
2119
发表于 2007-5-7 15:42:01 | 显示全部楼层
太好了!我从来不知道你能做到。
谢谢马特
回复

使用道具 举报

85

主题

404

帖子

7

银币

中流砥柱

Rank: 25

铜币
751
发表于 2007-5-7 15:42:49 | 显示全部楼层
既然你已经向我展示了这一点,我将向你展示你刚刚从
  1.       If IsNumeric(txtLengthFt) Then
  2.             If txtLengthFt.Value Mod 1 = 0 Then
  3.                   intL = CInt(txtLengthFt) * 12
  4.             Else
  5.                   MsgBox "Enter a valid Length"
  6.                   Me.Show
  7.             End If
  8.       Else
  9.             MsgBox "Enter a valid Length"
  10.             Me.txtLengthFt.SetFocus
  11.             Me.txtLengthFt.SelStart = 0
  12.             Me.txtLengthFt.SelLength = Len(txtLengthFt)
  13.             Me.Show
  14.       End If
  15.       If IsNumeric(txtLengthIn) Then
  16.             If txtLengthIn.Value Mod 1 = 0 Then
  17.                   intL = intL + CInt(txtLengthIn)
  18.             Else
  19.                   MsgBox "Enter a valid Length"
  20.                   Me.Show
  21.             End If
  22.       Else
  23.             MsgBox "Enter a valid Length"
  24.             Me.txtLengthFt.SetFocus
  25.             Me.txtLengthIn.SelStart = 0
  26.             Me.txtLengthIn.SelLength = Len(txtLengthFt)
  27.             Me.Show
  28.       End If
  29.       
  30.       If IsNumeric(txtWidthFt) Then
  31.             If txtWidthFt.Value Mod 1 = 0 Then
  32.                   intW = CInt(txtWidthFt) * 12
  33.             Else
  34.                   MsgBox "Enter a valid Length"
  35.                   Me.Show
  36.             End If
  37.       Else
  38.             MsgBox "Enter a valid Length"
  39.             Me.txtWidthFt.SetFocus
  40.             Me.txtWidthFt.SelStart = 0
  41.             Me.txtWidthFt.SelLength = Len(txtWidthFt)
  42.             Me.Show
  43.       End If
  44.       If IsNumeric(txtWidthIn) Then
  45.             If txtWidthIn.Value Mod 1 = 0 Then
  46.                   intW = intW + CInt(txtWidthIn)
  47.             Else
  48.                   MsgBox "Enter a valid Length"
  49.                   Me.Show
  50.             End If
  51.       Else
  52.             MsgBox "Enter a valid Length"
  53.             Me.txtWidthFt.SetFocus
  54.             Me.txtWidthIn.SelStart = 0
  55.             Me.txtWidthIn.SelLength = Len(txtWidthFt)
  56.             Me.Show
  57.       End If
  58.       If IsNumeric(txtHeightFt) Then
  59.             If txtHeightFt.Value Mod 1 = 0 Then
  60.                   intH = CInt(txtHeightFt) * 12
  61.             Else
  62.                   MsgBox "Enter a valid Length"
  63.                   Me.Show
  64.             End If
  65.       Else
  66.             MsgBox "Enter a valid Length"
  67.             Me.txtHeightFt.SetFocus
  68.             Me.txtHeightFt.SelStart = 0
  69.             Me.txtHeightFt.SelLength = Len(txtHeightFt)
  70.             Me.Show
  71.       End If
  72.       If IsNumeric(txtHeightIn) Then
  73.             If txtHeightIn.Value Mod 1 = 0 Then
  74.                   intH = intH + CInt(txtHeightIn)
  75.             Else
  76.                   MsgBox "Enter a valid Length"
  77.                   Me.Show
  78.             End If
  79.       Else
  80.             MsgBox "Enter a valid Length"
  81.             Me.txtHeightFt.SetFocus
  82.             Me.txtHeightIn.SelStart = 0
  83.             Me.txtHeightIn.SelLength = Len(txtHeightFt)
  84.             Me.Show
  85.       End If

中拯救了我
回复

使用道具 举报

85

主题

404

帖子

7

银币

中流砥柱

Rank: 25

铜币
751
发表于 2007-5-7 15:44:35 | 显示全部楼层
好吧,既然明白了,那我来问这个问题。3个文本框用于英寸。我在考虑使用CInt()转换成integer,并进行 12 Then
        MsgBox "Whoa pardner!  Nothing greater than 12 is allowed here!", vbCritical + vbOKOnly
        ' Enter a value of '0' in the textbox
        TextBox1.Text = "0"
        ' Highlight the text in the textbox for immediate re-entry of number(s)
        TextBox1.SelStart = 0
        TextBox1.SelLength = TextBox1.TextLength
    End If
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case Asc("0") To Asc("9")
        Case Else
            KeyAscii = 0
    End Select
End Sub[/code]
回复

使用道具 举报

170

主题

1424

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
2119
发表于 2007-5-7 15:56:37 | 显示全部楼层
看看为什么函数第二次循环,它不应该只是验证数据并继续前进。不要清除你的用户已经输入的数据,因为你搞砸了一个字段而不得不重新输入是愚蠢的。
如果验证了数据,则
blah blah
else
msgbox " You lush!这是不对的!”endif
回复

使用道具 举报

85

主题

404

帖子

7

银币

中流砥柱

Rank: 25

铜币
751
发表于 2007-5-7 16:01:34 | 显示全部楼层
我重新审视我正在做的事情。首先,我将所有的验证代码从按钮点击事件中移出,从而解决了这个问题。其次,我缩小了它循环的原因。这是因为我触发了事件,并且当我返回表单时没有退出click事件,所以事件在他们更新信息问题后再次运行。
回复

使用道具 举报

170

主题

1424

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
2119
发表于 2007-5-7 16:47:12 | 显示全部楼层
好东西Matt
CmdrDuh-如果您的代码不在点击事件中,如何退出它?
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-7-7 18:47 , Processed in 1.636516 second(s), 72 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表