0

我已将此方法复制粘贴到两个类中。我宁愿从头等舱重用它。这是在 Windows 窗体应用程序中。

public void defineMapArea()
{
    PictureBox pictureBox1 = new PictureBox();

    // Dock the PictureBox to the form and set its background to white.
    pictureBox1.Dock = DockStyle.Fill;
    pictureBox1.BackColor = Color.White;

    // Connect the Paint event of the PictureBox to the event handler method.
    pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);

    // Add the PictureBox control to the Form. 
    this.Controls.Add(pictureBox1);
}

唯一需要将方法从一个类更改为另一个类的“this”关键字是指该类,因为将鼠标悬停在“this”上可以确认。我想也许“this”只适用于调用该方法的类,但我认为它仍然指的是定义该方法的类。简单地将一个类作为参数传递会很棒,但我在想这不起作用,因为我已经尝试过了。

任何帮助表示赞赏。谢谢!

4

4 回答 4

5

我会这样做:

public static void DefineMapArea(Form form, Action<object, PaintEventArgs> handler)
{
    if (form == null)
    {
        throw new ArgumentNullException("form");
    }

    if (handler == null)
    {
        throw new ArgumentNullException("handler");
    }

    PictureBox pictureBox1 = new PictureBox();

    // Dock the PictureBox to the form and set its background to white.
    pictureBox1.Dock = DockStyle.Fill;
    pictureBox1.BackColor = Color.White;

    // Connect the Paint event of the PictureBox to the event handler method.
    pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(handler);

    // Add the PictureBox control to the Form. 
    form.Controls.Add(pictureBox1);
}

您可以调用它(从您的表单中假设):

DefineMapArea(this, (sender, e) =>
{
    // ... put here your code
});

或者

DefineMapArea(this, Handler);

void Handler(object sender, PaintEventArgs e)
{
    // ... put here your code
}
于 2013-09-10T08:55:53.643 回答
3

您可以创建一个如下所示的辅助类:

public static class MapHelper
{
    public static void defineMapArea(this Control parent, PaintEventHandler handler)
    {
        PictureBox pictureBox1 = new PictureBox();

        // Dock the PictureBox to the form and set its background to white.
        pictureBox1.Dock = DockStyle.Fill;
        pictureBox1.BackColor = Color.White;

        // Connect the Paint event of the PictureBox to the event handler method.
        pictureBox1.Paint += handler;

        // Add the PictureBox control to the Form. 
        parent.Controls.Add(pictureBox1);
    }
}

然后这样称呼它:

parentControl.defineMapArea(new PaintEventHandler(this.pictureBox1_Paint));

这是一种适用于任何控件的扩展方法!请注意,您必须传递PaintEventHandleras 附加参数。

于 2013-09-10T08:58:13.663 回答
0

你可以这样写:

class TestClass
{
    internal static void defineMapArea(Form1 form)
    {
        PictureBox pictureBox1 = new PictureBox();

        // Dock the PictureBox to the form and set its background to white.
        pictureBox1.Dock = DockStyle.Fill;
        pictureBox1.BackColor = Color.White;

        // Connect the Paint event of the PictureBox to the event handler method.
        pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(form.pictureBox1_Paint);

        // Add the PictureBox control to the Form. 
        form.Controls.Add(pictureBox1);
    }
}

并且可以通过这种方式从其他类访问:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        TestClass.defineMapArea(this);
    }

    public void pictureBox1_Paint(object sender, EventArgs e)
    {

    }
}
于 2013-09-10T08:56:35.767 回答
0

您可以使用表单属性。在每个表单中添加以下内容:

公共控制 clsThis { get { return this; } }

然后您可以使用 clsThis 引用该控件并将其作为参数发送到该方法

于 2013-09-10T09:20:43.463 回答