Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTA Help - Dynamically create text box? 1

Status
Not open for further replies.

pce0

Technical User
Jan 27, 2004
16
0
0
Can anyone tell me if it is possible to dynamically create text box's using HTA?

I have this piece of vbscript that writes out the subkeys and their values from a specified part of the registry:

const HKLM=&H80000002
set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

strKeyPath="SOFTWARE\Corporate\Test"

objReg.enumvalues HKLM,strKeyPath,arrsubkeys
for each subkey in arrsubkeys
objreg.GetStringValue HKLM,strKeyPath,subkey,strValue
wscript.echo subkey&": "&strvalue
next


What I want to do now is embed this in an HTA app that will display each subkey value in a text box that the user can edit - ideally I'd like the HTA to dynamically create a text box for each reg subkey - Any thoughts?

I've managed to get an HTA to populate a List Box with each subkey value, but this is of little use as the ultimate goal is to enable the user to edit the key value via the HTA!

Thanks in advance
 
You can use HTML.
[tt]<HTML>
...
<Body>
<Script Language=VBScript>
...
a=objreg.GetStringValue HKLM,strKeyPath,subkey,strValue
Document.Write "<INPUT ID=1 Type=Textbox VALUE=""" & a & "">"
...
</Script>
</Body>
</HTML>[/tt]
 
Thanks for the response, still struggling with this though...

The 1st line of the code below obtains a reg string value and puts it in the var strValue, I want the second line to create a textbox populated with this value, however all I'm getting is a textbox populated with the string '& strvalue &'...

objreg.GetStringValue HKLM,strKeyPath,subkey,strValue
Document.Write "<INPUT ID=1 Type=Textbox VALUE="" & strvalue & "">"


So I guess my question is -
What is the correct syntax for passing a variable into the VALUE paramater for a textbox????
 
To do thing proper, you use createElement() and appendChild() and alike dom method. Document.write has too much limitation, to my opinion, is not the way to go.
 
Do you have example usage of createElement() and appendChild() ? - apologies but I have littl experience with HTLM/HTA !

Thanks
 
[1] First and foremost, your use enumvalues is not exactly right. enumvalues will not result array of subkeys as the variable name suggests. It is the enumkey method.

[2] Factoring that, suppose to simplify the case, you want to enumerate top-level subkeys and put each of them in a textbox with the full form ingredients (name, action) and with a button to do some actions when editing done. Normally, a form would script an action and some submit button. But, here in a hta, I just put up a button to do some action after editing.

[3] I do extreme simplification: no user friendly text to explain what is in there each textbox...etc. Also the action button is just a msgbox.

[4] I cannot explain all the facets of the form elements. It is standard stuff. To explain in full would take me too long to spell thing clearly!
[tt]
<html>
<head>
<script language="vbscript">
sub doit
dim objReg, strKeyPath, arrsubkeys, subkey
dim oform, otxtbx, obr, osubmit
const HKLM=&H80000002
set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

strKeyPath="SOFTWARE\Corporate\Test"
objReg.enum[red]key[/red] HKLM,strKeyPath,arrsubkeys
if ubound(arrsubkeys)<>-1 then
set oform=document.createElement("form")
with oform
.name="formname"
'.action="abc.asp" 'not for this application
end with
for each subkey in arrsubkeys
set otxtbx=document.createElement("input")
with otxtbx
.type="text"
.value=subkey
end with
oform.appendChild otxtbx
set otxtbx=nothing

set obr=document.createElement("br")
oform.appendChild obr
set obr=nothing
next
end if

set osubmit=document.createElement("input")
with osubmit
'.type="submit"
.type="button"
.value="do thing"
end with
osubmit.attachEvent "onclick",getref("dothing")
oform.appendChild osubmit
set osubmit=nothing

set obr=document.createElement("br")
oform.appendChild obr
set obr=nothing

document.getElementsByTagName("body")(0).appendChild oform
set oform=nothing

end sub

sub dothing
msgbox "I am ready to do thing which can damage your computer."
end sub
</script>
</head>
<body>
<button onclick="doit">doit</button><br />
</body>
</html>
[/tt]
 
