如何将 EventArgs 传递给函数

How to pass EventArgs to a function

我有一个函数可以将图像绘制到表单中。

private void DrawImage()
{
    OpenFileDialog openfiledialog = new OpenFileDialog();
    if (openfiledialog.ShowDialog() == DialogResult.OK)
    {
        Bitmap image = (Bitmap)Image.FromFile(openfiledialog.FileName, true);
        TextureBrush texturebrush = new TextureBrush(image);
        texturebrush.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile;
        Graphics formGraphics = this.CreateGraphics();
        formGraphics.FillRectangle(texturebrush, new RectangleF(90.0F, 110.0F, 00, 300));
        formGraphics.Dispose();
    }
}

但我不会绘制任何图像,如果我在按钮单击事件中编写相同的代码,则可以正常工作。

private void button1_Click(object sender, EventArgs e)
{
    //Same code as written in DrawImage()
}

我认为的问题是它需要 "EventArgs e"。我在 msdn 中阅读了一些内容,但没有 sure.No 知道 EventArgs 在表单上绘制图像的作用是什么。

有什么方法可以实现这个功能。

TL;DR

没有。对于您的情况,请忽略它们。然而,它们有时可能很有用。阅读完整答案以了解。


完整答案

处理事件时,您会收到一个通用 EventArgs 对象,如果您编写自定义事件,您可能希望收到 EventArgs.

的自定义实现

为简单起见,将其视为保存事件相关数据的基 class。

因此,例如,如果您要实现点击事件,如图所示,您将收到两个参数:事件源和事件相关数据的持有者。

有时候这是没用的。就像你的样品一样。您不必接收它,也不必使用它。但是,您可以对源或事件数据执行一些检查,但这不是您代码的目标,因此两者在这里都没有用。

至于我,我更喜欢将 my 方法与事件分开,并从事件中调用它们。我确实将每个事件绑定放在 class 底部的 "group" 中,以保持一切干净和可读。这只是一个建议,您必须找到您自己的代码保存方式。


为了这个答案,这里有两个示例:

1。无用的参数

此示例只是关闭一个表单或 window。

public void btn1_Click(object sender, EventArgs e) { Close(); }

2。有用的参数

考虑一个具有某种类型的数据网格的组件,该组件使用所选行的 ID (PK) 触发 SelectedRowsChanged 事件。

// Event declaration. You can, after that, bind it elsewhere.
public event EventHandler<SelectionEventArgs> SelectedRowsChanged;

// This is the local implementation wich will fire the event.
// Here you invoke the event with the selected rows id's.
public void OnSelectedRowsChanged() { if (SelectedRowsChanged != null) CustomSelection(this, new SelectionEventArgs(this.SelectedRows)); }

// This is the custom implementation of the EventArgs to include the
// event-related data (row id)
public class SelectionEventArgs : EventArgs
{
   public int[] SelectedRows{ get; private set; }
   public SelectionEventArgs(int[] selectedRows) { SelectedRows = selectedRows; }
}

// ... then, somewhere else on your code

this.myControl.SelectedRowsChanged += myControl_SelectedRowsChanged;

public void myControl_SelectedRowsChanged(object sender, SelectionEventArgs e)
{
   if (e.SelectedRows.Length > 0) { /* do something */ }
}

对于内心强大的人,你也可以玩一下lambda。所以,而不是:

this.button1.Click += button1_Click;
public void button1_Click(object sender, EventArgs e) { DoSomething(); }

你可能只有这个:

this.button1.Click += (s, e) => { DoSomething(); };

与第一个示例一样,存在事件参数 ((s, e)),但它们对 DoSomething 方法毫无用处。

Lambda 可用于 C# 5 (.NET 4.5) 及更高版本。 有时在表单构造函数或类似的东西中这样做会更容易。

考虑这些提示来解决问题:

  • 如果您使用 this.CreateGraphics 在您的表单上绘图,那么您的绘图将在表单引用时消失,例如,如果您将其最小化并恢复。您应该将绘图逻辑放在 Paint 事件中。

  • 重构你的方法时,你应该传递一个Graphics类型的参数给你的方法,并用它来绘图。你也不应该处理传递的参数。您应该将 Paint 事件中的 e.Graphics 传递给您的方法。

  • 在你的方法中你应该处理你的画笔。将其放在 using 块中。

  • 重构方法时,应将显示对话框的代码部分移出方法。您应该在不需要时在 Paint 事件处理程序中调用它。

  • 最好不要使用Image.FromFile加载图片,它会锁定文件直到图片处理完毕。而是使用 Image.FromStream.

  • 您正在使用 0 作为 Width 的矩形。因此,如果您使用当前方法绘制,由于矩形的宽度,您将看不到任何结果。

代码

Bitmap image;
private void DrawImage(Bitmap image, Graphics g, Rectangle r)
{
    using (var brush = new TextureBrush(image))
    {
        brush.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile;
        g.FillRectangle(brush, r);
    }
}        

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog dialog = new OpenFileDialog();
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        using (var s = new System.IO.FileStream(dialog.FileName, System.IO.FileMode.Open))
            image = new Bitmap(Image.FromStream(s));

        this.Invalidate();
    }
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    if (image != null)
        this.DrawImage(image, e.Graphics, new RectangleF(10, 10, 200, 200));
}