none
Drop Down selection box in HTA. RRS feed

  • Question

  • Below is a simple way to create a drop down option. But in this methow you create the "option value" in the <Body>. Is there a way to do this inside the <VBScript> boundries? for instance, I can create a button withing the <Script> by use of the following line:

    Document.Body.InnerHTML = "<input Type=""Button"" Value=""My Button"" onClick=""MySub"">"

    I am sorry but I dont know how to describe this any better. Please feel free to ask any question you need. Thank you.

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    <SCRIPT LANGUAGE="VBScript">
    Sub TestSub
     For Each objOption in OptionChooser.Options
      If objOption.Selected Then
       Msgbox objOption.InnerText
      End If
     Next
    End Sub

    </SCRIPT>
    <Body>
    <select size="1" name="OptionChooser" onChange="TestSub">
     <option value="0"></option>
     <option value="1">Option 1</option>
     <option value="2">Option 2</option>
     <option value="3">Option 3</option>
     </select>
    </Body>


    Thank you Lee
    Wednesday, August 31, 2011 12:22 PM

Answers

  • That approach can be made to work, but the following approach is how I would do it ...

    <html>
    <head>
    <SCRIPT LANGUAGE="VBScript">
    
    sub AddSelect
    
     set oSelect = document.createElement("Select")
     with oSelect
      .size = "1" 
      .ID = "OptionChooser" 
      .onChange = GetRef("TestSub")
     end with
     document.body.appendChild oSelect
    
    For i = 0 to 3
     set Opt = document.createElement("Option")
     Opt.value = i
     if i> 0 then Opt.text = "Option " & i
     OptionChooser.Add Opt
    next
    
    end sub
    
    Sub TestSub
      set objOption = window.event.srcElement
      nOptIdx = objOption.selectedIndex
      Msgbox objOption(nOptIdx).Text
    End Sub
    
    </SCRIPT>
    </head>
    <Body>
    <input type=button onclick=AddSelect value="Add Select">
    </Body>
    </html>
    
    
    

     


    Tom Lavedas
    • Marked as answer by LeePiron Wednesday, August 31, 2011 6:59 PM
    Wednesday, August 31, 2011 3:13 PM
    Moderator

