简体   繁体   English

如何知道何时在C#中单击按钮

[英]How to know when a button is clicked in C#

Pls I need your help in some cases about button in C#. 请在某些情况下需要有关C#中按钮的帮助。 How can I know when a particular button is clicked. 我如何知道何时单击特定按钮。 I want to use the if condition statement to write the event. 我想使用if条件语句编写事件。 So I want it to do something when particular button is clicked. 因此,我希望它在单击特定按钮时能够执行某些操作。 I want to put all the code inside one function or class then I can call it anytime. 我想将所有代码放在一个函数或类中,然后我可以随时调用它。 For example 例如

private void showPanel()  
{  
    if (dashPanelButton.Clicked == true)  
    {  
        dashPanel.Visible = true;  
    }  
    else if(studInfoBtn.Clicked == true)  
    {  
        studInfoPanel.Visible = true;  
    }  
    else  
    {  
        homePanel.Visible = true; 
    }
}

Note the above code is just an assumption not really a working code. 注意上面的代码只是一个假设,并不是真正的工作代码。 Just using it to explain myself 只是用它来解释自己

You don't "check if a button is clicked". 您不会“检查是否单击了按钮”。 The code isn't going to just sit around and wait for that click to happen. 代码不会只是闲坐着等待点击发生。 Instead, you "respond to a button click" with a click event handler: 而是使用单击事件处理程序来“响应按钮单击”:

void myButton_Click(Object sender, EventArgs e)
{
    // do something when the button is clicked
}

You can attach the handler to the button in the designer, or in code: 您可以将处理程序附加到设计器中的按钮或代码中:

myButton.Click += new EventHandler(myButton_Click);

Now, if you want the same handler to be used for multiple buttons, that's where that Object sender becomes useful. 现在,如果您希望将同一处理程序用于多个按钮,则该Object sender将变得有用。 That's a reference to the object which raised the event. 这是引发事件的对象的引用。 So in your case, it would be the button which was clicked: 因此,在您的情况下,将是单击的按钮:

void myButton_Click(Object sender, EventArgs e)
{
    var theButton = (Button)sender;
    // now "theButton" is the button which was clicked
}

Register the button Click event and do whatever you want in the event handler like 注册按钮Click事件并在事件处理程序中执行所需的操作

dashPanelButton.Click += new EventHandler(myhandler);

protected void myhandler(object sender, EventArgs e)
{
  //do whatever you want
}

You can do what you want, like this: 您可以执行所需的操作,如下所示:

private void showPanel(Object sender, EventArgs e)
    {
        if (sender == dashPanelButton)
        {
            dashPanel.Visible = true;
        }
        else if (sender == studInfoBtn)
        {
            studInfoBtn.Visible = true;
        }
        else
        {
            homePanel.Visible = true;
        }
    }

make sure your button is wired correctly 确保您的按钮接线正确

dashPanelButton.Click += new EventHandler(showPanel);
studInfoBtn.Click += new EventHandler(showPanel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道何时在form1中单击了按钮但在form2中知道C# - How to know when a button is clicked in form1 but know in form2 C# 如何知道在Winforms C#中单击了哪个按钮? - How to know which button was clicked in winforms c#? C# 尝试关闭未保存的Word文档时,如何知道保存提示对话框中的取消按钮是否被点击 - How to know whether cancel button is clicked in Save Prompt dialog box , when we try to close Unsaved Word document in C# 如何知道在使用C#编程创建的WPF按钮中单击了哪个按钮 - How to know which button is clicked in wpf button created programatically using c# 如何知道C#中最后单击的文本框? - How to know the last clicked textbox in C#? c# 单击按钮时如何打开 combobox - c# How do I open the combobox when the button is clicked 在 wpf C# 中单击按钮时如何禁用其他功能 - how to disable the other functions when Button clicked in wpf C# 单击C#按钮时TinyMCE中没有值 - No value in TinyMCE when C# button is clicked C# - 单击另一个按钮时创建一个按钮 - C# - Creating a button when another button is clicked 我在datagridview中添加了更新按钮,当在C#中单击按钮时,如何更改文本? - i have added update button inside datagridview how to change text on it when button is clicked in c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM