保持“Backspace”键不被按下

时间:2013-06-20 16:55:41

标签: c#

我正在制作类似于“彼得答案”的东西。它被称为“Adrian Answers”,因为这是我的名字。但这无关紧要。这个问题之前已经得到了回答,但我无法弄清楚如何将这个应用到我的情况中。我需要退格键才能无法按下。如果你按住它,它应该只在程序中注册一次,但不能在此之后的任何时间注册。顺便说一句,我想在textBox1中使用此功能。这是Peter Answers的参考。 http://www.peteranswers.com/

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 PeterAnswers
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool secret = false;
        string answer;
        string normal = "Adrian, please answer my question:";
        int i = 0;
        bool secret2 = false;

        private void textBox1_KeyPress(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.OemPeriod && textBox1.Text.Length == 0)
            {
                i = 0;
                e.SuppressKeyPress = true;
                secret = true;
                textBox1.Text += normal[i];
                textBox1.Select(textBox1.Text.Length, 0);
                i++;
                answer = null;
            }
            else if (e.KeyData == Keys.OemPeriod && secret == true)
            {
                e.SuppressKeyPress = true;
                textBox1.Text += normal[i];
                secret = false;
                textBox1.Select(textBox1.Text.Length, 0);
                secret2 = true;
            }
            else if(e.KeyData != Keys.OemPeriod && secret == true && e.KeyData != Keys.Back && Control.ModifierKeys != Keys.Shift  && e.KeyData != Keys.Space)
            {
                e.SuppressKeyPress = true;
                answer += e.KeyData;
                textBox1.Text += normal[i];
                textBox1.Select(textBox1.Text.Length, 0);
                i++;
            }
            else if (e.KeyData == Keys.Back && secret == true)
            {
                string petition = textBox1.Text;
                if (petition.Length != 0)
                {
                    if (petition.Length > 1)
                    {
                        petition = petition.Remove(petition.Length - 1);
                        answer = answer.Remove(answer.Length - 1);
                        i--;
                    }
                    else if (petition.Length == 1)
                    {
                        petition = petition.Remove(petition.Length - 1);
                        i--;
                        secret = false;
                        secret2 = false;
                        answer = null;
                    }
                    else if (answer.Length > 0)
                    {
                        answer = answer.Remove(answer.Length - 1);
                    }
                    else if (answer.Length <= 0)
                    {
                         answer = null;
                    }
                }
            }
            else if (e.KeyData == Keys.Space && secret == true)
            {
                e.SuppressKeyPress = true;
                answer += " ";
                textBox1.Text += normal[i];
                textBox1.Select(textBox1.Text.Length, 0);
                i++;
            }
            else if (Control.ModifierKeys == Keys.Shift && secret == true)
            {
                e.SuppressKeyPress = true;
                textBox1.Select(textBox1.Text.Length, 0);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (answer != null && secret2 == true)
            {
            answerLabel.Visible = true;
            answer = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(answer.ToLower());
                answerLabel.Text += " " + answer;
            }
            else 
            {
                Random rand = new Random();
                switch(rand.Next(0, 4))
                {
                    case 1:
                        answerLabel.Visible = true;
                        answerLabel.Text += " Sorry, cannot compute answer at the moment.  Please try again later.";
                        break;
                    case 2:
                        answerLabel.Visible = true;
                        answerLabel.Text += " Something seems to be blocking my mental powers...";
                        break;
                    case 3:
                        answerLabel.Visible = true;
                        answerLabel.Text += " No answer.";
                        break;
                    case 4:
                        answerLabel.Visible = true;
                        answerLabel.Text += " I find your lack of faith disturbing...";
                        break;
                }
            }
            secret = false;
            secret2 = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            i = 0;
            answerLabel.Visible = false;
            textBox1.Clear();
            textBox2.Clear();
            answer = null;
            secret = false;
            secret2 = false;
            answerLabel.Text = "Answer:";
        }
    }
}

2 个答案:

答案 0 :(得分:5)

使用KeyDownKeyUpBoolean标记的组合:

private Boolean _backspace = false;

private void textBox1_KeyDown(Object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Back)
    {
        e.SuppressKeyPress = _backspace;
        e.Handled = _backspace;
        _backspace = true;
    }
}

private void textBox1_KeyUp(Object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Back)
        _backspace = false;
}

该标志只是帮助处理程序知道密钥何时被保持以及何时再次允许它。

答案 1 :(得分:1)

Don的答案应该可以正常工作(我赞成它)。作为其答案的扩展,您还可以将其退格逻辑抽象为新的TextBox控件:

public class EnhancedTextBox : TextBox
{
    private Boolean _backspace = false;

    public EnhancedTextBox() 
    {
        KeyDown += new KeyEventHandler(EnhancedTextBox_KeyDown);
        KeyUp += new KeyEventHandler(EnhancedTextBox_KeyUp);
    }

    void EnhancedTextBox_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Back)
            _backspace = false;
    }

    void EnhancedTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Back)
        {
            e.SuppressKeyPress = _backspace;
            e.Handled = _backspace;
            _backspace = true;
        }
    }
}

所以,你要用它替换textBox1

private void InitializeComponent()
{
    this.textBox1 = new EnhancedTextBox();
    ...
}

然后将现有逻辑从KeyPress移至KeyUp

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyData == Keys.OemPeriod && textBox1.Text.Length == 0)
    {
        i = 0;
        e.SuppressKeyPress = true;
        secret = true;
        textBox1.Text += normal[i];
        textBox1.Select(textBox1.Text.Length, 0);
        i++;
        answer = null;
    }
    else if (e.KeyData == Keys.OemPeriod && secret == true)
    {
        e.SuppressKeyPress = true;
        textBox1.Text += normal[i];
        secret = false;
        textBox1.Select(textBox1.Text.Length, 0);
        secret2 = true;
    }
    else if(e.KeyData != Keys.OemPeriod && secret == true && e.KeyData != Keys.Back && Control.ModifierKeys != Keys.Shift  && e.KeyData != Keys.Space)
    {
        e.SuppressKeyPress = true;
        answer += e.KeyData;
        textBox1.Text += normal[i];
        textBox1.Select(textBox1.Text.Length, 0);
        i++;
    }
    else if (e.KeyData == Keys.Back && secret == true)
    {
        string petition = textBox1.Text;
        if (petition.Length != 0)
        {
            if (petition.Length > 1)
            {
                petition = petition.Remove(petition.Length - 1);
                answer = answer.Remove(answer.Length - 1);
                i--;
            }
            else if (petition.Length == 1)
            {
                petition = petition.Remove(petition.Length - 1);
                i--;
                secret = false;
                secret2 = false;
                answer = null;
            }
            else if (answer.Length > 0)
            {
                answer = answer.Remove(answer.Length - 1);
            }
            else if (answer.Length <= 0)
            {
                answer = null;
            }
        }
    }
    else if (e.KeyData == Keys.Space && secret == true)
    {
        e.SuppressKeyPress = true;
        answer += " ";
        textBox1.Text += normal[i];
        textBox1.Select(textBox1.Text.Length, 0);
        i++;
    }
    else if (Control.ModifierKeys == Keys.Shift && secret == true)
    {
        e.SuppressKeyPress = true;
        textBox1.Select(textBox1.Text.Length, 0);
    }
}

将其移至KeyUp将阻止内存中的字符串在保留退格时被连续删除,正如您在Don的回答中的评论中所怀疑的那样。