清除事件中参数的含义 [英] Clear the meaning of parameters on an events

查看:144
本文介绍了清除事件中参数的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#Windows应用程序中更新鲜.
我看到当我在click/double_click事件等上编写代码时,我的代码看起来像
如下

I am fresher in C# windows application.
I see that when i write code on click/double_click events etc. then my code look like
as follows

private void Button_Click(object sender, EventArgs e)
      {
        // coding
      }


我想要对我要调用的button_Click事件而不是再次进行编码的已定义函数/过程进行相同的编码.

但是我不知道对象发送者和EventArgs e
的含义
我们如何以编程方式调用Button_Cick

帮帮我.


I want same codeing on my defined functions/procedures for which I want to call button_Click event instead of coding again.

But I don''t know the meaning of object sender and EventArgs e

How we can call Button_Cick programatically

Help Me.

推荐答案

将您的代码从Button_Click事件中放入方法,即
Put your code from your Button_Click event into a method i.e.
private void SomeMethod()
{
}


然后从Button_Click调用此方法,然后从任何其他位置从应用程序中调用它.这样一来,它就可以在任何地方使用,而不会发生任何疯狂的黑客攻击.您也可以将Button_Click事件处理程序分配给许多不同的按钮(和其他控件).这可以在设计模式下完成,方法是选择要分配给它的按钮,然后在属性面板中单击事件按钮(看起来像闪电),然后在按钮的下拉列表中单击事件,选择Button_Click.希望这很清楚,对您有帮助.

另外,如果看不到属性面板,则可以通过按键盘上的Ctrl + W + P来显示它.


好的,所以我想也许我应该给您介绍一下对象发件人和EventArgs e的用途.发送者是触发事件的控件.在您的情况下,这将是您的按钮,并且可以像这样被投射回按钮


then call this method from your Button_Click and from where ever else you need to call it from in your app. That way it is usable from every where with out any crazy hacks. Also you may assign the Button_Click event handler to many different buttons (and other controls). This can be done in design mode by selecting the button you want to assign it to then in the properties panel click on the events button(looks like lightning bolt) then in the drop down for your buttons click event choose Button_Click. Hope this is clear and it helps you out.

Also if your properties panel is not visible you can display it by pressing Ctrl + W + P on your keyboard.


Ok so I thought maybe I should give you and idea of what object sender and EventArgs e are for. The sender is the control that fired the event. In your case this would be your button and could be cast back into a button like so

Button button = sender as Button;


EventArgs是一种将额外信息传递给事件处理程序的方法.对于Button_Click处理程序,EventArgs不包含任何有用的信息. EventArgs只是其他EventArgs的基类,例如MouseEventArgs,它确实包含一些有用的信息,例如e.Button包含单击以触发事件的按钮,或者e.Location包含事件发生时的鼠标光标的位置.被解雇.希望这对您有意义,我的大脑似乎今天似乎还没有运转. :)


The EventArgs is a way to pass extra information to the event handler. In the case of your Button_Click handler EventArgs contains no useful information. EventArgs is just the base class for other EventArgs like MouseEventArgs which does contain some useful information like e.Button which contains the button that was clicked to fire the event or e.Location which contains the location of the mouse cursor at the time the event was fired. Hopefully this will make sense to you my brain just does not seem to be working today. :)


我认为LanFanNinja为您提供了许多问题的很好的答案,并且很好地概述了EventArgs的基本知识.

在设计时,您可以按住Shift键并单击以选择一整堆按钮,甚至是嵌套"在其他容器中的容器中……然后按F4进入属性"检查器,然后将所有事件设置为相同的步骤:

如果您这样做了:然后,如LanFanNinja向您展示的那样,您可能需要使用强制转换,将传递给Click事件的sender对象转换回Button,从而获得单击了哪个Button……一个switch/case语句或某种结构,可让您根据单击的Button来确定要执行的操作.

有一种简单的方法可以以编程方式调用按钮单击事件":示例:
I think LanFanNinja has given you a good answer to many aspects of your questions, and a good overview of the basics of EventArgs.

At design-time you can control-shift-click select a whole bunch of buttons ... even ones "nested" inside containers in other containers ... and F4 your way to the Properties Inspector, and set all their whatever Events to the same procedure:

If you do that: then, as LanFanNinja showed you, you''ll probably need to use casting to get which Button was clicked, by converting the ''sender object passed into the Click event back into a Button ... and then have a switch/case statement, or some structure, to let you determine what to do based on which Button was clicked.

There is a simple way to invoke a Button Click Event programatically: example:
button1.PerformClick();

但是,请考虑一下您在做什么:将用户在运行时可能做的事情混合在一起……异步触发事件(一种奇特的说法:随时")...用您正在您的代码中进行的操作.

在给定的情况下,我鼓励您考虑这种混合考虑"是否是好的设计.

But, think of what you are doing here: you are mixing together something the user may do at run-time ... triggering an event asynchronously (a fancy way of saying: "at any time") ... with something you are doing inside your code.

In a given scenario, I''d encourage you to think about whether this "mixing of concerns" is good design or not.


您实际上并没有显示任何事件.您的方法Button_Click不是事件;它不是事件.这只是一个方法,其名称和签名都没有暗示它用作事件处理程序.当您将此方法添加到某个事件的调用列表时,就会发生这种情况.这是您未显示的一些代码.因此,解决方案将只是使用一些任意参数调用该方法,但是sender应该是对同一按钮的引用,并且在这种情况下,event arguments参数无关紧要.现在,处理程序可能不使用任何这些参数,因此它们全都可以是null.这是基于现有代码进行调用的最简单方法,但是以上所有内容看起来都非常糟糕.

这样会更好:

You don''t really show any event. Your method Button_Click is not an event; this just a method, nothing by its name and signature suggests it''s used as an event handler. It happens when you add this method to the invocation list of some event; this is some code you did not show. So, the solution would be just calling this method with some arbitrary parameters, but sender is supposed to be a reference to the same button, and the event arguments parameter does not matter in this case. Now, chances are that handler does not use any of these parameters, so they all can be just null. This is a simplest way to make a call based on your existing code, but all the above looks like a pretty bad style.

This would be much better:

void MyButtonClickHandler() {
    //...
}

myButton.Click += (sender, eventArgs) => {
   MyButtonClickHandler();
}

//...

// now, somewhere else, you can call the handler in a way not 
// related to myButton:
MyButtonClickHandler();



—SA



—SA


这篇关于清除事件中参数的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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