【问题标题】:Different MsgBox after each iteration每次迭代后不同的 MsgBox
【发布时间】:2017-05-28 16:53:52
【问题描述】:

Ekkehard.Horner 用下面的代码帮助了我很多(我已经修改过)。但现在我正在寻找一种方法来在每次显示 MsgBox "message 1" 时添加一个计数器。

<html>
<head>
<title>Sync Tool</title>
<HTA:APPLICATION
  APPLICATIONNAME="Sync Tool"
  ID="SyncTool"
  VERSION="1.0.0"
  BORDER="dialog"
  MAXIMIZEBUTTON="no"
  MINIMIZEBUTTON="no"
  SCROLL="no"
  SINGLEINSTANCE="yes"
  CONTEXTMENU="no"
  SELECTION="no"/>
</head>

<script language="VBScript">
Set objShell = CreateObject("WScript.Shell")

Sub Window_OnLoad
    Dim width, height
    width  = 330
    height = 310
    self.ResizeTo width, height
    self.MoveTo (screen.AvailWidth-width)/2, (screen.AvailHeight-height)/2
End Sub

Sub OnClickButtonSyncNow()
    ' Box A
    If BoxAA.checked Then
        MsgBox "BoxAA"
    Else
        'Do nothing
    End If

    If BoxAB.checked Then
        MsgBox "BoxAB"
    Else
        'Do nothing
    End If

    If BoxAC.checked Then
        MsgBox "BoxAC"
    Else
        'Do nothing
    End If

    If BoxAD.checked Then
        MsgBox "BoxAD"
    Else
        'Do nothing
    End If

    If BoxAE.checked Then
        MsgBox "BoxAE"
    Else
        'Do nothing
    End If

    ' Box B
    If BoxBA.checked Then
        MsgBox "BoxBA"
    Else
        'Do nothing
    End If

    If BoxBB.checked Then
        MsgBox "BoxBB"
    Else
        'Do nothing
    End If

    If BoxBC.checked Then
        MsgBox "BoxBC"
    Else
        'Do nothing
    End If

    If BoxBD.checked Then
        MsgBox "BoxBD"
    Else
        'Do nothing
    End If

    If BoxBE.checked Then
        MsgBox "BoxBE"
    Else
        'Do nothing
    End If

    Dim bF : bF = False
    Dim c1
    For Each c1 In Split("A B C D E F G H I J")
        Dim n : n = c1
        Dim b : Set b = document.getElementById(n)
        If b.Checked Then
            bF = True
        End If
    Next
    If bF Then
        MsgBox "all done"
    Else
        MsgBox "message 1"
    End If
End Sub

Sub ExitTool()
    window.close()
End Sub
</script>

