【问题标题】:How to get "cancel" button on InputBox to quit processing instead of returning empty string?如何让 InputBox 上的“取消”按钮退出处理而不是返回空字符串?
【发布时间】:2019-06-28 16:47:39
【问题描述】:

我有一个非 VBA 程序,它从由循环激活的一系列 InputBox 中获取用户输入。我需要它,这样,如果用户单击“取消”按钮,它就会跳出循环。不幸的是,“取消”按钮没有这样做,而是返回一个空字符串,然后将其传递给我的输入验证函数,失败,并给出一个失败的验证消息。在验证消息上单击“确定”会再次弹出 InputBox。这将创建一个取消 > 确定 > 取消 > 确定的无限循环。我需要它工作,以便当用户单击“取消”时,InputBox 关闭而不发送任何输入。这可能吗?感谢您的帮助:)

这是我的代码:

'Assign value to variable
strAmountInput = InputBox("Please enter the monthly rainfall for " & strMonth)


If Validation(strAmountInput) = True Then
    'Set Value for dblAmount
    dblAmount = CDbl(strAmountInput)

    'Add monthly rainfall amount to array, using i as index
    dblMonthlyRain(i) = dblAmount

    'Add item displaying monthly rainfall to listbox
    lstMonthlyRainfall.Items.Add("Rainfall for " & strMonth & " = " & CStr(dblMonthlyRain(i)))
Else
    'Prevent loop from advancing if validation fails, forces user to enter valid input before moving on
    i -= 1

End If

_______________________________________________________________________________________

Private Function Validation(Amount As String) As Boolean

    'Validate user input for InputBox
    If IsNumeric(Amount) = False Then
        MessageBox.Show("Please enter numbers only")
        Return False
    Else
        Return True
    End If

End Function

【问题讨论】:

  • If String.IsNullOrEmpty(strAmountInput) Then Exit DoExit ForExit While。也许,摆脱相当丑陋的 InputBox 并构建一个专门用于此任务的表单。

标签: vb.net inputbox


【解决方案1】:

基于this answer,当按下取消时,输入框返回一个长度为零的字符串,因此您必须检查。

strAmountInput = InputBox("Please enter the monthly rainfall for " & strMonth)

If strAmountInput = "" Then
   'Cancel was pressed or no value was entered
Else

  If Validation(strAmountInput) = True Then
     'Set Value for dblAmount
     dblAmount = CDbl(strAmountInput)

     'Add monthly rainfall amount to array, using i as index
     dblMonthlyRain(i) = dblAmount

     'Add item displaying monthly rainfall to listbox
     lstMonthlyRainfall.Items.Add("Rainfall for " & strMonth & " = " & 
     CStr(dblMonthlyRain(i)))
  Else
     'Prevent loop from advancing if validation fails, forces user to enter valid input before moving on
      i -= 1

  End If
End If

【讨论】:

    【解决方案2】:

    我需要它工作,以便当用户单击“取消”时,输入框 关闭而不发送任何输入。这可能吗?感谢任何 帮助:)

    其他答案地址使用本机 InputBox 函数。这是自定义表单派上用场的领域。有两个步骤:

    • 创建表单本身
    • 创建处理程序例程以模拟本地 InputBox 调用

    创建一个类似于 InputBox 的用户表单:一个标题、标签、文本框和两个按钮(“确定”和“取消”)。此处的优点是您可以在文本框中动态进行数据验证(例如,防止非数字条目),您还可以禁用“确定”按钮,直到文本框中存在有效条目。这是一个很好的用户界面设计考虑 - 训练用户进行正确的输入。

    创建一个函数,它接受与 InputBox 类似的参数。在此函数中,您可以实例化用户表单,首先用所需的任何数据填充它(例如,将标签设置为问题),显示用户表单,等待用户输入,然后获取任何响应。

    您目前感兴趣的部分:如果他们取消了怎么办?

    这在 VBA 中使用 Variant 类型更容易,因为您可以返回布尔值或字符串(或在智能输入框中接近整数!)。但是 Vb.Net 没有这种奢侈。

    要实现这一点,请在您的自定义输入框调用ByRef IsCancelled as Boolean 中添加一个附加参数。当用户关闭表单时,您的自定义函数(模拟 InputBox 调用的函数)可以根据您的自定义表单设置设置取消标志。您将如何在主程序中使用它的示例:

    Dim noUserInput as Boolean
    noUserInput = False
    strAmountInput = InputBox("Please enter the monthly rainfall for " & strMonth, noUserInput)
    If Not no UserInput Then
        dblAmount = CDbl(strAmountInput)
    
        'Add monthly rainfall amount to array, using i as index
        dblMonthlyRain(i) = dblAmount
    
        'Add item displaying monthly rainfall to listbox
        lstMonthlyRainfall.Items.Add("Rainfall for " & strMonth & " = " & CStr(dblMonthlyRain(i)))
    End If
    

    显然,如果用户取消了,那么肯定是有原因的,所以让他们陷入无限循环以不断要求输入会很烦人。

    我的建议似乎比其他答案要多做一些工作。但是,这种方法是可重复使用的,并且根据经验,您可以将其调整为多种返回类型,从而为许多程序提供有用的永久实用程序。

    我拥有的唯一代码示例是在另一台机器上和在 VBA 中(请参阅我上面关于使用 Variant 类型的注释,这使得这更容易)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      相关资源
      最近更新 更多