Amendment
The only thing I would like to amend is the test of ubound() when something goes wrong either [a] the key is not found or that there is no subkey, the resulting arrsubkeys is not it is array of ubound=-1. It results in fact arrsubkeys being null. Hence the testing should reflect this properly.
[tt]
'etc etc
[red]'[/red]if ubound(arrsubkeys)<>-1 then
[blue]if not isnull(arrsubkeys) then[/blue]
'etc etc
end if
'etc etc
[/tt]
 
Many thanks for the script - this has been most usefull!

It was the enumvalues method I wanted though as its the values of subkeys that I want the user to be able to change (apologies for any confusion).

I now have the following script that obtains the values of each subkey and puts them each in a textbox, what I now need to do is use the var subkey to label each textbox with the name of of subkey to which it represents. I then need a function that will update all key values with those in the textboxs when the 'Update Registry Settings' button is pressed.

Getting rather frustrated as I have this working with native VBS, just trying to get the UI bit sorted with HTA - which, as you may have guessed is new to me !

<html>
<head>
<script language="vbscript">
sub Window_onLoad
const HKLM=&H80000002

set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

strKeyPath="SOFTWARE\Corporate\Test"
objReg.enumvalues HKLM,strKeyPath,arrsubkeys

set oform=document.createElement("form")
with oform
.name="formname"
end with
for each subkey in arrsubkeys
objreg.GetStringValue HKLM,strKeyPath,subkey,strValue
set otxtbx=document.createElement("input")
with otxtbx
.type="text"
.name=subkey
.value=strvalue
end with
oform.appendChild otxtbx
set otxtbx=nothing
set obr=document.createElement("br")
oform.appendChild obr
set obr=nothing
next

set obr=document.createElement("br")
oform.appendChild obr
set obr=nothing

document.getElementsByTagName("body")(0).appendChild oform
set oform=nothing
end sub

sub UpdateReg
const HKLM=&H80000002
set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
strKeyPath="SOFTWARE\Corporate\Test"
objReg.enumvalues HKLM,strKeyPath,arrsubkeys
for each subkey in arrsubkeys
' strNewValue= need to obtain contents of textbox.subkey
objReg.setStringValue HKLM,strKeyPath,subkey,strNewValue
next
end sub
</script>
</head>
<body>
<button onclick="UpdateReg">Update Registry Settings</button><br />
</body>
</html>
 
[5] Okay if you want named values under a key, you have to be careful the method signature.
>objReg.enumvalues HKLM,strKeyPath,arrsubkeys

You can it subkeys: you are free to do. But the return include an array storing the data type of the named value with name (arrsubkeys's entry).

[tt]objReg.enumvalues HKLM,strKeyPath,arrsubkeys, [red]arrtypes[/red][/tt]

You can use the type info to issue the corresponding method (getstringvalue is only for type 1: REG_SZ. You assume all values are REG_SZ without checking: that makes the script's general validity problematic - granted you know the structure of that particular key. The structure thereafter generated may not be apt for any data other than REG_SZ - you just have to borne it in mind.

[6] Suppose you have verified it is proper to use getstringvalue for certain "subkey" (the name of the named value), you can expand that part like this.
[tt]
objreg.GetStringValue HKLM,strKeyPath,subkey,strValue
[blue]'insert this block immediately after
set otext=document.createTextNode(subkey & " : ")
oform.appendChild otext
set otext=nothing[/blue]
set otxtbx=document.createElement("input")
with otxtbx
.type="text"
.name=subkey
.value=strvalue
end with
oform.appendChild otxtbx
set otxtbx=nothing
[/tt]
 
Your absolutley right, it's only because I know that the regkey I'm querying will only ever contain string values that I'm taking this (lazy) approach!

Many thanks for your continued help - I'm learning a lot !

Maybe your could advise how I now go about obtaining the updated values from the textboxs back to script variables so I can use setStringValue to update the registry values with user input from the textbox's - ideally I want a single 'update' button that will accommodate this ??

P.s - appreciate you will have written the script for me by the time i'm finished ! ;-)
 
Not to worry - sussed it !

The code below allows me to obtain the updated textvalues and apply them back to the corresponding reg string!

set dvalue = document.getElementsByTagName("INPUT")
for each ivalue in dvalue
objReg.setStringValue HKLM,strKeyPath,ivalue.name,ivalue.value
next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top