<body bgcolor="firebrick">
<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg th{font-family:Arial, sans-serif; font-size:14px; font-weight:normal; padding-top:0px ;padding-right:20px; padding-bottom:0px; padding-left:0px ;border-style:solid; border-width:0px; overflow:hidden; word-break:normal;}
.tg td{font-family:Arial, sans-serif; font-size:14px; padding-top:5px ;padding-right:10px; padding-bottom:0px; padding-left:0px ;border-style:solid; border-width:0px; overflow:hidden; word-break:normal;}
.tg .tg-header{color:#FFFB00; font-size:22px; font-weight:bold; font-family:Verdana, Geneva, sans-serif !important;}
.tg .tg-text{color:white; font-family:Verdana, Geneva, sans-serif !important; vertical-align:top}
.button {height:50px; width:136px; font-weight:bold; background-color:#555555; border: 2px solid #FFFB00; color:white; text-align:center; text-decoration:none; display:inline-block; font-size:16px;}
</style>
<table class="tg">
  <tr>
    <th class="tg-header">Box A</th>
    <th class="tg-header">Box B</th>
  </tr>
  <tr>
    <td class="tg-text"><input type="checkbox" name="BoxAA" id="A">AA</td>
    <td class="tg-text"><input type="checkbox" name="BoxBA" id="B">BA</td>
  </tr>
  <tr>
    <td class="tg-text"><input type="checkbox" name="BoxAB" id="C">AB</td>
    <td class="tg-text"><input type="checkbox" name="BoxBB" id="D">BB</td>
  </tr>
  <tr>
    <td class="tg-text"><input type="checkbox" name="BoxAC" id="E">AC</td>
    <td class="tg-text"><input type="checkbox" name="BoxBC" id="F">BC</td>
  </tr>
  <tr>
    <td class="tg-text"><input type="checkbox" name="BoxAD" id="G">AD</td>
    <td class="tg-text"><input type="checkbox" name="BoxBD" id="H">BD</td>
  </tr>
  <tr>
    <td class="tg-text"><input type="checkbox" name="BoxAE" id="I">AE</td>
    <td class="tg-text"><input type="checkbox" name="BoxBE" id="J">BE</td>
  </tr>
</table>
<br>
<input type="button" class="button" name="SyncNow" id="SyncNow" value="Sync Now" onclick="OnClickButtonSyncNow">
<input type="button" class="button" name="Exit" id="Exit" value="Exit" onclick="ExitTool">
</body>
</html>

我正在寻找的结果是这样的:

If bF Then
    MsgBox "all done"
    window.close()
Else
    MsgBox "message 1" 'counter + 1
ElseIf counter = 2 Then
    MsgBox "message 2" 'counter + 1
ElseIf counter = 3 Then
    MsgBox "message 3" 'counter + 1
ElseIf counter = 4 Then
    MsgBox "message 4" 'counter + 1
ElseIf counter = 5 Then
    MsgBox "message 5" 'no more counting needed because of window.close()
    window.close()
End If

我尝试了不同的方法,例如 Do..Loop,但我的计数器从未超过 1。 真的很难理解 VBScript 中的计数基础知识。

【问题讨论】:

  • 当单击按钮并且未选中所有复选框时,它会显示消息 1。但我想要完成的是每次单击按钮时显示一条新消息并且所有复选框仍然未选中,需要在 5 条消息后停止。我将在今晚晚些时候更新我的第一篇文章,以添加整个 HTA。
  • 刚刚在第 1 篇文章中添加了完整的 HTA

标签: vbscript count hta msgbox


【解决方案1】:

您需要一个全局变量来保存跨过程调用的计数,以及一个包含您使用计数器作为索引访问的消息的数组。把你的代码改成这样:

<script language="VBScript">
Dim ctr : ctr = 0
messages = Array( "message 1", _
    "message 2", _
    "message 3", _
    "message 4", _
    "aborting"
)
...
Sub OnClickButtonSyncNow
    ...
    If bF Then
        MsgBox "all done"
        window.Close
    Else
        MsgBox messages(ctr)
        If ctr >= 4 Then window.Close
        ctr = ctr + 1
    End If
End Sub
...
</script>

【讨论】:

  • 首先,感谢您纠正我的帖子,英语不是我的第一语言 :) 其次,我认为我没有很好地解释我想要完成的事情,或者我只是没有'看不到/理解你的答案。当用户单击按钮SyncNow 并且bFFalse 时,它会显示MsgBox,告诉用户它忘记选择一个复选框。我正在寻找的是当用户再次单击SyncNow 按钮时,它必须显示另一条消息,告诉用户他/她仍然没有选择任何复选框。在 5 条消息之后,我将关闭 HTA,因为用户显然不兼容 :)
  • 我认为我谈论计数器可能具有误导性和不正确性。
  • @WatskeBart 我做了一些小的调整,但是 AFAICS 代码应该已经完成​​了您刚才描述的操作。如果不是,请描述结果与您预期的不同。您是否希望每次迭代都有不同的消息?
  • 没错,找不到合适的词“迭代”。 bF 时的每次迭代都是Falsewindow.close 第五次迭代后。
  • 这正是我想要的,非常感谢。我知道我必须对计数器做一些事情,但从未想过要为消息设置一个数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 2018-04-12
  • 2013-02-07
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多