【问题标题】:How to have radio buttons using vb script?如何使用 vb 脚本设置单选按钮?
【发布时间】:2016-10-10 20:38:29
【问题描述】:

我想在我的电脑上设置时间。为此,我正在编写三个不同国家/地区的 3 个单选按钮,以便用户可以通过选择其中一个单选按钮来设置时区。

还请帮助解决以下错误:

Set objExecObject = objShell.Exec("tzutil.exe /s """ & TimeZoneFriendlyName & "") 
WScript.Echo "Setting the time zone sucessfully." 

Error message

【问题讨论】:

  • VB.NET IsNot vba AndAlso IsNot vbscript
  • 欢迎来到 StackOverflow。我们很想帮助您,但是 1) 听起来您有两个问题(另外)您的第一个问题是什么? 2)如果您确实有两个问题,请分开发布。 3) 不要将错误发布为图片;发布错误的文本。您可以编辑您的问题(通过单击问题下方的“编辑”链接)并希望得到更好的答案。
  • objShell 需要包含一个WshShell 对象,使用Set objShell = WScript.CreateObject("WScript.Shell")

标签: vbscript automation


【解决方案1】:
<p><b>Search <u>w</u>here</b></p>
<p><INPUT Name=tb2 TYPE=RADIO CHECKED AccessKey=w> Google <INPUT Name=tb2 TYPE=RADIO> Wikipedia <INPUT Name=tb2 TYPE=RADIO> Nethack <INPUT Name=tb2 TYPE=RADIO> MSDN <INPUT Name=tb2 TYPE=RADIO> Australia <INPUT Name=tb2 TYPE=RADIO> Maps </p>

Sub Search
'   On Error Resume Next
    Set WshShell = CreateObject("WScript.Shell")
    If tb2(5).Checked=True then WshShell.Run "https://www.google.com/maps/place/" & Replace(tb1.value, " ", "+")
    If tb2(4).Checked=True then WshShell.Run "https://www.google.com/search?q=" & Replace(tb1.value, " ", "+") & "&cr=countryAU"
    If tb2(3).Checked=True then WshShell.Run "https://www.google.com/search?q=site:msdn.com+" & Replace(tb1.value, " ", "+")
    If tb2(2).Checked=True then WshShell.Run "https://www.google.com/search?q=nethack+" & Replace(tb1.value, " ", "+")
    If tb2(1).Checked=True then WshShell.Run "https://en.wikipedia.org/wiki/" & tb1.value
    If tb2(0).Checked=True then WshShell.Run "https://www.google.com/search?q=" & Replace(tb1.value, " ", "+")
    WshShell.RegWrite "HKCU\Software\StackOverflow\VBS\Searchterm", tb1.value & vbtab & WshShell.RegRead("HKCU\Software\StackOverflow\VBS\Searchterm")
    If tb3.checked=true then window.close
End Sub

