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()
 

benshaws

Hobbyist Programmer
Joined
Sep 19, 2013
Messages
18
Location
Leeds, UK
Programming Experience
5-10
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.
:)
 

vmars316

Active member
Joined
Aug 24, 2020
Messages
39
Programming Experience
Beginner
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...
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
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.
 

vmars316

Active member
Joined
Aug 24, 2020
Messages
39
Programming Experience
Beginner
Thank you jmcilhinney ;
Thanks for that primer , Exactly what I needed .
I will study this a while ,
And get back to you .
Thanks
 

vmars316

Active member
Joined
Aug 24, 2020
Messages
39
Programming Experience
Beginner
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
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
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.
 

vmars316

Active member
Joined
Aug 24, 2020
Messages
39
Programming Experience
Beginner
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 .
 

Sheepings

Senior Programmer
Staff member
Joined
Mar 7, 2014
Messages
132
Location
UK
Programming Experience
10+
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 ?
 

Sheepings

Senior Programmer
Staff member
Joined
Mar 7, 2014
Messages
132
Location
UK
Programming Experience
10+
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.
 

vmars316

Active member
Joined
Aug 24, 2020
Messages
39
Programming Experience
Beginner
Thanks Sheepings ;

Hmm...
Encrypted file , Is there a facility in vb.net .vb for encryption . Sounds interesting .
 
Last edited by a moderator:

Sheepings

Senior Programmer
Staff member
Joined
Mar 7, 2014
Messages
132
Location
UK
Programming Experience
10+
Start searching the interwebs for VB.Net encrypting a json file.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
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.
 

Sheepings

Senior Programmer
Staff member
Joined
Mar 7, 2014
Messages
132
Location
UK
Programming Experience
10+
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.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
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.
Generally speaking, I would agree. If it's in a specific environment though, it's not necessarily a bad thing. If you're in a situation where only children are using the system and those children should only be using your browser and never IE directly then making changes that affect IE aren't necessarily a problem. If you're in a situation where other people use the same machine but use Edge or Firefox or Chrome then it still doesn't matter. That said, it is a good idea to understand exactly what the ramifications of doing so are.
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.
I would agree that it's not a good idea to store your own app's data and settings in the Registry but that's not what's happening here. This is a case of making use of data that another app is already storing in the Registry.

Having said all that, your idea of a whitelist of sites may well be the superior option regardless. That is, as long as the user can't just open an IE window and navigate elsewhere. It's actually much more complex to prevent that in a WebBrowser control than simply handling the NewWindow event. There are various specific ways that new windows can be opened and that only catches some of them.

Another point to consider is whether using IE via the WebBrowser control is a good idea in the first place. There is the WebView and WebView2 controls available that use the old and new Edge browsers and provide more control and a better interface nowadays, so they may be a better option.
 

Sheepings

Senior Programmer
Staff member
Joined
Mar 7, 2014
Messages
132
Location
UK
Programming Experience
10+
It only webview 2 which is in WPF, and I do agree with you that this might be more suitable than what the OP is currently doing. It's webview which only belongs to Android if I recall.

Anyway, getting late now. 4AM - Time to hit the sack. Good night!
 
Top Bottom