我现在情况是这样的
在winfrom应用程序里 在一个窗体里我引用一个Web页进来 我想关闭这个窗体的时候 按ESc键 就可以关闭窗口 如果这个web页面上没有像TextBox之类的控件 可以正常关闭 但是有了这类的控件 当页面打开焦点就在页面上的控件上 窗体按Esc键 就关闭不了
在winfrom应用程序里 在一个窗体里我引用一个Web页进来 我想关闭这个窗体的时候 按ESc键 就可以关闭窗口 如果这个web页面上没有像TextBox之类的控件 可以正常关闭 但是有了这类的控件 当页面打开焦点就在页面上的控件上 窗体按Esc键 就关闭不了
{
if (e.KeyCode == Keys.Escape)
{
HtmlElementCollection input = webBrowser1.Document.GetElementsByTagName("input"); foreach (HtmlElement element in input)
{
if (element.GetAttribute("type") == "text")
{
return;
}
} this.Close();
}
}
并加 onkeyup事件 就行了。
但是有了TextBox控件 就关闭不了
这样才会触发的还有一点当你焦点在webBrowser上的时候,由于焦点在web页的控件上 Form1_KeyDown事件会失效
所以最好Page_Load的时候焦点设置在其他form控件上
但是你可以用webBrowser1_PreviewKeyDown这个事件 就是webBrowser的事件来做处理
是可以触发的
比如说 里面有上市公司的 一些财务报表啊 以网页的形式内嵌在winfrom窗体内的
那就可以用
webBrowser1.DocumentText = "<html>...";
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows Form Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// webBrowser1
//
this.webBrowser1.Location = new System.Drawing.Point(41, 114);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(433, 250);
this.webBrowser1.TabIndex = 0;
this.webBrowser1.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.webBrowser1_PreviewKeyDown);
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 51);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Open163";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(513, 399);
this.Controls.Add(this.button1);
this.Controls.Add(this.webBrowser1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); } #endregion private System.Windows.Forms.WebBrowser webBrowser1;
private System.Windows.Forms.Button button1; private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Escape)
this.Close();
} private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Url = new Uri("http://www.163.com");
}
}
当前窗口里面有webBrowser
如果这个webBrowser的页面里面有textbox框,并且焦点在这个textbox框上,按下esc的键盘事件不能够触发,于是不能关闭窗口。
现在要求大家给个解决方案,焦点在webBrowser中的textbox框里时,按下esc也能关闭窗口。17楼写的东西,估计楼主已经写好了。所以是白写了。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication142
{
public partial class Form1 : Form
{
WebBrowser WB = new WebBrowser(); public Form1()
{
InitializeComponent(); WB.Parent = this;
WB.Dock = DockStyle.Fill; WB.DocumentText = "<html><head></head><body>"
+ "<input type='text'></input>"
+ "</body></html>";
WB.PreviewKeyDown += new PreviewKeyDownEventHandler(WB_PreviewKeyDown);
} void WB_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Escape)
Close();
}
}
}