gpt4 book ai didi

c# - 防止 "Backspace"键被按下

转载 作者:行者123 更新时间:2023-12-02 15:16:31 26 4
gpt4 key购买 nike

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

最佳答案

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

该标志只是帮助处理程序知道何时按住该键以及何时再次允许该键。

关于c# - 防止 "Backspace"键被按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17219434/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com