是否可以在文本更改时刷新单独的WebBrowser控件而不会失去对文本框的关注?

时间:2015-07-02 14:32:30

标签: c# focus refresh webbrowser-control

我正在为我的公司设计文章编辑器,我希望能够在单独的WebBrowser窗口/控件中显示文章的实时预览。每次用户更改文章的某个字段中的任何内容时,WebBrowser控件都需要刷新页面。

以前,我在相同的表单上有WebBrowser控件,但由于空间原因,我不得不将其分解为单独的表单并使用编辑器表单上的按钮访问它。但是,由于我将该控件移动到一个单独的表单中,WebBrowser将重点放在每次刷新上,这意味着我可以键入一个字符,然后我必须单击回到我输入的文本框。

我的问题:有没有办法在后台刷新预览页面而不会盗取焦点,以便我可以更新预览以反映用户输入的内容,而不会在打字时打断用户?

以下是分别显示和刷新预览的方法:

private void buttonShowPreview_Click(object sender, EventArgs e)
    {
        if (buttonShowPreview.Tag == null)
        {
            Form browserForm = new Form();
            browserForm.FormClosing += new FormClosingEventHandler(delegate(Object form, FormClosingEventArgs args)
            {
                if (args.CloseReason == CloseReason.UserClosing)
                {
                    args.Cancel = true;
                    browserForm.Hide();
                    previewShowing = false;
                }
            });

            browserForm.Size = new System.Drawing.Size(1024, 768);
            browserForm.DesktopLocation = new System.Drawing.Point(0, 0);

            browserForm.Text = "Article Preview";

            preview = new WebBrowser();
            browserForm.Controls.Add(preview);
            preview.Dock = DockStyle.Fill;
            preview.Navigate("about:blank");

            buttonShowPreview.Tag = browserForm;
        }

        Form previewForm = buttonShowPreview.Tag as Form;

        previewForm.Show();
        previewShowing = true;
        RefreshPreview();
    }

private void RefreshPreview(string jumpToAnchor)
    {
        if (preview != null)
        {
            preview.Document.OpenNew(true);
            preview.Document.Write(structuredContent.GetStructuredContentHTML(content, jumpToAnchor, false));
            preview.Refresh(); 
        }
    }

1 个答案:

答案 0 :(得分:1)

根据Robberechts here的回答,尝试禁用父窗体,更新WebBrowser,然后在DocumentCompleted()事件中重新启用父窗体:

    private void buttonShowPreview_Click(object sender, EventArgs e)
    {
        if (buttonShowPreview.Tag == null)
        {
            Form browserForm = new Form();
            browserForm.FormClosing += new FormClosingEventHandler(delegate(Object form, FormClosingEventArgs args)
            {
                if (args.CloseReason == CloseReason.UserClosing)
                {
                    args.Cancel = true;
                    browserForm.Hide();
                }
            });

            preview = new WebBrowser();

            preview.DocumentCompleted += preview_DocumentCompleted; // handle the DocumentCompleted() event

            browserForm.Controls.Add(preview);
            preview.Dock = DockStyle.Fill;
            preview.Navigate("about:blank");

            buttonShowPreview.Tag = browserForm;
        }

        Form previewForm = buttonShowPreview.Tag as Form;

        previewForm.Size = new System.Drawing.Size(1024, 768);
        previewForm.DesktopLocation = new System.Drawing.Point(0, 0);

        previewForm.Text = "Article Preview";

        RefreshPreview();
        previewForm.Show();
    }

    private void RefreshPreview(string jumpToAnchor)
    {
        if (preview != null && preview.Parent != null)
        {
            preview.Parent.Enabled = false; // disable parent form

            preview.Document.OpenNew(true);
            preview.Document.Write(structuredContent.GetStructuredContentHTML(content, jumpToAnchor, false));
            preview.Refresh();
        }
    }

    private void preview_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser wb = sender as WebBrowser;
        if (wb.Parent != null)
        {
            wb.Parent.Enabled = true; // re-enable parent form
        }
    }