我可以将附加参数传递给事件吗? [英] Can I Pass Additional Parameter To An Event?

查看:77
本文介绍了我可以将附加参数传递给事件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我们可以将其他参数传递给事件处理程序方法。

我的表单中有一个面板,我想要在它的绘画事件上用渐变颜色绘制它。

颜色将是有条件的并且基于某些值。我可以直接将此值传递给paint事件。

我不想使用全局变量或属性。

有没有选项?

Hello,

Can we pass an additional parameter to an event handler method.
I have a panel in my form and i want to paint it with gradient colors on its paint event.
The Colors will be conditional and based on some value. Can i directly pass this value to the paint event.
I don't want to use global variable or property.
Is there any option?

推荐答案

不是事件,而是事件处理程序。



偶数处理程序的参数由事件定义参数class(派生自 System.EventArgs );并且您无法更改事件处理程序的签名。所以,您应该问:我可以传递更多信息吗?。当然你可以。



(如果是你自己定义的事件,你需要创建自己的事件参数类,在那里你可以提供你需要的所有信息。在 Paint 事件的情况下,这不是一个选项。)



你可以在一个上实现渲染有两种方法:1)覆盖 Control.OnPaint ; 2)处理事件.Control.Paint。这些方法的签名是:

Not to "event", but to an event handler.

Parameters of the even handlers are defined by the "event arguments" class (derived from System.EventArgs); and you cannot change the signature of the event handler. So, you should have asked, "can I pass additional information?". Of course you can.

(If it was the event you define yourself, you would need to create you own event arguments class where you would put all the information you need. In your case of Paint event, this is not an option.)

You can implement rendering on one of two ways: 1) overriding Control.OnPaint; 2) handling the event .Control.Paint. The signatures of these methods are:
protected override void OnPaint(PaintEventArgs e) { /* ... */ }

//...

// alternatively, an event handler:
void SomeEventHandlerOfThePaintEvent(object sender, PaintEventArgs e) { /* ... */ }



有多少参数?答案1和2是错误的。实际上,它是2和3.还有一个隐藏的参数:this,因为第一种方法是实例(非静态)方法;并且任何事件处理程序都可以是实例1。您可以使用this传递任何其他信息(这是OOP初学者的基本元素,甚至不使用OOP的核心:后期绑定):


How many parameters are there? The answer 1 and 2 would be wrong. Actually, it's 2 and 3. There is one more hidden argument: "this", because first method is the instance (non-static) method; and any event handler can be instance one. You can use "this" to pass any additional information (and this is the very basic element of the beginners of OOP, not even using the heart of OOP: late binding):

using System.Windows.Forms;
using System.Drawing;
//...

class SomeControl : Control {

    Color someGradientColor;
    Color CalculteAnotherGradientColor() { /* ... */ }

    protected override void OnPaint(PaintEventArgs e) {
        // use someGradientColor here
        // ...
        Color someColorToRender = CalculteAnotherGradientColor();
        // and so on...
        // for further detail, please see my past answers referenced below
    }

} //class SomeControl





类似的东西。



请参阅我过去的答案:



静态vs实例方法:

在c#中输入类型 [< a href =http://www.codeproject.com/Answers/701146/Type-casting-in-csharp#answer3target =_ blanktitle =New Window> ^ ],
C#windows base this key单词相关及其在应用程序中的用途 [ ^ ],

< a href =http://www.codeproject.com/Answers/223846/What-makes-static-methods-accessible#answer1>是什么让静态方法可以访问? [ ^ ];

使用传递数据渲染图形:

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

在面板上捕获绘图 [ ^ ],

mdi子表单之间的绘制线 [ ^ ],

如何加快我的vb.net应用程序? [ ^ ],

用C#.net鼠标滚轮缩放图像 [ ^ ]。



-SA



Something like that.

Please see also my past answers:

Static vs instance methods:
Type casting in c#[^],
C# windows base this key word related and its uses in the application[^],
What makes static methods accessible?[^];
Rendering graphics with passing data:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
How to speed up my vb.net application?[^],
Zoom image in C# .net mouse wheel[^].

—SA


这篇关于我可以将附加参数传递给事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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