events - 一种条件,多种事件

标签 events vbscript conditional-statements

我正在尝试找出如何使用 .vbs 在一个条件下发生多个事件。 (这里我尝试使用 case 语句。)是否有这样的命令,或者我是否必须为每一行重写该命令? (这要在记事本上键入,方法是在前面的代码行中激活它。)

  msgbox("I was woundering how old you are.")
    age=inputbox("Type age here.",,"")
    Select Case age
    Case age>24
        x=age-24
        WshShell.SendKeys "I see you are "&x&" years older than me."
        WshShell.SendKeys "{ENTER}"
    Case age<24
        x=24-age
        WshShell.SendKeys "I see you are "&x&" years younger than me."
        WshShell.SendKeys "{ENTER}"
    Case age=24
        WshShell.SendKeys "Wow were the same age!"
        WshShell.SendKeys "{ENTER} "
    End Select

最佳答案

我认为您正在寻找 Select Case True,它是 Select Case 开关的增强使用:

age = inputbox("I was wondering how old you are.")

Select Case True
    ' first handle incorrect input
    Case (not IsNumeric(age))
        WshShell.SendKeys "You silly, you have to enter a number.{ENTER}"
    ' You can combine cases with a comma:
    Case (age<3), (age>120)
        WshShell.SendKeys "No, no. I don't think that is correct.{ENTER}"

    ' Now evaluate correct cases
    Case (age>24)
        WshShell.SendKeys "I see you are " & age - 24 & " years older than me.{ENTER}"
    Case (age<24)
        WshShell.SendKeys "I see you are " & 24 - age &" years younger than me.{ENTER}"
    Case (age=24)
        WshShell.SendKeys "Wow were the same age!{ENTER}"

    ' Alternatively you can use the Case Else to capture all rest cases
    Case Else
        ' But as all other cases handling the input this should never be reached for this snippet
        WshShell.SendKeys "Woah, how do you get here? The Universe Collapse Sequence is now initiated.{ENTER}"

End Select

我添加了一些额外的案例来向您展示这款增强型开关的强大功能。与 If a And b Then 语句相反,带有逗号的情况被短路。

关于events - 一种条件,多种事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15773668/

相关文章:

javascript - iframe 和父级之间的事件通知

C++:如何构建没有空指针的事件/消息系统?

java - 在java swing中处理事件

vba - 从多个 Excel 工作表复制数据并使用 VBScript 将其附加到单个 Excel 工作表

python - python的不等式/数值+条件语句?

node.js - 在 NodeJS 中发送 HTTP 响应之前等待事件发生?

html - 经典 ASP (VBScript) 将 HTML 代码转换为纯文本

windows - 如何在没有浏览器的情况下下载文件并打开/保存对话框批处理?

java - 如何无限地生成随机数,直到它符合Java中的条件?

SQL 选择行,前提是它们包含在给定年份的 TOP 中