All replies

  • Sure, it's simple using DHTML code, something like this ...

    <html>
    <head>
    <SCRIPT LANGUAGE="VBScript">
    
    sub Window_onload
    
    For i = 1 to 3
     set Opt = document.createElement("Option")
     Opt.value = i
     Opt.text = "Option " & i
     OptionChooser.Add Opt
    next
    
    end sub
    
    Sub TestSub
     For Each objOption in OptionChooser.Options
     If objOption.Selected Then
      Msgbox objOption.InnerText
     End If
     Next
    End Sub
    
    </SCRIPT>
    </head>
    <Body>
    <select size="1" name="OptionChooser" onChange="TestSub">
     <option value="0"></option>
     </select>
    </Body>
    </html>
    

    MS has extensive reference material, of which the SELECT object is documented here.  Admittedly, the DHTML example is in JScript, but it illustrates the point and is fairly easily translated to VBScript.


    Tom Lavedas
    Wednesday, August 31, 2011 1:23 PM
    Moderator
  • As an aside, here is another way to determine which option was selected without resorting to a For/Next/If construct ...

    Sub TestSub
       set objOption = window.event.srcElement
       nOptIdx = objOption.selectedIndex
       Msgbox objOption(nOptIdx).Text
    End Sub


    Tom Lavedas
    Wednesday, August 31, 2011 1:30 PM
    Moderator
  • Thank you Tom, these answers are great, and following your examples I have learnt a little more about document.createElement("Option") and then assigning .text to that "Option".

    However, 1 last thing if you don't mind... is it possible to create the dropdown box within the <script> rather than in the <Body>. Because if I use the above example in my HTA then I will always have a dropdown box. I would like to create the dropdown box in a later sub.

    So, instead of this:

    <Body>
    <select size="1" name="OptionChooser" onChange="TestSub">
    <option value="0"></option>
    </select>
    </Body>

    Can I have something like this:

    Document.Body.InnerHTML = "<select size=""1"" name=""OptionChooser"" onChange=""TestSub""></select>"

    Thanks for taking the time to look at this.


    Thank you Lee
    Wednesday, August 31, 2011 1:42 PM
  • That approach can be made to work, but the following approach is how I would do it ...

    <html>
    <head>
    <SCRIPT LANGUAGE="VBScript">
    
    sub AddSelect
    
     set oSelect = document.createElement("Select")
     with oSelect
      .size = "1" 
      .ID = "OptionChooser" 
      .onChange = GetRef("TestSub")
     end with
     document.body.appendChild oSelect
    
    For i = 0 to 3
     set Opt = document.createElement("Option")
     Opt.value = i
     if i> 0 then Opt.text = "Option " & i
     OptionChooser.Add Opt
    next
    
    end sub
    
    Sub TestSub
      set objOption = window.event.srcElement
      nOptIdx = objOption.selectedIndex
      Msgbox objOption(nOptIdx).Text
    End Sub
    
    </SCRIPT>
    </head>
    <Body>
    <input type=button onclick=AddSelect value="Add Select">
    </Body>
    </html>
    
    
    

     


    Tom Lavedas
    • Marked as answer by LeePiron Wednesday, August 31, 2011 6:59 PM
    Wednesday, August 31, 2011 3:13 PM
    Moderator
  • Thank you Tom, this is EXACTLY what I want!!!!

    I have modified it slightly to show you where I am going... If you save just what I have below in a HTA file then this is all I need. The Sub Window_onLoad part calls the AddSelect. Now I can continue with my script and include drop down buttons.

    Keep up the good work Tom, we all really appreciate it. Have a good one.

     

    <SCRIPT LANGUAGE="VBScript">

    Sub Window_onLoad
     AddSelect
    End Sub

    sub AddSelect
     set oSelect = document.createElement("Select")
     with oSelect
      .size = "1"
      .ID = "OptionChooser"
      .onChange = GetRef("TestSub")
     end with
     document.body.appendChild oSelect

     For i = 0 to 3
      set Opt = document.createElement("Option")
       Opt.value = i
       if i> 0 then Opt.text = "Option " & i
        OptionChooser.Add Opt
     next
    end sub

    Sub TestSub
     set objOption = window.event.srcElement
      nOptIdx = objOption.selectedIndex
      Msgbox objOption(nOptIdx).Text
    End Sub

    </SCRIPT>


    Thank you Lee
    Wednesday, August 31, 2011 7:03 PM
  • Hi, I know this is an old thread, but hopefully someone will read it. I am trying to use a HTA with a dropdown option utilizing the code above or maybe one provided. In my case, I want to use it to install language packs based on a selection, but also want whatever language that is selected to show in the dialog box. Right now the selected option shows as "Option", I want the message to show as "Russian" or whatever language the users select. That said, how can I accomplish the following:

    1. Have the button initiate or launch the installer that installs the language based on selection
    2. Populate the dropdown with the friendly name of the language rather than just the word "Option"
    <SCRIPT LANGUAGE="VBScript">
    
    Sub Window_onLoad
     AddSelect
    End Sub
    
    sub AddSelect
     set oSelect = document.createElement("Select")
     with oSelect
      .size = "1" 
      .ID = "OptionChooser" 
      .onChange = GetRef("TestSub")
     end with
     document.body.appendChild oSelect
    
     For i = 0 to 3
      set Opt = document.createElement("Option")
       Opt.value = i
       if i> 0 then Opt.text = "Option " & i
        OptionChooser.Add Opt
     next
    end sub
    
    Sub TestSub
     set objOption = window.event.srcElement
      nOptIdx = objOption.selectedIndex
      Msgbox objOption(nOptIdx).Text
    End Sub
    
    </SCRIPT>
    Monday, August 12, 2019 6:24 PM