Learning DX Step-By-Step - #6

User Input / Save / Retrieve

Wednesday, April 11, 2007 by RomanDA | Discussion: DesktopX Tutorials

Step-by-Step Tutorial

#6 - User Input / Save / Retrieve

A series by RomanDA

Listing of other DX Tutorials: Click here
Today's Lesson: "User Input / Save / Retrieve"

In this lesson we will cover how to gather information from a user via a popup dialog box, and to how to store and retrieve this data from the registry.

As in the previous tutorial, the goal here is for you to learn how to create an object, add code to it, and be able to edit that code to make changes to the way the object/widget behaves. The code is provided for you to copy/paste, but it would work best if you type in the code, to see how DX "Auto-fills" the info as you type. Its really the best way to see how DX works.

In order to use these Tutorials you will need to purchase DesktopX for $14.95 from Stardock.

Lets get started.

STEP 1 - What are we doing here?
Why do we want to get input from a user?
Well that's all depends on the gadget/widget you are creating. For a Weather widget you need to get the City code or Zip code to use. If you have a "to-do list" widget, you would want to get the item they want to add into the list. There are a lot of reasons you need to gather input from the user.

Why store the data in the Registry?
I have picked using the Registry over using the built in functions because I have had issues with them not working 100% and they only work when you run it as a widget/gadget not in "builder" mode, and we want to see how this works. A lot of people do not like to send things to the Registry but I have not had any problems with this, some people have told me they needed to "allow" the program to write to the Reg, but after that it worked fine.

You can store data in a TEXT file, an INI file, or even an XML file. Just about any place you want to store things. The problem with this is that you need to know the location of this file on the user's computer. This can cause problems, but there are a lot of widgets that use this method. I have used it as well, there are ways to cope with trying to locate the file. Not something I'm going to get into on this tutorial.

STEP 2 - Lets Get Started- Make a TEXT object.
As in the previous tutorials, I think you should know enough by now so that i can move past the every-single-step process.

Create a TEXT object.
Add the text "Click to Add Text"
Then pick the font/color/size that works best for you.
Once that is all done lets add a VERY simple script to allow you to ask for some input, and quickly show that info.

ADD the following at the bottom of the existing script items (Object_OnScriptEnter/Object_OnScriptExit)

Function Object_OnLButtonUp(x,y,dragged)
If dragged = False Then
x = InputBox("Please Enter A Piece of Text To Add to " & vbNewLine & object.text , "Add Some Text")
If x <> "" Then object.text = object.text & vbnewline & x
End If
End Function

Lets break the above apart.

Function Object_OnLButtonUp(x,y,dragged) This line creates a FUNCTION that is called when you RELEASE the "Left" Mouse Button, IE: when you click your mouse.
The X/Y/dragged are used to determine if you DRAGGED the mouse when you went to click the mouse.
If dragged = False Then we use this so that it wont activate the function every time you MOVE/DRAG the item around the screen.
Meaning, if the object is not dragged then run the function.
x = InputBox("Please Enter A Piece of Text To Add to " & vbNewLine & object.text , "Add Some Text") X is the variable we want to store the data into.
The INPUTBOX is used to prompt the user for "input".
the format is inputbox(message,title)

We are putting in a message:
"Please Enter A Piece of Text To Add to "
(existing text here)
Then we have a TITLE called "Add Some Text"
If x <> "" Then object.text = object.text & vbnewline & x If X <> "", which means if there is SOMETHING in the variable X then take the object.text and add the existing object.text and X to make a new piece of text.
the vbnewline puts a NEW LINE or linefeed after the existing text.
This will basically add the text the user inputs to the next line under the Existing Text.
End If
End Function
End the IF DRAGGED...
And end the Function.

The way this will work is very simple.
The original Text object shows:
Click to Add Text

When you click on the object you will see a dialog box like below:

Then you put in your text that you want to add "2nd Line of Text"

The next Object will now look like:

Click to Add Text
2nd Line of Text

You can keep adding more and more lines to the object.

This was simply to show you how to get "input" from the user.
The next step will be to take this data and store it somewhere.

