如何在表单加载事件上设置WebBrowser.DocumentText [英] How to set WebBrowser.DocumentText on form load event

查看:79
本文介绍了如何在表单加载事件上设置WebBrowser.DocumentText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个电子邮件阅读器并使用WebBrowser控件显示电子邮件的HtmlBody。一切都很好,除了我无法让这个控件在加载表单时接受DocumentText。



这个重要的原因是因为.eml文件默认使用此程序打开,我需要它在表单加载时立即显示电子邮件正文。所有其他控件都在加载数据,比如To,From,Subject TextBox控件。
$


我注意到一些有趣的东西,如果我在设置之前弹出MessageBox WebBrowser.DocumentText,然后控件接受并显示数据。



感谢您的帮助!

I'm creating an email reader and using the WebBrowser control to display the HtmlBody of emails. Everything works great, except that I can't get this control to accept the DocumentText on the loading of the form.

The reason this is important is because .eml files are defaulted to open with this program, and I need it to display the email body right away when the form loads. All other controls are loading the data, like the To, From, Subject TextBox controls.

I noticed something funny, if I have a MessageBox pop up right before I set the WebBrowser.DocumentText, then the control accepts and displays the data.

Thanks for the help!

private void EmailViewer_Load(object sender, EventArgs e)
        {
			...some code to fill a DataGridView with emails from the folder of the .eml used to open program...
			OpenInitialEmail();
        }
		
		
		private void OpenInitialEmail()
        {
            foreach (DataGridViewRow row in dgvEmailList.Rows)
            {
                if (row.Cells["FilePath"].Value.ToString() == UserSettings.OpenToEmailFullPath.ToString())
                {
                    DataGridViewCellEventArgs args = new DataGridViewCellEventArgs(0, row.Index);
                    dgvEmailList.Rows[row.Index].Selected = true;
                    dgvEmailList_RowEnter(this, args);
                    return;
                }
            }
        }
		
        private void dgvEmailList_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            //Get file path from DGV
            DataGridViewRow row = dgvEmailList.Rows[e.RowIndex];
            string path = row.Cells["FilePath"].Value.ToString();

            //Create Mime to hold email, and MimeEntities to hold attachments.
            Mime m = Mime.Parse(path);
            MimeEntity[] attachments = m.Attachments;
            
            //Load UI Controls with data from the email.
            txtToAddress.Text = (m.MainEntity.To != null) ? m.MainEntity.To.ToAddressListString(): "";
            txtFromAddress.Text = m.MainEntity.From.ToAddressListString();
            txtSubject.Text = m.MainEntity.Subject;
            txtDateOfEmail.Text = m.MainEntity.Date.ToShortDateString() + ' ' + m.MainEntity.Date.TimeOfDay.ToString();

            //Load body of email into webEmailBody control.
            //If Body is Text, replace line feeds with html br/ elements.
            string NL = Environment.NewLine.ToString();
			
			MessageBox.Show("WITH THIS HERE, IT WORKS. WITHOUT IT, webEmailBody does not disply the email.");
            webEmailBody.DocumentText = m.BodyHtml ?? m.BodyText.Replace(NL, "<br/>");
			
			...More code to populate list of attachments...
		}

推荐答案

我放置了一个Application.DoEvents();设置DocumentText之后,这似乎有效。 仍然可以使用任何更好的解决方案!

I placed an Application.DoEvents(); after setting the DocumentText and that seems to work.  Still open to any better solutions!

我唯一的问题是每次触发dgvEmailList_RowEnter时都会触发Application.DoEvents(),这会在用户使用箭头键快速滚动时减慢应用程序的运行速度通过DGV。 当它们使用箭头键快速滚动
列表时,它也会抛出此错误:

My only issue with it is Application.DoEvents() is firing every time dgvEmailList_RowEnter is fired which slows down the application ALOT when user uses arrow keys to scroll quickly through the DGV.  It also throws this error when they scroll through list too fast with arrow keys:

System.Windows.Forms.dll中出现未处理的"System.InvalidOperationException"类型异常



附加信息:操作无效,因为它导致对SetCurrentCellAddressCore函数的重新调用。

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

Additional information: Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.

即使将其放入尝试捕捉..应用程序仍在缓慢触发DoEvents()这么多。

Even putting it in a try catch.. the app is still slow firing DoEvents() so much.


这篇关于如何在表单加载事件上设置WebBrowser.DocumentText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