C# 检查按钮是否被点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16553768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:19:49  来源:igfitidea点击:

C# Checking if button was clicked

c#buttonclick

提问by ItsLuckies

I am making a program that should just continue if 2 conditions are given.

我正在制作一个程序,如果给出 2 个条件,它应该继续。

The first one, 2 TextBoxs have the same word in and a Buttonwas clicked, which opens a new Form. Now I have the event for the "complete" button.

第一个, 2 TextBoxs 有相同的词 in 并且 aButton被点击,这会打开一个新的Form. 现在我有了“完成”按钮的事件。

private void button2_Click(object sender, EventArgs e)
{
    if (textBox2.Text == textBox3.Text && ???) 
    {    
        StreamWriter myWriter = File.CreateText(@"c:\Program Files\text.txt");
        myWriter.WriteLine(textBox1.Text);
        myWriter.WriteLine(textBox2.Text);
     }
]

My problem is, I can't find a method that gives something like `button1.Clicked or something similar.

我的问题是,我找不到提供类似 `button1.Clicked 或类似内容的方法。

I hope someone can help me here..

我希望有人能在这里帮助我..

采纳答案by Jan Doerrenhaus

Clickis an event that fires immediately after you release the mouse button. So if you want to check in the handler for button2.Clickif button1was clicked before, all you could do is have a handler for button1.Clickwhich sets a bool flag of your own making to true.

Click是在您松开鼠标按钮后立即触发的事件。因此,如果您想检查之前button2.Click是否button1单击过的处理程序,您所能做的就是拥有一个处理程序,button1.Click该处理程序将您自己制作的 bool 标志设置为 true。

private bool button1WasClicked = false;

private void button1_Click(object sender, EventArgs e)
{
    button1WasClicked = true;
}

private void button2_Click(object sender, EventArgs e)
{
    if (textBox2.Text == textBox3.Text && button1WasClicked)
    { 
        StreamWriter myWriter = File.CreateText(@"c:\Program Files\text.txt");
        myWriter.WriteLine(textBox1.Text);
        myWriter.WriteLine(textBox2.Text);
        button1WasClicked = false;
    }
}

回答by Safi

These helped me a lot: I wanted to save values from my gridview, and it was reloading my gridview /overriding my new values, as i have IsPostBack inside my PageLoad.

这些对我有很大帮助:我想从我的 gridview 中保存值,并且它正在重新加载我的 gridview/覆盖我的新值,因为我的 PageLoad 中有 IsPostBack。

if (HttpContext.Current.Request["MYCLICKEDBUTTONID"] == null)
{
   //Do not reload the gridview.

}
else
{
   reload my gridview.
}

SOURCE: http://bytes.com/topic/asp-net/answers/312809-please-help-how-identify-button-clicked

来源:http: //bytes.com/topic/asp-net/answers/312809-please-help-how-identify-button-clicked

回答by Muhammad Umair

button1, button2 and button3 have same even handler

button1、button2 和 button3 具有相同的偶数处理程序

private void button1_Click(Object sender, EventArgs e)
    {
        Button btnSender = (Button)sender;
        if (btnSender == button1 || btnSender == button2)
        {
            //some code here
        }
        else if (btnSender == button3)
            //some code here
    }

回答by Akashdeep Lama Jyrwa

i am very new to this website. I am an undergraduate student, doing my Bachelor Of Computer Application. I am doing a simple program in Visual Studio using C# and I came across the same problem, how to check whether a button is clicked? I wanted to do this,

我对这个网站很陌生。我是一名本科生,正在攻读计算机应用学士学位。我正在使用 C# 在 Visual Studio 中做一个简单的程序,我遇到了同样的问题,如何检查按钮是否被点击?我想做这个,

if(-button1 is clicked-) then
{
this should happen;
}
if(-button2 is clicked-) then
{
this should happen;
}

I didn't know what to do, so I tried searching for the solution in the internet. I got many solutions which didn't help me. So, I tried something on my own and did this,

我不知道该怎么办,所以我尝试在互联网上搜索解决方案。我得到了许多对我没有帮助的解决方案。所以,我自己尝试了一些东西并做到了这一点,

int i;
private void button1_Click(object sender, EventArgs e)
        {
            i = 1;
            label3.Text = "Principle";
            label4.Text = "Rate";
            label5.Text = "Time";
            label6.Text = "Simple Interest";
        }


private void button2_Click(object sender, EventArgs e)
        {
            i = 2;
            label3.Text = "SI";
            label4.Text = "Rate";
            label5.Text = "Time";
            label6.Text = "Principle";
        }
private void button5_Click(object sender, EventArgs e)
        {

            try
            {
                if (i == 1)
                {
                    si = (Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox2.Text) * Convert.ToInt32(textBox3.Text)) / 100;
                    textBox4.Text = Convert.ToString(si);
                }
                if (i == 2)
                {
                    p = (Convert.ToInt32(textBox1.Text) * 100) / (Convert.ToInt32(textBox2.Text) * Convert.ToInt32(textBox3.Text));
                    textBox4.Text = Convert.ToString(p);
                }

I declared a variable "i" and assigned it with different values in different buttons and checked the value of i in the if function. It worked. Give your suggestions if any. Thank you.

我声明了一个变量“i”,并在不同的按钮中为它分配了不同的值,并在 if 函数中检查了 i 的值。有效。如果有的话,请提出您的建议。谢谢你。