您的报价也有误。 Cmd /k 将允许您查看 CMD 中发生的情况,一旦工作就将其删除。

 WScript.CreateObject("WScript.Shell").Run "cmd /k tzutil.exe /s """ & TimeZoneFriendlyName & """"

这是一个显示单选按钮、复选框、按钮的 HTA,并展示了如何实现键盘访问。

<HTML> 
<HEAD><TITLE>Search</TITLE>
  <HTA:APPLICATION ID="oMyApp" 
    APPLICATIONNAME="Search" 
    BORDER="normal"
    BORDERSTYLE="sunken"
    CONTEXTMENU="yes"
    CAPTION="yes"
    ICON="C:\windows\system32\shell32,-5"
    SCROLL="no"
    SHOWINTASKBAR="yes"
    SINGLEINSTANCE="yes"
    SYSMENU="yes"
    WINDOWSTATE="normal"
  />

<style>
BODY  {font-size :100%;font-family: Arial, Helvetica, sans-serif;color: black;
  background:URL(images/watermark.gif);background-color: white;
  margin-top:20pt; margin-left:40pt; margin-right:10pt ; text-align:Justify}
P  {margin-left:0pt;margin-right:0pt}
</style>



<SCRIPT LANGUAGE="VBScript">
Dim Searchterm

Sub Search
'   On Error Resume Next
    Set WshShell = CreateObject("WScript.Shell")
    If tb2(5).Checked=True then WshShell.Run "https://www.google.com/maps/place/" & Replace(tb1.value, " ", "+")
    If tb2(4).Checked=True then WshShell.Run "https://www.google.com/search?q=" & Replace(tb1.value, " ", "+") & "&cr=countryAU"
    If tb2(3).Checked=True then WshShell.Run "https://www.google.com/search?q=site:msdn.com+" & Replace(tb1.value, " ", "+")
    If tb2(2).Checked=True then WshShell.Run "https://www.google.com/search?q=nethack+" & Replace(tb1.value, " ", "+")
    If tb2(1).Checked=True then WshShell.Run "https://en.wikipedia.org/wiki/" & tb1.value
    If tb2(0).Checked=True then WshShell.Run "https://www.google.com/search?q=" & Replace(tb1.value, " ", "+")
    WshShell.RegWrite "HKCU\Software\StackOverflow\VBS\Searchterm", tb1.value & vbtab & WshShell.RegRead("HKCU\Software\StackOverflow\VBS\Searchterm")
    If tb3.checked=true then window.close
End Sub

Sub Init
    Count = 0
    window.resizeTo 550, 400
'   On Error Resume Next
    Set WshShell = CreateObject("WScript.Shell")
    HistoryArr = Split(WshShell.RegRead("HKCU\Software\StackOverflow\VBS\Searchterm"), Chr(9))
    tb1.value = HistoryArr(0)
    For each t in HistoryArr
        Count=Count + 1
        Set Hist = document.createElement("OPTION")
        Hist.Text=t
        Hist.Value=Count
        HistoryList.Add(Hist)
        Count=Count + 1
        If Count > 50 then exit For
    next
    tb1.select
'   tb1.focus
End Sub

Sub Paste
    tb1.value=document.parentwindow.clipboardData.GetData("TEXT")
    tb1.select
End Sub

Sub LBTransfer
    x = CLng(HistoryList.Options.selectedindex)
    tb1.value = HistoryList.Options(x).text
    tb1.select
End Sub

Sub LBTransferK
    x = CLng(HistoryList.Options.selectedindex)
    tb1.value = HistoryList.Options(x).text
    HistoryList.focus
End Sub
</script>
</head>

<body Onload=Init>
<p><b>Enter <u>s</u>earch term</b></p>
<p><INPUT Name=tb1 TYPE=Text size=60 AccessKey=s> <INPUT Type=Button onclick=paste Value=Paste AccessKey=p></p> 
<p><b><u>H</u>istory</b></p>
<p><SELECT NAME="HistoryList" SIZE="4" Onclick=LBTransfer OnKeyPress=LBTransferK AccessKey=h>
</SELECT></p>
<p><b>Search <u>w</u>here</b></p>
<p><INPUT Name=tb2 TYPE=RADIO CHECKED AccessKey=w> Google <INPUT Name=tb2 TYPE=RADIO> Wikipedia <INPUT Name=tb2 TYPE=RADIO> Nethack <INPUT Name=tb2 TYPE=RADIO> MSDN <INPUT Name=tb2 TYPE=RADIO> Australia <INPUT Name=tb2 TYPE=RADIO> Maps </p>
<p><INPUT NAME="Search" TYPE="SUBMIT" VALUE="Search" OnClick=Search> <INPUT Name=tb3 TYPE=CHECKBOX CHECKED AccessKey=c> <u>C</u>lose on search</p>


</body>
</html>

【讨论】:

    【解决方案2】:

    您不能使用 VBscript 创建单选按钮。您将需要使用 HTA 或其他包装器。

    错误似乎是你没有设置shell对象的问题

    所以要使用

    Set objExecObject = objShell.Exec("tzutil.exe /s """ & TimeZoneFriendlyName & "") 
    WScript.Echo "Setting the time zone sucessfully." 
    

    你首先要有一个

    Set objShell = CreateObject("Wscript.Shell")
    

    在那之前

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多