如何根据两个标准以两种不同的方式触发同一事件? (C#)

发布于 2024-10-29 12:10:12 字数 885 浏览 3 评论 0原文

我提出这个问题可能用词有误。这是一个棘手的提议,我迫切需要解决方案:-(

我想以两种不同的方式触发 textBox1_KeyDown,但根据某些标准按下相同的键。下面的代码会更加清晰。

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (textBox1.Text == "")
        {
            if (e.KeyCode == Keys.Enter)
            {
                textBox1.Text = "x";
            } 
        }


        if (textBox1.Text != "")
        {
            if (e.KeyCode == Keys.Enter)
            {
                 textBox1.Text = "y";
            }
        }
    }

我想做的是,当我按 textBox1 上的 Enter 按钮时,如果其中没有文本,我希望它显示“x”,如果其中有一些文本,那么我希望文本框在按下时显示“y”。当我按照上面的方式进行编码时,这两个过程都会在一个实例中发生,也就是说,当我在 textBox1 为空时按 Enter,它会显示“y”(应该是“x”)。这意味着它首先显示“x”,然后由于文本框中有一个数量,因此文本变为“y”,如我的代码所调用的那样?就像我希望文本框仅显示“x”一样。 " 当它为空时,我按 Enter 键,或者当它不空白时,我按 Enter 键时,它应该只显示“y”。

我一定是错过了一些愚蠢的东西..谢谢..请给我代码。我几乎不懂技术术语..

I might have got the words wrong to put forward this question. This is a tricky proposition and I want solution urgently :-(

I want to trigger textBox1_KeyDown in two different ways, but by keying down the same key based on some criteria. The code below would give more clarity.

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (textBox1.Text == "")
        {
            if (e.KeyCode == Keys.Enter)
            {
                textBox1.Text = "x";
            } 
        }


        if (textBox1.Text != "")
        {
            if (e.KeyCode == Keys.Enter)
            {
                 textBox1.Text = "y";
            }
        }
    }

What I'm trying to do is when I press Enter button on textBox1, I want it to display "x" if there's no text in it. If there's some text in it, then I want the textbox to display "y" upon pressing Enter. When I do the way I coded above, both the procedures happen in one instance. That's, when I press Enter when the textBox1 is blank it displays "y" (which should be "x"). That means it first displays "x" and then since the textbox has a quantity in it, the text becomes "y" as called by my code. How to Separate the two functions?? Like I want the textbox to show only "x" when its blank and I press Enter or it should show only "y" when its not blank and I press Enter.

I must be missing something silly.. Thanks.. Kindly gimme the code. I hardly understands technical terms..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光无声 2024-11-05 12:10:12

这是因为两个 if 语句都被执行了。第一个 if 语句执行并使文本框中的文本不为空。这也会导致下一个 if 语句被触发。只需这样做就可以修复它:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (textBox1.Text == "")
    {
        if (e.KeyCode == Keys.Enter)
        {
            textBox1.Text = "x";
        } 
    }


    else if (textBox1.Text != "")
    {
        if (e.KeyCode == Keys.Enter)
        {
             textBox1.Text = "y";
        }
    }
}

注意在第二个 if 语句上添加“else”。

That is because both if statements get executed. The first if statement executes and makes the text in the textbox not blank. This causes the next if statement to fire too. Simply doing this should fix it:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (textBox1.Text == "")
    {
        if (e.KeyCode == Keys.Enter)
        {
            textBox1.Text = "x";
        } 
    }


    else if (textBox1.Text != "")
    {
        if (e.KeyCode == Keys.Enter)
        {
             textBox1.Text = "y";
        }
    }
}

note addition of "else" on second if statement.

余生一个溪 2024-11-05 12:10:12

首先,您可以将 return; 放在最深嵌套的 if 语句中,这样下一个 if 就不会被执行。

您可以做的另一件事是颠倒条件的顺序,因此您可以在处理程序的顶部而不是底部测试 if (textBox1.Text != "")

最后,您可以在两个条件之间使用 else

Well for a start you could put return; in your deepest-nested if statements so that the next if doesn't get executed.

The other thing you can do is reverse the order of your conditions, so you test for if (textBox1.Text != "") at the top of your handler, rather than the bottom.

And finally, you could just use else between the two conditionals.

桃气十足 2024-11-05 12:10:12

根据您在那里编写的内容,您可以翻转 if 语句的顺序或将第二个块设为 else 或 else if。

Based upon what you have written there you could just flip the order of your if statements or make the second block an else or else if.

亣腦蒛氧 2024-11-05 12:10:12

也许我遗漏了一些东西,但为什么不添加一个 else:

private void textBox1_KeyDown(object sender, KeyEventArgs e)    
{
    if (textBox1.Text == "")         
    {
        if (e.KeyCode == Keys.Enter)
        {
            textBox1.Text = "x";
        }
    }

    else if (textBox1.Text != "")
    {
        if (e.KeyCode == Keys.Enter)
        {
            textBox1.Text = "y";
        }
    }
} 

Maybe I'm missing something, but why not just add an else:

private void textBox1_KeyDown(object sender, KeyEventArgs e)    
{
    if (textBox1.Text == "")         
    {
        if (e.KeyCode == Keys.Enter)
        {
            textBox1.Text = "x";
        }
    }

    else if (textBox1.Text != "")
    {
        if (e.KeyCode == Keys.Enter)
        {
            textBox1.Text = "y";
        }
    }
} 
无所的.畏惧 2024-11-05 12:10:12

您正在寻找类似的东西,所以我相信:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    //determine if text is empty or otherwise equal to 'x'...
    if (textBox1.Text == string.Empty || textBox1.Text != "x")
    {
        //confirmed, set to 'x'...
        textBox1.Text = "x";
    }
    else //and a catch-all for y
    {
        //text wasn't empty or 'x', set to 'y'...
        textBox1.Text = "y";
    }
}

您也可以使用 三元运算符

//get a copy of the text
var value = textBox1.Text;
//set textbox value to 'x' if not empty or equal to 'x', otherwise 'y'
textBox1.Text = value == string.Empty || value != "x" ? "x" : "y";

You're looking for something like this, so I believe:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    //determine if text is empty or otherwise equal to 'x'...
    if (textBox1.Text == string.Empty || textBox1.Text != "x")
    {
        //confirmed, set to 'x'...
        textBox1.Text = "x";
    }
    else //and a catch-all for y
    {
        //text wasn't empty or 'x', set to 'y'...
        textBox1.Text = "y";
    }
}

You could also achieve this in a shorthanded fashion using the ternary operator:

//get a copy of the text
var value = textBox1.Text;
//set textbox value to 'x' if not empty or equal to 'x', otherwise 'y'
textBox1.Text = value == string.Empty || value != "x" ? "x" : "y";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文