Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 你用退格删除了哪个号码_C#_.net_Winforms - Fatal编程技术网

C# 你用退格删除了哪个号码

C# 你用退格删除了哪个号码,c#,.net,winforms,C#,.net,Winforms,当我用backspace删除文本框中的数字时,我想保存该数字,以便与其他文本框中的其他数字进行比较。我该怎么做? 这是我想放进去的代码: List<TextBox> box1; private void Form1_Load(object sender, EventArgs e) { box1 = this.Controls.OfType<TextBox>() .Where(x => x.Name.StartsWith("te

当我用backspace删除文本框中的数字时,我想保存该数字,以便与其他文本框中的其他数字进行比较。我该怎么做? 这是我想放进去的代码:

List<TextBox> box1;
private void Form1_Load(object sender, EventArgs e)
    {   box1 = this.Controls.OfType<TextBox>()
              .Where(x => x.Name.StartsWith("textBox1")).ToList();

        foreach (TextBox t in box1)
            t.TextChanged += textBox_TC1;
     }

private void textBox_TC1(object sender, EventArgs e)
    {
        TextBox textBox = (TextBox)sender;
        if (textBox.Text.Length == 1 && allwhite == 0)
        {
            bool sameText = box1.Any(x => x.Text == textBox.Text &&
                                         !x.Equals(textBox));

            if (sameText)
                textBox.BackColor = System.Drawing.Color.Red;

        }
        else if (textBox.Text.Length == 0)
        {
            textBox.BackColor = System.Drawing.Color.White;
        }
    }
列表框1;
私有void Form1\u加载(对象发送方、事件参数e)
{box1=this.Controls.OfType()
.Where(x=>x.Name.StartsWith(“textBox1”).ToList();
foreach(框1中的文本框t)
t、 TextChanged+=textBox_TC1;
}
私有无效文本框_TC1(对象发送方,事件参数e)
{
TextBox TextBox=(TextBox)发送方;
if(textBox.Text.Length==1&&allwhite==0)
{
bool sameText=box1.Any(x=>x.Text==textBox.Text&&
!x.Equals(文本框));
if(sameText)
textBox.BackColor=System.Drawing.Color.Red;
}
else if(textBox.Text.Length==0)
{
textBox.BackColor=System.Drawing.Color.White;
}
}
我想将我的新代码放在“else if(textBox.Text.Length==0)”中,因为我只能删除文本框中带退格的文本,并且最大长度为1。
当我退格某个内容时,我想将该数字与box1中的所有其他文本框进行比较,然后如果该数字仅等于其他一个文本框,则该数字将使其他文本框的背景色变为白色。我不知道如何保存即将删除的号码,因此如果您能帮助我,我将非常高兴。

您可以使用以下方法:

static void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    TextBox t = sender as TextBox;
    switch (e.KeyCode)
    {
        case Keys.Delete:
        case Keys.Back:
            int start = e.KeyCode == Keys.Back && t.SelectionLength == 0 ? t.SelectionStart - 1 : t.SelectionStart;
            int length = t.SelectionLength == 0 ? 1 : t.SelectionLength;

            // you can save your char right here....!

            t.Text = t.Text.Remove(start, length);

            e.SuppressKeyPress = true;
            break;
    }
}

您应该使用
TextChanged
事件来检测
TextBox
上的更改,并且在
TextChanged
的末尾,您应该将当前值保留在
TextBox
属性的某个位置,并在希望将其与其他值进行比较时使用它。您不应使用除
TextChanged
以外的任何事件,因为用户可以在不使用键盘的情况下删除或粘贴值

例如,您可以编写如下代码:

...
else if (textBox.Text.Length == 0)
{
    var previusText = textBox.Tag as string;
    var items= box1.Where(x => x.Text == previusText && !x.Equals(textBox)).ToList();
    if (items.Count()==1)
    {
        items[0].BackColor = System.Drawing.Color.White;
    }
}

//Keep previous text
textBox.Tag = textBox.Text;
...

用户可能不使用backspace或delete来删除字符,而只选择文本并键入/粘贴新字符。解决方案必须处理所有情况。您可以存储以前的值(作为一个字段),当出现
TextChanged
时,您可以获取以前的值,检查它是否少了一个数字,等等。或者您可以使用另一个事件(例如
KeyDown
)要检测按下退格键的时刻。您是否正在尝试比较密码或其他内容?您应该使用
TextChanged
事件,并且在
TextChanged
结束时,您应该将
TextBox
属性的当前值保留在类似
Tag
的位置,并在希望将其与其他值进行比较时使用它。您不应使用除
TextChanged
以外的任何事件,因为用户可以不使用键盘删除或粘贴值。@Sinatr我不仅有1个文本框,还有81个在列表中,所以,如果我要存储特定文本框的数字,我需要一些东西来检查这些文本框在什么列表中,因为我所有的文本框都在两个不同的列表中,我有18个列表。“我不知道该怎么做。”RezaAghaei哦,我没有抓住这一点。无论如何,谢谢。这段代码只会使方框变为白色,而不是与之相等的方框。我已经修复了没有键盘或使用ctrl+a或ctrl+z不能删除或粘贴值的问题,因此您不必担心这个问题。@Percutient这是主要思想,您可以将其更改为您的意愿:)我不会阅读您的所有代码。如我所见,您知道如何使用linq很好地查找控件,因此,只需更改代码并使其符合您的意愿。@Percutient但您得到的主要思想是使用
TextChanged
事件并将以前的值保留在
Tag
@Percutient中,现在您可以检查编辑的答案:)我知道您的代码正试图将以前的文本放入textBox.Tag中。但是当您放置'x.Text==previusText'时,x.Text已经为空,因此previusText也是一个空字符串,因为当textBox.Text.Length为0时,所有这些都会发生。至少在我看来是这样的,我想不出任何办法来解决这个问题。