如何检测 WebBrowser 控件的内容何时发生更改(在设计模式下)? [英] How do I detect when the content of a WebBrowser control has changed (in design mode)?
问题描述
我在设计模式下使用 WebBrowser
控件.
I'm using a WebBrowser
control in design mode.
webBrowser1.DocumentText = "<html><body></body></html>";
doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
doc.designMode = "On";
我有一个保存按钮,我想根据控件的内容是否已更改启用或禁用该按钮.
I have a save button that I would like to enable or disable depending on whether the contents of the control have changed.
我还需要知道控件的内容何时发生更改,因为我需要阻止用户离开控件而不接受说明其更改将丢失的确认消息框.
I also need to know when the contents of the control have changed as I need to stop the user from navigating away from the control without accepting a confirmation message box stating that their changes will be lost.
我找不到任何让我知道内容已更改的事件.
I can't find any events that would let me know that the contents have changed.
推荐答案
没有这样的事件,因为 DocumentText 是一个简单的字符串.我会创建一个字符串变量来存储最后保存的文本,并在每个 KeyDown/MouseDown/Navigating 事件中检查它.
There is no such event since DocumentText is a simple string. I would create a string variable storing the last saved text and check it at each KeyDown / MouseDown / Navigating event.
string lastSaved;
private void Form_Load(object sender, System.EventArgs e)
{
// Load the form then save WebBrowser text
this.lastSaved = this.webBrowser1.DocumentText;
}
private void webBrowser1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
// Check if it changed
if (this.lastSaved != this.webBrowser1.DocumentText)
{
// TODO: changed, enable save button
this.lastSaved = this.webBrowser1.DocumentText;
}
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
// Check if it changed
if (this.lastSaved != this.webBrowser1.DocumentText)
{
// TODO: ask user if he wants to save
// You can set e.Cancel = true to cancel loading the next page
}
}
这篇关于如何检测 WebBrowser 控件的内容何时发生更改(在设计模式下)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!