tags:

views:

1617

answers:

4

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.

+4  A: 

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?

Franci Penov
Gah, I got downvote because some people don't know they can cast the browser to Control to get to the Enabled property?!
Franci Penov
I suspect that the questioner looked for an Enabled property and the WebBrowser control make a good job of hiding the fact that it has one hidden away. The fact that it hides the Enabled property makes me wonder whether that property actually works in all cases on all OSes.
andynormancx
It does not show in Intellisense because of a conflict between the Enabled inherited from Control and Enabled inherited from WebBrowserBase. However, the property is documented and downcasting to Control works on all OSes.
Franci Penov
+1  A: 

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;
    }
}
LoveMeSomeCode
+3  A: 

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.

andynormancx
Why would it hide that explicitly? Does it work?
ck
Yes it works. But I understand your concern, I don't know why it hides it.
andynormancx
Why the down vote ? Are you worried that Enabled it hidden for a reason or something else ?
andynormancx
It's possible it was downvoted before you edited your post.
Franci Penov
A: 

Can you remove the webbrowser from its parent Control array?

ck