如何使这个方法可重用?

时间:2013-09-10 08:51:51

标签: c# reusability

我已将此方法复制粘贴到两个类中。我宁愿在第一堂课中重复使用它。这是在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 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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));

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

答案 2 :(得分: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)
    {

    }
}

答案 3 :(得分:0)

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

公共控制clsThis         {             得到             {                 归还这个;             }         }

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