2

我正在制作类似于“Peter Answers”的东西。它被称为“Adrian Answers”,因为那是我的名字。但这无关紧要。之前已经回答了这个问题,但我不知道如何将其应用于我的情况。我需要退格键不能被按住。如果你按住它,它应该只在程序中注册一次,但此后不再注册。顺便说一句,我想要 textBox1 中的这个功能。这是彼得的答案供参考。 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:";
        }
    }
}
4

2 回答 2

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;
}

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

于 2013-06-20T17:05:01.953 回答
1

唐的回答应该可以正常工作(我赞成)。作为他答案的扩展,您还可以将他的退格逻辑抽象为一个新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();
    ...
}

然后将您现有的逻辑KeyPressKeyUp

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);
    }
}

正如您在 Don's answer 下的评论中所怀疑的那样,将其移动到KeyUp将防止内存中的字符串在保持退格时被连续删除。

于 2013-06-20T20:10:07.730 回答