我要当反贼 发表于 2023-4-10 17:00:02

可以不使用SetVar这个函数再换一种写法吗

chatGPT_小控 发表于 2023-4-10 17:00:15

我要当反贼 发表于 2023-04-10 17:00
可以不使用SetVar这个函数再换一种写法吗 ...

可以不使用SetVar函数,另一种写法如下:Sub OnClick(ByVal Item)    If Item.GetTagName = "Button1" Then         '弹出确认对话框,询问用户是否确认操作      Dim WshShell, intResult      Set WshShell = CreateObject("WScript.Shell")

我要当反贼 发表于 2023-4-10 17:01:24

请写出完整的程序

chatGPT_小控 发表于 2023-4-10 17:01:54

我要当反贼 发表于 2023-04-10 17:01
请写出完整的程序

完整的程序如下:

Sub OnClick(ByVal Item)
    If Item.GetTagName = "Button1" Then
      '弹出确认对话框,询问用户是否确认操作
      Dim WshShell, intResult
      Set WshShell = CreateObject("WScript.Shell")
      intResult = WshShell.Popup("是否确认此操作?", 0, "提示", vbYesNo + vbQuestion)

      '根据用户的选择更新变量值
      If intResult = vbYes Then
            If Item.GetPropertyValue("Tag1") = 0 Then
                Item.SetPropertyValue "Tag1", 1
            Else
                Item.SetPropertyValue "Tag1", 0
            End If
      End If
    End If
End Sub

请将上述代码复制到WINCC VBS脚本编辑器中,并确保按钮名称为“Button1”和要控制的变量名称为“Tag1”。

我要当反贼 发表于 2023-4-10 17:47:12

可否不在代码中体现按钮名称呢?

chatGPT_小控 发表于 2023-4-10 17:47:31

我要当反贼 发表于 2023-04-10 17:47
可否不在代码中体现按钮名称呢?

可以,您可以使用Item.GetTagName()函数获取该按钮的名称并进行比较。以下是不体现按钮名称的代码:Sub OnClick(ByVal Item)    If Item.GetTagType = 10 Then      '弹出确认对话框,询问用户是否确认操作      Dim WshShell, intResult      Set WshShell = CreateObject("WScript.Shell")      intResult = MsgBox("是否确认此操作?", vbYesNo, "确认提示")      If i

我要当反贼 发表于 2023-4-10 17:48:20

请发完整的程序

chatGPT_小控 发表于 2023-4-10 17:48:50

我要当反贼 发表于 2023-04-10 17:48
请发完整的程序

完整的代码如下:Sub OnClick(ByVal Item)    If Item.GetTagType = 10 Then      '弹出确认对话框,询问用户是否确认操作      Dim WshShell, intResult      Set WshShell = CreateObject("WScript.Shell")      intResult = MsgBox("是否确认此操作?", vbYesNo + vbQuestion, "确认操作")      If intResult = vbYes Then            Dim currentValue            currentValue = CInt(Item.Value)            If currentValue = 0 Then                Item.Value = 1            Else                Item.Value = 0            End If      End If    End IfEnd Sub

请注意,这个脚本只适用于WINCC 7.4 SP1及以上版本。另外,由于不体现按钮名称,如果您在页面中有多个按钮,需要确保只将该脚本与目标按钮相关联,否则可能会引发错误。
页: 1 [2]
查看完整版本: wincc vbs脚本1