Random Numbers

Team Fly 

Page 49

        Dim strValue As String


        objOpenedKey = objMainKey.OpenSubKey _
(''Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" )

        objGotValue = objOpenedKey.GetValue( "User Agent" 

        If (Not objGotValue Is Nothing) Then 
            strValue = objGotValue.ToString() 
        Else 
            strValue = " "
        End If

        objMainKey.Close()

        TextBox1.Text = strValue

    End Sub

You must also add Imports Microsoft.Win32 up there at the top of the code window where all those other Imports are. The Microsoft.Win32 namespace contains the Registry-access functions, such as the OpenSubKey method that you need in this example.

Press F5 to run this example, and click the button. If your Registry contains the same value for this key as my Registry contains, you should see a result similar to this:

 Mozilla/4.2 (compatible; MSIE 5.0; Win32) 

Note that the complete name (path) of the entire Registry entry is divided into three different locations in the example code (they are in boldface): first the primary key, CurrentUser, then the path of subkeys, and finally the actual specific "name": objOpenedKey.GetValue("User Agent").

Writing to the Registry

The RegistryKey class includes a group of methods you can use to manage and write to the Registry. These methods include Close, CreateSubKey, DeleteSubKey, DeleteSubKeyTree, DeleteValue, Get-SubKeyNames, GetType, GetValue, GetValueNames, OpenSubKey, and SetValue.

Random Numbers

In VB6 and previous versions, you would generate a random number between 1 and 12 like this:

 X = Int(Rnd * 12 + 1) 

Or to get a random number between 0 and 12, you would use this code:

 X = Int(Rnd * 13) 

You used the Rnd and Randomize functions.

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net