我现在情况是这样的
在winfrom应用程序里  在一个窗体里我引用一个Web页进来 我想关闭这个窗体的时候 按ESc键 就可以关闭窗口 如果这个web页面上没有像TextBox之类的控件 可以正常关闭 但是有了这类的控件 当页面打开焦点就在页面上的控件上 窗体按Esc键 就关闭不了

解决方案 »

  1.   

    private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Escape)
                {
                    HtmlElementCollection input = webBrowser1.Document.GetElementsByTagName("input");                foreach (HtmlElement element in input)
                    {
                        if (element.GetAttribute("type") == "text")
                        {
                            return;
                        }
                    }                this.Close();
                }
            }
      

  2.   

    遍历每个TextBox上
    并加 onkeyup事件 就行了。
      

  3.   

    如果web页面里没有TextBox控件  窗体可以正常关闭
    但是有了TextBox控件 就关闭不了
      

  4.   

    还要注意一点Form1_KeyDown的事件要触发的话 必须将窗体的KeyPreview属性设置成True
    这样才会触发的还有一点当你焦点在webBrowser上的时候,由于焦点在web页的控件上  Form1_KeyDown事件会失效
    所以最好Page_Load的时候焦点设置在其他form控件上
      

  5.   

    对呀  就如我上面所说  web页上有控件并且焦点还在的时候确实是不会触发Form1_KeyDown事件的
    但是你可以用webBrowser1_PreviewKeyDown这个事件 就是webBrowser的事件来做处理
    是可以触发的
      

  6.   

    不过 现在有个为题就是 窗体拿不到 web页面的事件
      

  7.   

    是winfrom窗体拿不到Web页面事件 
      

  8.   

    webBrowser1_PreviewKeyDown?还是?  
      

  9.   

     不知道 大家有没有用过 股票分析系统 
    比如说 里面有上市公司的 一些财务报表啊 以网页的形式内嵌在winfrom窗体内的  
      

  10.   

    没有 你是不是想把自己的制定网页或者说HTML内容显示在winform上?
    那就可以用
    webBrowser1.DocumentText = "<html>...";
      

  11.   

    实验成功   partial class Form1
        {
            /// <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");
            }
      

  12.   

    我想我大概明白楼主的意思了。他是想让用户按下esc的时候能够关闭当前窗口。
    当前窗口里面有webBrowser
    如果这个webBrowser的页面里面有textbox框,并且焦点在这个textbox框上,按下esc的键盘事件不能够触发,于是不能关闭窗口。
    现在要求大家给个解决方案,焦点在webBrowser中的textbox框里时,按下esc也能关闭窗口。17楼写的东西,估计楼主已经写好了。所以是白写了。
      

  13.   

    这个倒是没做过。如果焦点在textbox上的话,实在是没有好的办法。
      

  14.   

    焦点在input type='text' 里可以escusing System;
    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();
            }
        }
    }