AxWebBrowser problem

Posted on 2003-02-19
Medium Priority
Last Modified: 2012-05-04
I have a form that uses an AxWebBrowser control.  However, it will not navigate to any site or local file.  What is the problem and how can I fix it?  I have tried everything.  Please help.  By the way, IDocHostUIHandler and ICustomDoc are impliemented corectly in another file in the namespace WebBrowser and Slide is a class with members and properties fileName and title.

        public class SlideView : System.Windows.Forms.Form,
#region members            
            private AxSHDocVw.AxWebBrowser browserControl;
            private Slide slide;
#endregion members

#region constructors            
            // should I Delete this constructor?
            public SlideView()
            public SlideView(Slide slide)
                  if (slide.FileName == String.Empty)
                        slide.FileName = GLOBALS._Document.MainPath + @"\Slides\" + slide.Title + ".htm";
                  this.slide = slide;
                  this.Text = slide.Title;
#endregion constructors            

#region properties
            public Slide Slide {
                  get {
                        return slide;
#endregion properties

#region methods
            private void InitBrowser()
                  browserControl = new AxSHDocVw.AxWebBrowser();
                  browserControl.Location = new Point(0,0);
                  browserControl.Size = this.ClientSize;
                  browserControl.DocumentComplete += new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(OnDocumentComplete);
                        // this does not work for any site!!!!!! Please help.
                        object o = null;
                        browserControl.Navigate("www.yahoo.com", ref o, ref o, ref o, ref o);
#endregion methods

#region autogen            
            void InitializeComponent() {
                  // CreatedForm
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.BackColor = System.Drawing.SystemColors.ActiveCaption;
                  //this.ClientSize = new System.Drawing.Size(292, 266);
                  this.Name = "SlideView";
#endregion autogen            
#region events            
            protected override void OnSizeChanged(EventArgs e)
                  browserControl.Size = this.ClientSize;
            public void OnDocumentComplete(object sender, DWebBrowserEvents2_DocumentCompleteEvent e)
                  MessageBox.Show("document complete");
                  WebBrowser.ICustomDoc customDoc = (WebBrowser.ICustomDoc)
#endregion events
#region DocHostUI            
            public uint ShowContextMenu(uint dwID, ref /*tagPOINT*/UtilityLibrary.Win32.POINT pPoint,
                                    [MarshalAs(UnmanagedType.IUnknown)] object pcmdtReserved,
                                    [MarshalAs(UnmanagedType.IDispatch)] object pdispReserved)
                // use this code to show a custom menu
              /*const int Ok = 0;
                 Point p = new Point(pPoint.x, pPoint.y);
                  p = PointToClient(p);
                  myCustomContextMenu.Show(this, p);
                  throw new COMException("", Ok); */// HRESULT = S_OK: MSHTML won't show its menu

                  // use this code to let MsHtml shows its menu
                  System.Windows.Forms.MessageBox.Show("HI from cm");
                  const int Error = 1;
                  throw new COMException("", Error); // HRESULT = S_FALSE: MSHTML will show its menu

          public void GetHostInfo(ref WebBrowser.DOCHOSTUIINFO theHostUIInfo)
                  // turn three flags on
                  theHostUIInfo.dwFlags |= (uint) (/*WebBrowser.DOCHOSTUIFLAG.DOCHOSTUIFLAG_SCROLL_NO |
          public void ShowUI(uint dwID,
                        [MarshalAs(UnmanagedType.Interface)] IntPtr pActiveObject,
                        [MarshalAs(UnmanagedType.Interface)] IntPtr pCommandTarget,
                        [MarshalAs(UnmanagedType.Interface)] IntPtr pFrame,
                        [MarshalAs(UnmanagedType.Interface)] IntPtr pDo) {}

          public void HideUI() {}
          public void UpdateUI() {}
          public void EnableModeless(int fEnable) {}
          public void OnDocWindowActivate(int fActivate) {}
          public void OnFrameWindowActivate(int fActivate) {}
          ///<see DocHostUIHandler.cs></see>
          public void ResizeBorder(ref /*tagRECT*/object prcBorder,
                                   [MarshalAs(UnmanagedType.Interface)] IntPtr pUIWindow,
                                   int fRameWindow) {}
          public void TranslateAccelerator(ref tagMSG lpMsg, ref Guid pguidCmdGroup, uint nCmdID)
                  //const int Ok = 0;
                  const int Error = 1;
                  /*const int WM_KEYDOWN = 0x0100;
                  const int VK_CONTROL = 0x11;
                  if (lpMsg.message != WM_KEYDOWN)
              // allow message
              throw new COMException("", Error); // returns HRESULT = S_FALSE

                if (GetAsyncKeyState(VK_CONTROL) >= 0)
              // Ctrl key not pressed: allow message
                    throw new COMException("", Error); // returns HRESULT = S_FALSE

                  // disable the Ctrl-N and Ctrl-P accelerators
                  lpMsg.wParam &= 0xFF; // get the virtual keycode
                  if ( (lpMsg.wParam == 'N') || ((lpMsg.wParam == 'P')) )
                    throw new COMException("", Ok); // returns HRESULT = S_OK

                  */// allow everything else
                  throw new COMException("", Error); // returns HRESULT = S_FALSE
          public void GetOptionKeyPath(ref string pchKey, uint dw) {}
          public void GetDropTarget([MarshalAs(UnmanagedType.Interface)] IntPtr pDropTarget,
                                    [MarshalAs(UnmanagedType.Interface)] ref IntPtr ppDropTarget)

          public void GetExternal([Out, MarshalAs(UnmanagedType.IDispatch)] out object ppDispatch)
                  ppDispatch = null;

          public void TranslateUrl(uint dwTranslate, string URLIn, ref string URLOut)  
                  URLOut = URLIn; // add code here to modify the Url the user is trying to
                  // navigate to. Ex:
                  //      // prevent users from leaving your web site
                  //      if (URLIn.IndexOf("mywebsite.com") < 0)
                  //        URLOut = "http://www.mywebsite.com";
                  //      else
                  //        URLOut = URLIn;

          public void FilterDataObject(IDataObject pDO, ref IDataObject ppDORet)
                  //      return doIn;
#endregion DocHostUI
Question by:thedude112286
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 8019261

did you solve the problem until now?
I use a webbrowser class with axshdocvw that gives me the possibility to add a webbrowser control to my windows application. It works fine, if you still need, I could copy it here?


Author Comment

ID: 8020499
I haven't yet resolved the problem and if you wouldn't mind, I think it would be a big help if you could post the webbrowser class.  Thanks.

Accepted Solution

ACGT earned 300 total points
ID: 8020685
You can use this class as a control.
You have to reference mshtml.dll and you have to import shdocvw.dll.
The main class that uses the control only has to call WebbrowserControl.Navigate("your URL");
Try and give a sign, if I didn't explain enough :-)


using System;
using mshtml;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServices;

    #region Delegates
      public delegate void WebBrowserElementEventHandler(object sender, mshtml.IHTMLEventObj e);

      public class WebBrowser : System.Windows.Forms.UserControl
        #region Control Components
            private System.Windows.Forms.Panel pnlWeb;
            private AxSHDocVw.AxWebBrowser webMain;

            private System.ComponentModel.Container components = null;

        #region Enumerators
            public enum ieScrollBars {None, Always, Auto};

        #region Event Handler Declarations
            public event WebBrowserElementEventHandler WebBrowserElementEvent;

        #region Constructor(s)
            public WebBrowser()

                  // Go to the blank page, initializes the WebBrowser control to handle HTML documents.
                  Navigate("about:blank", true);
                  // Wait for the control to initialize to HTML and load the blank page.
                  while (body == null) {Application.DoEvents();}

                  // Set default properties for the control to appear like a generic label control.
                  pnlWeb.BorderStyle = BorderStyle.None;
                  //ScrollBars = ScrollBars.none;
                  this.Text = "";
                  this.HTML = "";
                  body.style.font = "8.25pt Microsoft Sans Serif, 10pt Verdana";
                  body.style.margin = "0";
                  body.style.borderStyle = "none";
                  body.style.background = "buttonface";
                  ScrollBars = ieScrollBars.None;

        #region Component Designer generated code
            protected override void Dispose( bool disposing )
                  if (disposing) if (components != null) components.Dispose();

            private void InitializeComponent()
                  System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(WebBrowser));
                  this.pnlWeb = new System.Windows.Forms.Panel();
                  this.webMain = new AxSHDocVw.AxWebBrowser();
                  // pnlWeb
                  this.pnlWeb.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
                  this.pnlWeb.Controls.AddRange(new System.Windows.Forms.Control[] {
                  this.pnlWeb.Dock = System.Windows.Forms.DockStyle.Fill;
                  this.pnlWeb.Name = "pnlWeb";
                  this.pnlWeb.Size = new System.Drawing.Size(392, 360);
                  this.pnlWeb.TabIndex = 1;
                  // webMain
                  this.webMain.ContainingControl = this;
                  this.webMain.Dock = System.Windows.Forms.DockStyle.Fill;
                  this.webMain.Enabled = true;
                  this.webMain.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("webMain.OcxState")));
                  this.webMain.Size = new System.Drawing.Size(388, 356);
                  this.webMain.TabIndex = 0;
                  // WebBrowser
                  this.Controls.AddRange(new System.Windows.Forms.Control[] {
                  this.Name = "WebBrowser";
                  this.Size = new System.Drawing.Size(392, 360);


        #region Public Control Properties
            // Sets the scroll bars of body of the document.
            /// <summary>
            /// Special Internet Explorer BODY.Scroll Property
            /// </summary>
            [Description("Internet Explorer's BODY.Scroll Property")]
            public ieScrollBars ScrollBars
                        if (body.scroll.Equals("yes")) return ieScrollBars.Always;
                        if (body.scroll.Equals("auto")) return ieScrollBars.Auto;
                        return ieScrollBars.None;
                        if (value == ieScrollBars.Auto) body.scroll = "auto";
                        if (value == ieScrollBars.None) body.scroll = "no";
                        if (value == ieScrollBars.Always) body.scroll = "yes";

            public BorderStyle BorderStyle
                  get {return pnlWeb.BorderStyle;}
                  set {pnlWeb.BorderStyle = value;}


        #region Public HTML Properties

            [Browsable(false)]  // Do not show in the properties box
            public HTMLDocument document
            { get {return (HTMLDocument) webMain.Document;} }
            [Browsable(false)]  // Do not show in the properties box
            public HTMLBody body
            { get {return (HTMLBody) document.body;} }

            // Document Body's innerText
            [DefaultValue("")] // Let the properties box know the default value
            [Browsable(true)]  // Do show in the properties box
            [Description("Internet Explorer's BODY.innerText Property")]
            public override string Text
                  get {return body.innerText;}
                  set {body.innerText = value;}

            // Document Body's innerHTML
            [Description("Internet Explorer's BODY.innerHTML Property")]
            public string HTML
                  get {return body.innerHTML;}
                  set {body.innerHTML = value;}


        #region COM Event Handler for WebBrowser HTML Elements
            // COM Event Handler for HTML Element Events
            public void DefaultMethod()
                  // Call the custom Web Browser HTML event
                  WebBrowserElementEvent(this, document.parentWindow.@event);

        #region Public Methods
            /// <summary>
            /// Navigates to a web page, defaults to waiting for the page to load before continuing.
            /// </summary>
            /// <param name="url">The URL to be redirected to</param>
            public void Navigate(string url)
            {Navigate(url, true); }

            /// <summary>
            /// Navigates to a web page
            /// </summary>
            /// <param name="url">The URL to be redirected to</param>
            /// <param name="wait">Wait for the page to load before continuing</param>
            public void Navigate(string url, bool wait)
                  // Creates the null missing value object
                  object o = System.Reflection.Missing.Value;

                  // Resets the browser to an empty container, cleaning the slate
                  if (wait) webMain.Navigate(null, ref o, ref o, ref o, ref o);

                  // Wait until the browser is empty
                  if (wait) while (document != null) {Application.DoEvents();}

                  // Go to the new URL
                  webMain.Navigate(url, ref o, ref o, ref o, ref o);
                  //if (wait) while (document.body == null) {Application.DoEvents();}
                  if (wait) while (webMain.Busy) {Application.DoEvents();}


Author Comment

ID: 8029375
Thank you very much.  This worked perfectly.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question