WebBrowser or WPF control content may not display correctly in an Office application when hosted inside a CustomTaskPane control

Applies to: Office ProductsOutlook DevelopmentOutlook 2019

Symptoms


In some situations, the WebBrowser or WPF control content may not display or function correctly in an Office application when the control is hosted inside a CustomTaskPane control.

Cause


This issue might be caused by the WebBrowser or WPF controls themselves. Host applications that don't return focus to HTML elements which are rendered inside WebBrowser or WPF controls might also cause this issue.

Workaround


To work around this issue, add an intermediary form between the CustomTaskPane control and the WebBrowser or WPF control. For more information about how to add a custom task pane to an application, see Add a custom task pane to an application.

For WPF control rendering issues, we recommend that you host a WPF UserControl inside a WindowsForm and add an intermediary form. To add an intermediary form, see Walkthrough: Hosting a 3-D WPF Composite Control in Windows Forms.

For WebBrowser control rendering issues, use the UserControl MyUserControl to implement the workaround as follows:

  1. Add a UserControl to your add-in and name it MyUserControl.
  2. Change the source code of your UserControl and declare the Load, Paint, and Resize event handlers. See MyUserControl.cs in the source code example below.
  3. Add a WindowsForm form to your project. Name it WorkaroundForm and declare the Load event handler.
  4. Add your WebBrowser control to WorkaroundForm. See WorkaroundForm.cs in the source code example below.
  5. Create a CustomTaskPane and add an instance of MyUserControl to it.
// ThisAddIn.cs namespace TaskPaneWorkaround{    public partial class ThisAddIn    {        private MyUserControl myUserControl1;        private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;         private void ThisAddIn_Startup(object sender, System.EventArgs e)        {            myUserControl1 = new MyUserControl();            myCustomTaskPane = this.CustomTaskPanes.Add(myUserControl1, "My Task Pane");            myCustomTaskPane.Visible = true;        }         private void ThisAddIn_Shutdown(object sender, System.EventArgs e)        {        }         #region VSTO generated code         /// <summary>        /// Required method for Designer support - do not modify        /// the contents of this method with the code editor.        /// </summary>        private void InternalStartup()        {            this.Startup += new System.EventHandler(ThisAddIn_Startup);            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);        }                #endregion    }} // MyUserControl.cs using System;using System.Windows.Forms;using System.Runtime.InteropServices; namespace TaskPaneWorkaround{    public partial class MyUserControl: UserControl    {        bool isformdisplayed = false;        WorkaroundForm workaroundForm;         [DllImport("user32.dll", SetLastError = true)]        private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);         public MyUserControl()        {            this.SuspendLayout();            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;            this.Name = "MyUserControl";            this.Paint += new System.Windows.Forms.PaintEventHandler(this.MyUserControl_Paint);            this.Resize += new System.EventHandler(this.MyUserControl_Resize);            this.ResumeLayout(false);             this.Paint += MyUserControl_Paint;            this.Resize += MyUserControl_Resize;        }         private void MyUserControl_Load(object sender, System.EventArgs e)        {           this.Paint += MyUserControl_Paint;        }         private void MyUserControl_Paint(object sender, PaintEventArgs e)        {            if (!isformdisplayed)            {                this.SuspendLayout();                workaroundForm = new WorkaroundForm();                SetParent(workaroundForm.Handle, this.Handle);                workaroundForm.Dock = DockStyle.Fill;                workaroundForm.Width = Width;                workaroundForm.Height = Height;                workaroundForm.Show();                isformdisplayed = true;                this.ResumeLayout();            }        }         private void MyUserControl_Resize(object sender, EventArgs e)        {            if (isformdisplayed)            {                workaroundForm.Width = this.Width;                workaroundForm.Height = this.Height;            }        }    }} //WorkaroundForm.cs using System;using System.Drawing;using System.Windows.Forms; namespace TaskPaneWorkaround{    public partial class WorkaroundForm: Form    {        public WorkaroundForm()        {            this.SuspendLayout();            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;            this.ClientSize = new System.Drawing.Size(509, 602);            this.Name = "Form1";            this.Text = "Form1";            this.Load += new System.EventHandler(this.WorkaroundForm_Load);            this.ResumeLayout(false);        }        private void WorkaroundForm_Load(object sender, EventArgs e)        {            this.SuspendLayout();            this.Location = new Point(0, 0);            this.Dock = DockStyle.Fill;            this.FormBorderStyle = FormBorderStyle.None;            WebBrowser webBrowser = new WebBrowser();            this.Controls.Add(webBrowser);            webBrowser.Location = new Point(0, 0);            webBrowser.Dock = DockStyle.Fill;            this.ResumeLayout();            webBrowser.Focus();            webBrowser.Navigate(new System.Uri("https://bing.com"));        }    }}

More information


Most of WebBrowser and WPF control rendering issues have been addressed by hotfixes. However, if you experience any issues where the control content isn't rendered correctly, focus isn't returned to HTML elements that are rendered inside the control, or certain key combinations don’t work as expected, consider the workaround in the Workaround section.

Author: v-venum

Writer: catagh

Tech Reviewers: mhaque

Editor: v-todmc