MD6380
MD6380

Reputation: 1007

C# how to disable webbrowser usage

I have a C# application that has a webbrowser and need to be able to completely disable it at times. What is the best method of doing this?

I was thinking about creating a transparent panel to put over the webbrowser when needed, but am not sure if this is the best approach. I also noticed that C# doesn't easily allow making a panel transparent.

Edit: To clarify what I mean by disable...I don't want to allow the user to be able to click or change\edit anything in the webbrowser.

Upvotes: 5

Views: 14777

Answers (5)

user2048591
user2048591

Reputation: 1

In VB: DirectCast(WebBrowser1, Control).Enabled = False

Upvotes: 0

andynormancx
andynormancx

Reputation: 13742

Strangely the WebBrowser control doesn't have an Enabled property as you might expect it to. You can set the AllowNavigation property to false which will stop the user clicking on links.

Turns out it does have an Enabled property, inherited from Control. But you have to explicitly cast it to Control to access it:

((Control)webBrowser1).Enabled = false;

You should be aware though that setting Enabled completely disables any user interaction with the WebBrowser control. That includes the scroll bar, so once you set Enabled to false the user can't even scroll the web page in the control.

If that was more than you wanted then setting the AllowNavigation property to false will stop the user clicking on links but still allow them to scroll the page. You'd need to check though that it also stops Javascript onclick events from firing.

If you wanted to stop any events from firing in the page you could probably achieve that will some DOM work from the WebForms code into the WebBrowser control. You also probably need to start disabling input controls within the WebBrowser. It all depends how disabled you want it.

Upvotes: 18

LoveMeSomeCode
LoveMeSomeCode

Reputation: 3937

When you say 'disable' it, what do you mean? If you set the 'Enabled' property to false, it will be disabled, this works for all form controls. But if you needed to, you can set a panel to transparent by setting it's BackColor.

UPDATE: Actually, you're right, it's an wrapper for an ActiveX control, so it's not a typical WinForms control. What you want is the transparent paenl solution. This guy Tobi has a solution on his blog:

http://saftsack.fs.uni-bayreuth.de/~dun3/archives/creating-a-transparent-panel-in-net/108.html

the basic idea is overriding this method on the panel class:

protected override CreateParams CreateParams
{
    get
    {
         CreateParams createParams = base.CreateParams;
         createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
         return createParams;
    }
}

Upvotes: 1

cjk
cjk

Reputation: 46415

Can you remove the webbrowser from its parent Control array?

Upvotes: 0

Franci Penov
Franci Penov

Reputation: 75982

Based on your last comment, I would assume you are using WinForms. (WPF does allow for transparent panels quite easily :-)) This means you are using the WebBrowser control. Have you tried setting its Enabled property to false?

Upvotes: 3

Related Questions