2017-09-18 17 views
0

私は、Ctrl + Sなどの組み合わせを入力したときにイベントが発生するようなテキストエディタプログラムを作成しています。トリガーされていないようです。エラーがイベントハンドラがC#でどのように初期化されているかについて私が経験していないことが原因である可能性があります。すべての助けをいただければ幸いです。C#のキーストロークをキャッチする方法

のForm1.cs:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Colied_Text 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void textBox1_CtrlS(object sender, KeyEventArgs e)  //Save file 
     { 
      if(e.Control && e.KeyCode == Keys.S) 
      { 
       textBox1.Text = "Ctrl+S for save"; 
      } 
     } 
    } 
} 

Form1.Designer.cs:trueにプロパティKeyPreviewを設定

namespace Colied_Text 
{ 
    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.textBox1 = new System.Windows.Forms.TextBox(); 
      this.SuspendLayout(); 
      // 
      // textBox1 
      // 
      this.textBox1.AcceptsReturn = true; 
      this.textBox1.AcceptsTab = true; 
      this.textBox1.AllowDrop = true; 
      this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
      | System.Windows.Forms.AnchorStyles.Left) 
      | System.Windows.Forms.AnchorStyles.Right))); 
      this.textBox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); 
      this.textBox1.Font = new System.Drawing.Font("Consolas", 10F, System.Drawing.FontStyle.Bold); 
      this.textBox1.ForeColor = System.Drawing.SystemColors.Menu; 
      this.textBox1.Location = new System.Drawing.Point(12, 10); 
      this.textBox1.MaxLength = 100000; 
      this.textBox1.Multiline = true; 
      this.textBox1.Name = "textBox1"; 
      this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both; 
      this.textBox1.Size = new System.Drawing.Size(645, 339); 
      this.textBox1.TabIndex = 0; 
      this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_CtrlS); 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(64))))); 
      this.ClientSize = new System.Drawing.Size(669, 361); 
      this.Controls.Add(this.textBox1); 
      this.Name = "Form1"; 
      this.Text = "CoTxEd"; 
      this.ResumeLayout(false); 
      this.PerformLayout(); 

     } 

     #endregion 

     private System.Windows.Forms.TextBox textBox1; 
    } 
} 
+0

@derloopkat @derloopkat私は、それが処理するキーストロークを示すためにその名前の機能を割り当てました。 –

答えて

2

。イベントがフォーカスを持つコントロールに渡される前に、フォームはキーイベントを受け取ります。

public Form1() 
{ 
    InitializeComponent(); 
    this.KeyPreview = true; 
} 
+0

これは完璧に機能しました、ありがとう! –

関連する問題