Answered Need Help with Modify Registry Key

vmars316

Active member
Joined
Aug 24, 2020
Messages
39
Programming Experience
Beginner
Hello & Thanks ;
Win 10 , VS vb.net .vb
I am trying to to Modify
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\TabbedBrowsing
value :ShortcutBehavior
from 2 to 1 .
Which will disable "Open Tab in New window" .
I am writing a KidSafeBrowser and I want to restrict opening a 'New Tab' .
Without Disabling this a new instance of IE 11 is opened , and kiddos can go anywhere .
Program checks every download page URL against a list of Safe-Sites .
Working with the Registry is new to me , so I am a bit 'gun shy' .
And I want to get it right the 1st time .
Anyways , here is my code below .
I think I am close but I need help to properly code this .
Thanks for your Help...
VB.NET:
 Dim rKey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Internet Explorer\TabbedBrowsing\ShortcutBehavior", True)
       Key.SetValue("ShortcutBehavior", 1, RegistryValueKind.DWord)
       key.Close()
 
vmars316,

Should that be
VB.NET:
rKey.SetValue("ShortcutBehavior", 1, RegistryValueKind.DWord)
rKey.Close()

I can't see from your snippet where/how "key" is declared.
:)
 
Thanks benshaws ;
Guess I had better GetValue 1st .
I find it impossible to understand/translate docs.microsoft.com Docs .
Anyways in my code , I am getting Errors on RegistryValueKind.DWord and MsgBox .
I wish there is a book on "How to understand Microsoft's Documentation" .

VB.NET:
    Private Sub GetReg_Click(sender As Object, e As EventArgs) Handles GetReg.Click
        Dim rKey = My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Internet Explorer\TabbedBrowsing\ShortcutBehavior", True)
        rKey.GetValue("ShortcutBehavior", 1, RegistryValueKind.DWord)
        MsgBox("The value is " & ToString(Double rkey)) ;
        rKey.Close()
    End Sub

Thanks for your Help...
 
Firstly, you need to distinguish between the key and the value. If you consider the Windows Registry Editor analogous to the File Explorer, the keys are the folders and the values are the files. the key you want is 'HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\TabbedBrowsing' and the value is 'ShortcutBehavior'.

Secondly, you need to provide arguments in a manner that the method you're calling expects. This is meaningless:
VB.NET:
rKey.GetValue("ShortcutBehavior", 1, RegistryValueKind.DWord)
Thirdly, if you call a method that returns a value then you generally need to use that value, either directly or by assigning it to a variable and using that later. Where exactly do you expect the value you get to be?

Finally, you have to actually write VB code. This is meaningless:
VB.NET:
MsgBox("The value is " & ToString(Double rkey)) ;
It appears that the value you're interested in doesn't exist by default. If you want to test whether it exists on any particular machine then you get the value and see whether it is Nothing. I'd be tempted to start by defining an enumeration to represent these values, rather than using magic numbers:
VB.NET:
Public Enum ShortcutBehavior
    NewWindow = 0
    CurrentWindowNewTab = 1
    CurrentTab = 2
End Enum
You don't have to do that but it will make your code self-documenting and thus easier to read and less error-prone. You can then get the value like this:
VB.NET:
Private Const keyPath = "SOFTWARE\Microsoft\Internet Explorer\TabbedBrowsing"
Private Const valueName = "ShortcutBehavior"

Private Sub GetValue()
    Using key = My.Computer.Registry.CurrentUser.OpenSubKey(keyPath)
        Dim value = key.GetValue(valueName)

        If value Is Nothing Then
            MessageBox.Show("The value is not set")
        Else
            Dim behavior = DirectCast(value, ShortcutBehavior)

            MessageBox.Show($"The value is {behavior} ({CInt(behavior)})")
        End If
    End Using
End Sub
If you want to set the value, whether it already exists or not, you can do it like this:
VB.NET:
Private Const keyPath = "SOFTWARE\Microsoft\Internet Explorer\TabbedBrowsing"
Private Const valueName = "ShortcutBehavior"

Private Sub SetValue()
    Using key = My.Computer.Registry.CurrentUser.OpenSubKey(keyPath, True)
        key.SetValue(valueName, CInt(ShortcutBehavior.CurrentWindowNewTab), RegistryValueKind.DWord)
    End Using
End Sub
If I then run this code on my own machine:
VB.NET:
GetValue()
SetValue()
GetValue()
I see the following messages:
The value is not set
and:
The value is CurrentWindowNewTab (1)
If I go into the Registry Editor, manually change the value from 1 to 2 and run that code again I see the following messages:
The value is CurrentTab (2)
and:
The value is CurrentWindowNewTab (1)
Now, you originally said this:
I am trying to to Modify
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\TabbedBrowsing
value :ShortcutBehavior
from 2 to 1 .
Which will disable "Open Tab in New window" .
I am writing a KidSafeBrowser and I want to restrict opening a 'New Tab' .
That doesn't seem to match up with what I have read. According to what I have read, only a value of 0 (or no value at all) will allow a new window to be opened, while a value of 1 will allow a new tab to be opened. A value of 2 will not allow a new window or a new tab, so it seems like that is what you would want. Maybe the information I read is incorrect but you should probably double-check that. The Enum code that I posted above is based on my understanding of what the values mean and I used CurrentWindowNewTab because you said that you want to set the value to 1. If I'm correct though, you actually want to set the value to 2 so you should use ShortcutBehavior.CurrentTab to set the value.
 
