简体   繁体   中英

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.

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.

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?

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;
    }
}

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

你可以从其父控件数组中删除webbrowser吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM