![](/img/trans.png)
[英]How to make Visual Studio 2010 recognize file extensions as C#/ASPX/C/C++ files?
[英]How to make Visual C# studio recognize key input
我對C#和一般編程很陌生。 基本上,我的問題是我試圖編寫一個使用鍵輸入的簡單代碼,但是當我運行(調試)程序時,它根本無法識別任何鍵輸入。 KeyPreview設置為true,但是它似乎仍然不做任何事情。 你能告訴我我在做什么錯嗎? 謝謝。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public List<string> list = new List<string>();
public Form1()
{
InitializeComponent();
KeyPreview = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F3)
{
list.Add("OMG!");
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(list[0]);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
在表單描述中編寫方法不會將其鏈接到有人按下某個鍵時觸發的事件。 在設計器內部(該視圖提供了表單的預覽並允許您對其進行拖放),在屬性面板中,頂部有一個閃電圖標。 如果按下它,它將列出以該形式公開的所有事件。 您可以雙擊KeyDown事件,它將自動創建正確的方法並添加:
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
在設計器自動生成的.Designer.cs文件中。 僅當表單內部的控件具有焦點時才需要KeyPreview ...可能是這種情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.