当前位置: 首页>>代码示例>>C#>>正文


C# Form.KeyPreview属性代码示例

本文整理汇总了C#中System.Windows.Forms.Form.KeyPreview属性的典型用法代码示例。如果您正苦于以下问题:C# Form.KeyPreview属性的具体用法?C# Form.KeyPreview怎么用?C# Form.KeyPreview使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在System.Windows.Forms.Form的用法示例。


在下文中一共展示了Form.KeyPreview属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Form1

//引入命名空间
using System.Windows.Forms;
using System.Security.Permissions;

public class Form1:
    System.Windows.Forms.Form

    // Declare the controls contained on the form.
{
    private MyMnemonicButton button1;
    internal System.Windows.Forms.ListBox ListBox1;

    public Form1() : base()
    {        

        // Set KeyPreview object to true to allow the form to process 
        // the key before the control with focus processes it.
        this.KeyPreview = true;

        // Add a MyMnemonicButton.  
        button1 = new MyMnemonicButton();
        button1.Text = "&Click";
        button1.Location = new System.Drawing.Point(100, 120);
        this.Controls.Add(button1);

        // Initialize a ListBox control and the form itself.
        this.ListBox1 = new System.Windows.Forms.ListBox();
        this.SuspendLayout();
        this.ListBox1.Location = new System.Drawing.Point(8, 8);
        this.ListBox1.Name = "ListBox1";
        this.ListBox1.Size = new System.Drawing.Size(120, 95);
        this.ListBox1.TabIndex = 0;
        this.ListBox1.Text = "Press a key";
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.ListBox1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

        // Associate the event-handling method with the
        // KeyDown event.
        this.KeyDown += new KeyEventHandler(Form1_KeyDown);
    }

    // The form will handle all key events before the control with  
    // focus handles them.  Show the keys pressed by adding the
    // KeyCode object to ListBox1. Ensure the processing is passed
    // to the control with focus by setting the KeyEventArg.Handled
    // property to false.
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        ListBox1.Items.Add(e.KeyCode);
        e.Handled = false;
    }

    [System.STAThreadAttribute]
    public static void Main()
    {
        Application.Run(new Form1());
    }
}

// This button is a simple extension of the button class that overrides
// the ProcessMnemonic method.  If the mnemonic is correctly entered,  
// the message box will appear and the click event will be raised.  
public class MyMnemonicButton:Button

    // This method makes sure the control is selectable and the 
    // mneumonic is correct before displaying the message box
    // and triggering the click event.
{
    [UIPermission(
        SecurityAction.Demand, Window = UIPermissionWindow.AllWindows)]
        protected override bool ProcessMnemonic(char inputChar)
    {

        if (CanSelect&&IsMnemonic(inputChar, this.Text))
        {
            MessageBox.Show("You've raised the click event " +
                "using the mnemonic.");
            this.PerformClick();
            return true;
        }
        return false;
    }
}
开发者ID:.NET开发者,项目名称:System.Windows.Forms,代码行数:86,代码来源:Form.KeyPreview

示例2: KeyPress

//引入命名空间
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace KeyPress
{
    /// <summary>
    /// Summary description for KeyPress.
    /// </summary>
    public class KeyPress : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public KeyPress()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (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.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(184, 104);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(80, 48);
      this.button1.TabIndex = 0;
      this.button1.Text = "button1";
      // 
      // button2
      // 
      this.button2.Location = new System.Drawing.Point(176, 192);
      this.button2.Name = "button2";
      this.button2.Size = new System.Drawing.Size(80, 32);
      this.button2.TabIndex = 1;
      this.button2.Text = "button2";
      // 
      // KeyPress
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.button2,
                                                                  this.button1});
      this.Name = "KeyPress";
      this.Text = "KeyPress";
      this.Load += new System.EventHandler(this.KeyPress_Load);
      this.ResumeLayout(false);

    }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new KeyPress());
        }

    private void KeyPress_Load(object sender, System.EventArgs e)
    {
      this.KeyPreview=true;
    }

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
      MessageBox.Show("Key Pressed = " + e.KeyChar.ToString());
    }
    }
}
开发者ID:C#程序员,项目名称:System.Windows.Forms,代码行数:111,代码来源:Form.KeyPreview


注:本文中的System.Windows.Forms.Form.KeyPreview属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。