STEP 3 - STORING User Entered Data
Using the same style entry box above we want to store a single piece of data into the registry.
To add this to the registry, we need to create a link into the Registry by using the WScript.Shell, we open a link.
Then we use the .REGWRITE command to send info to a particular part of the registry.

In this case we are going to use the "KEY" you see listed below. You could use just about anything.

The HKCU = HKEY_Current_User - you could use HKLM - for Local Machine. I prefer to use the Current User because this way each person could have their own settings on the machine.

As for the rest of the key, that is open to your desire, for now we want to use \Stardock\DesktopX\Widget\Tutorial6\SavedInfo

This can be done right in the previous code like this:

If x <> "" Then 
  object.text = object.text & vbnewline & x
  Set Sh = CreateObject("WScript.Shell")
  Sh.RegWrite "HKCU\SOFTWARE\Stardock\DesktopX\Widget\Tutorial6\SavedInfo", X
  Set Sh = Nothing
End If

This Just adds the value of X into the KEY "Saved Info".
Of course every time you run this it overwrites the info with the next piece of text you put into the INPUTBOX.

It would not look like what our text.object looks like now. So if you want it to match the widget's info with what you are storing in the registry just change the RegWrite line and replace the X with object.text

If we click on the widget, and add the "2nd Line of Text" to it, then we look in the registry we see the following in the KEY:

You can see the KEY at the bottom of the REG Editor, and see the value stored in "SavedInfo".

If you continue to add things, you will see this change, over and over.

STEP 4 - Retrieving the Data From the Registry
So, we stored the data into the Registry, and into the actual object (kind of dumb to do it in both, but it helps us see what's being stored).

Now we want to pull that data into another object. So lets make a new TEXT object, put in the text "Click to Restore" (you can save a LOT of time by "right-clicking" on the existing object and hitting "CLONE". This makes a duplicate object with all the same code already in place!)

We want to add a very simple script to this object.

Function Object_OnLButtonUp(x,y,dragged)
  If dragged = False Then
   Set Sh = CreateObject("WScript.Shell")
   On Error Resume Next
   X = Sh.RegRead("HKCU\SOFTWARE\Stardock\DesktopX\Widget\Tutorial6\SavedInfo")
   Set Sh = Nothing
   If X <> "" Then object.text = x
  End If
End Function

You will notice about 80% of this is the same. What is different is:

 On Error Resume Next
 --- This is used to keep the program working if the registry entry doesnt exist.
 X = Sh.RegRead("HKCU\SOFTWARE\Stardock\DesktopX\Widget\Tutorial6\SavedInfo")
 --- Pull the data from the KEY and store it in the variable X
 If X <> "" Then object.text = x
 --- In this case we want to make the entire Text object match what we stored
     in the registry key.  But we also want to Make sure that there was
     something in X from the registry.  Thus the "IF".

You could make a lot of modifications to this one, but it has a great base to build from.
An option would be to make a "function" or "SUB" that does the load and store.

There are a lot of usages for these items. Anytime you want to get user info, or store/retrieve data from the registry you now know what to do.

Keep in mind that you can read in ANY "KEY" from the registry, so you could easily modify this to pull in the data in the key for say... Who the "Registered Owner" of the pc is

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\RegisteredOwner

There are lots of things you could pull from the reg. and update as well. SO PLEASE PLEASE PLEASE Take care when you write to the registry.

Check back as I add new Step-By-Step Tutorials on how to make this a link to a folder, web-site, or just about anything you want!

I hope you have enjoyed this step into DX, and look forward to the next installment.

Reply #1 Wednesday, April 11, 2007 9:54 PM

Agree with you on the autofill.

When you type it in people look at the information in the tooltip or scroll through the list. Stardock has put a great deal of information at your fingertips there.
Reply #2 Thursday, May 3, 2007 7:58 AM
This is awesome. This is a great alternative to the above mentioned methods I've been using. Thank you, DA!   

Please login to comment and/or vote for this skin.

Welcome Guest! Please take the time to register with us.
There are many great features available to you once you register, including:

  • Richer content, access to many features that are disabled for guests like commenting on the forums and downloading skins.
  • Access to a great community, with a massive database of many, many areas of interest.
  • Access to contests & subscription offers like exclusive emails.
  • It's simple, and FREE!