C#窗口表单应用程序 [英] C# window form application

查看:91
本文介绍了C#窗口表单应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从另一种方法调用click方法

可以吗?



i想要在第一级点击所有方法方法事件发生并在那里停留

解决方案

你可以 - 它毕竟只是一种方法 - 但这不是一个好主意。

创建一个新方法来完成工作并从事件处理程序和其他位置调用它是一个非常好的想法。


假设这是Windows窗体:



控件继承自Control / ButtonBase,就像一个Button,提供了一种在代码中触发Click事件的方法:'PerformClick();



对于其他控制,取决于控制,你想做什么,可以使用不同的技术。



在一次使用中 - 你可能想要在Control上模拟一个实际的物理鼠标点击,这通常会使Focus成为Control;在另一个用例中,也许你的目标只是执行Click EventHandler中的代码。或者,也许,你想要两种行为?



你通常可以通过简单地用虚拟参数参数调用它来调用调用Click EventHandler的代码:< pre lang =cs> private void SomeMethod(...一些参数,,,)
{
SomeControl_Click( null null );
}

开始编辑:

正如OriginalGriff在他的回复中所说,如果你打算在EventHandler中使用这些代码从EventHandler之外,最好把这些代码放在一个自己的方法中,EventHandler和你的外部代码都可以调用它。



如果你真的希望在屏幕上的任何地方模拟物理鼠标点击,你必须使用Win API:这里有良好的源代码:[ ^ ]。如果您是C#和.NET的初学者,我建议您在使用基于API的代码之前等到更高级。

结束编辑



如果您举一个具体的例子说明您要做什么,以及涉及哪些控件,您可以得到更具体的答案。


how to call to click method from another method
can is ir possible?

i want to call all method in class one click method event occurs and stay alwayes there

解决方案

You can - it's just a method after all - but it's not considered a good idea.
It's a much, much better idea to create a new method which does the work and call that from your event handler and the other location(s) instead.


Assuming this is Windows Forms:

Controls inheriting from Control/ButtonBase, like a Button, provide a method for triggering a Click Event in code: 'PerformClick();

For other Controls, depending on the Control, and what you want to do, different techniques can be used.

In one use-case you may want to simulate an actual physical mouse-click on a Control, which would, normally, give Focus to the Control; in another use-case, perhaps your goal is simply execute to the code in the Click EventHandler. Or, perhaps, you want both behaviors ?

You can usually "get away with" invoking a Click EventHandler's code by simply calling it with "dummy" parameter arguments:

private void SomeMethod( ... some parameters , , ,)
{
     SomeControl_Click(null, null);
}

Begin Edit:
As OriginalGriff notes in his response, if you are going to make use of the code in an EventHandler from outside the EventHandler, it is much better to take that code and put it in a method of its own which both the EventHandler and your "external" code can call.

If you really wish to simulate a physical mouse-click anywhere on the screen, you must use the Win API: good source code here: [^]. If you are a beginner in C# and .NET, I'd suggest you wait until you are more advanced before using API based code.
End Edit

If you give a specific example of what you are trying to do, and what Controls are involved, you can get a more specific answer.


这篇关于C#窗口表单应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