我正在制作类似于“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:";
}
}
}