C# 如何自动单击表单应用程序上的按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17878730/
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
How to Automatically Click a button On a form application?
提问by akd
I have a working form application that has a button on it. I have the source code too. So now I am trying to run the application from schedule tasks on windows and I would like to change the code the to the click button action when the application runs. The problem is the form should be loaded first and then the click action should be fired. Do I need a timer to this? if so could you please give me a help with this? So it takes about 3-4 seconds to load the form application. so it should fire the click action after 5 seconds
我有一个工作表单应用程序,上面有一个按钮。我也有源代码。所以现在我试图从 Windows 上的计划任务运行应用程序,我想在应用程序运行时将代码更改为单击按钮操作。问题是应该首先加载表单,然后应该触发点击动作。我需要一个计时器吗?如果是这样,你能帮我吗?所以加载表单应用大约需要 3-4 秒。所以它应该在 5 秒后触发点击动作
采纳答案by Sayse
The Form.Shownevent will fire when the form is shown to the user for the first time, you can enter your event into this
该Form.Shown当表单显示给用户的第一次事件将触发,您可以输入您的事件到这个
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
this.BtnClick(null, null);
}
回答by Adil
You can call button click
event using Button.PerformClickon Form.Shownevent to ensure form is loaded.
您可以在Form.Shown事件上click
使用Button.PerformClick调用按钮事件以确保加载表单。
private void Form1_Shown(Object sender, EventArgs e)
{
Button1.PerformClick();
}
The Shown event is only raised the first time a form is displayed; subsequently minimizing, maximizing, restoring, hiding, showing, or invalidating and repainting will not raise this event, Reference
Shown 事件仅在第一次显示表单时引发;随后最小化、最大化、恢复、隐藏、显示或无效和重新绘制不会引发此事件,参考
回答by Md. Parvez Alam
you call call button click like this
你打电话给按钮点击这样
Button_Click(sender,e)
Button_Click(sender,e)
回答by sam
buttonName.PerformClick();
Call the button using PerformClick method
buttonName.PerformClick();
使用 PerformClick 方法调用按钮