Thank you jmcilhinney ;
Thanks for that primer , Exactly what I needed .
I will study this a while ,
And get back to you .
Thanks
 
Things are looking up :)
From jmcilhinney's code I made this so I could monitor what's going on :
VB.NET:
    Public Enum ShortcutBehavior
        NewWindow = 0
        CurrentWindowNewTab = 1
        CurrentTab = 2
    End Enum
    Private Const keyPath = "SOFTWARE\Microsoft\Internet Explorer\TabbedBrowsing"
    Private Const valueName = "ShortcutBehavior"
    Private Sub GetReg_Click(sender As Object, e As EventArgs) Handles GetReg.Click
        GetValue()
    End Sub
    Private Sub GetValue()
        Using key = My.Computer.Registry.CurrentUser.OpenSubKey(keyPath)
            Dim value = key.GetValue(valueName)
            If value Is Nothing Then
                MessageBox.Show("The value is not set")
            Else
                Dim behavior = DirectCast(value, ShortcutBehavior)

                MessageBox.Show($"The value is {behavior} ({CInt(behavior)})")
            End If
        End Using
    End Sub

    Private Sub SetReg2_Click(sender As Object, e As EventArgs) Handles SetReg2.Click
        SetValue2()
    End Sub
    Private Sub SetValue2()
        Using key = My.Computer.Registry.CurrentUser.OpenSubKey(keyPath, True)
            '            key.SetValue(valueName, CInt(ShortcutBehavior.CurrentWindowNewTab), RegistryValueKind.DWord)
            key.SetValue(valueName, CInt(ShortcutBehavior.CurrentTab), RegistryValueKind.DWord)
        End Using
    End Sub
    Private Sub SetReg1_Click(sender As Object, e As EventArgs) Handles SetReg1.Click
        SetValue1()
    End Sub
    Private Sub SetValue1()
        Using key = My.Computer.Registry.CurrentUser.OpenSubKey(keyPath, True)
            key.SetValue(valueName, CInt(ShortcutBehavior.CurrentWindowNewTab), RegistryValueKind.DWord)
        End Using
    End Sub

    Private Sub SetReg0_Click(sender As Object, e As EventArgs) Handles SetReg0.Click
        SetValue0()
    End Sub
    Private Sub SetValue0()
        Using key = My.Computer.Registry.CurrentUser.OpenSubKey(keyPath, True)
            key.SetValue(valueName, CInt(ShortcutBehavior.NewWindow), RegistryValueKind.DWord)
        End Using
    End Sub
And it turns out both values 1 and 2 stop a new widow from being opened .
And 0 of course causes a new window to be opened .

Btw: jmcilhinney ,
I would like to see your code Posted somewhere like stackoverflow and codeproject , it could help a lot of people .

Thanks
 
Your code for setting is a bit verbose. You don't need SetValue0, SetValue1, and SetValue2. You just need one SetValue method:
VB.NET:
Private Sub SetValue(value As ShortcutBehavior)
    Using key = My.Computer.Registry.CurrentUser.OpenSubKey(keyPath, True)
        key.SetValue(valueName, CInt(value), RegistryValueKind.DWord)
    End Using
End Sub
and then call it like this:
VB.NET:
Private Sub SetReg0_Click(sender As Object, e As EventArgs) Handles SetReg0.Click
    SetValue(ShortcutBehavior.NewWindow)
End Sub
Like I said, self-documenting.
 
Finishing touches:
When SetReg = 0 , Tap options look like this :
WhenSetReg-0.png


When SetReg = 1 , Tap options look like this :
WhenSetReg-1.png


When SetReg = 2 , Tap options look like this :


Btw: jmcilhenney : Your link to : "How Do I?" Videos : " for Visual Basic no longer work .
When-SetReg-2.png


Btw: jmcilhinny : Your link to "How Do I?" Videos " no longer works , I don't know about the other ones .
 
I'm surprised nobody corrected you on your approach.

What are the consequences of editing these registry values for users who only use IE?
How does your app restore the original values when your application is not in use, or if your program is removed from the computer?

There is nothing stopping you from creating an encrypted file which can be stored in %appdata% in your own app folder and read back to a list of allowed websites when your app is running. If your browser tries to navigate to a site that's not on the white list, it simply doesn't navigate anywhere. similarly to : How to Capture a click on WebBrowser1 Event ?
 
Let it be noted, that now-a-days, its generally not a good idea go editing the windows registry. Especially registry which does not belong to your application.

Instead, I advocate you create your own config files in your own %appdata% folder instead. Find alternate methods to do what you want to do without touching the registry of other applications.
 
Thanks Sheepings ;

Hmm...
Encrypted file , Is there a facility in vb.net .vb for encryption . Sounds interesting .
 
Last edited by a moderator:
What are the consequences of editing these registry values for users who only use IE?
I'm surprised you would care about such people. ;) Given that this is for a kid-safe browser, I would imagine that they're not supposed to be using other browsers, IE or not. That said, understanding the full implications of your actions is never a bad thing.
 
Web browser control is based on settings from IE. I don't agree to changing the values of the registry of other programs, when their are a variety of other ways including events which can help with this. Besides, isn't is generally considered bad practice now a days to be storing data in the registry, when we know %appdata% is more suitable. Editing the registry is also so old.
 

Latest posts

Back
Top