c#上按下键-退出程序



我的程序在整个屏幕上打开了一系列表单,我是否能够在转义方法中编码,因此在键入单词"test"时程序将关闭?

我正在查看msdn按键以及他们如何使用开关,我会使用类似的东西来检查按下的键,如果按下了正确的键,计数器将增加正确的按键,直到"测试",将达到4,如果按下的键不正确,重置计数器并重新开始,直到输入正确的键顺序。

我希望这是有意义的:p

public partial class TrollFrm : Form
{
    int number = 1; //change to 2 and have the first instance of troll count = number - 1
    System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
    public TrollFrm()
    {
        InitializeComponent();
        this.Text = "Trololol - Troll Count: " + number;
        startTimer();
    }
    private void TrollFrm_Load(object sender, EventArgs e)
    {
       //this.Enabled = false;
    }
    private void TrollFrm_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = true;
    }
    public void startTimer()
    {
        myTimer.Tick += new EventHandler(createForm);
        //myTimer.Interval = 500;
        myTimer.Start();
    }
    public void createForm(Object myObject, EventArgs myEventArgs)
    {
        Form frm = new TrollChildFrm();
        Random randomX = new Random();
        Random randomY = new Random();
        frm.Text = "Trololol - Troll Count: " + number;
        int xValue;
        int yValue;
        number++;
        if (number % 2 == 0)    //number is even.
        {
            xValue = (Convert.ToInt32(randomX.Next(1, 1920))) + 200;
            yValue = (Convert.ToInt32(randomY.Next(1, 1080))) - 200;
        }
        else    //number is not even.
        {
            xValue = (Convert.ToInt32(randomX.Next(1, 1920))) - 200;
            yValue = (Convert.ToInt32(randomY.Next(1, 1080))) + 200;
        }
        frm.Show();
        frm.Location = new Point(xValue, yValue);
        if (number == 20)
        {
            myTimer.Stop();
        }
    }

这是一个可以用于您描述的场景的实现(虽然没有测试):

int exitKeysCount = 0;
private void TrollFrm_KeyDown(object sender, KeyEventArgs e)
{
    if (exitKeysCount == 0 && e.KeyCode == Keys.T)
        exitKeysCount = 1;
    else if (exitKeysCount == 1 && e.KeyCode == Keys.E)
        exitKeysCount = 2;
    else if (exitKeysCount == 2 && e.KeyCode == Keys.S)
        exitKeysCount = 3;
    else if (exitKeysCount == 3 && e.KeyCode == Keys.T)
        this.Close();
    else exitKeysCount = 0;
}

我假设TrollFrm是您的父窗体,如果它们都在其他地方调用,请将this.Close()替换为主程序函数中的一些函数,也TrollFrm需要在按键期间集中。

在您的父窗体上尝试此父窗体。

 int trollCount = 0;
 private void TrollFrm_KeyDown(object sender, KeyEventHandler e)
 {
     if (trollCount == 0 && e.KeyCode == Keys.T)
       {
            trollCount = 1;
            frm.Text = "Trololol - Troll Count:" + trollCount
       }
     else if (trollCount == 1 && e.KeyCode== Keys.E)
       {
            trollCount = 2;
            frm.Text = "Trololol - Troll Count:" + trollCount
       }
     else if (trollCount == 2 && e.KeyCode== Keys.S)
       {
            trollCount = 3;
            frm.Text = "Trololol - Troll Count:" + trollCount
       }
     else if (trollCount == 4 && e.KeyCode== Keys.T)
       {
            trollCount = 4;
            this.Close();
       }
     else
        trollCount = 0;

还需要什么就告诉我。

相关内容

  • 没有找到相关文章

最新更新