C#On keypress - 退出程序

时间:2011-11-24 06:21:03

标签: c# winforms keypress exit

我的程序在整个屏幕上打开一系列表单,我能在转码方法中编码,所以在键入“test”这个单词时,程序会关闭吗?

我正在查看msdn按键以及它们如何使用开关,我是否会使用类似的东西来检查按下的键,如果按下了正确的按键,计数器将按正确的按键增加,直到“test” “,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();
        }
    }

2 个答案:

答案 0 :(得分:1)

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

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也需要关注。

答案 1 :(得分:0)

在父表单上尝试此父级。

 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;

告诉我你是否需要其他任何东西。