Question

I have copy pasted this method into two classes. I would rather reuse it from the first class. This is in a windows forms application.

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);
}

The only thing that needs to change in the method from one class to another is the "this" keyword which refers to the class, as hovering over "this" confirms. I thought maybe "this" will just apply to the class calling the method, but i think it still refers to the class that the method is defined in. It would be fantastic to simply pass a class as a parameter, but i'm thinking that doesn't work as i have attempted that.

Any help appreciated. Thanks!

Was it helpful?

Solution

I would do so:

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);
}

You can call it (assuming from your form):

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

Or

DefineMapArea(this, Handler);

void Handler(object sender, PaintEventArgs e)
{
    // ... put here your code
}

OTHER TIPS

You could create a helper class that looks like this:

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);
    }
}

And then call it like this:

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

This is an extension method that works on any control! Please note that you have to pass the PaintEventHandler as additional parameter.

You may write like this:

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);
    }
}

And can access from other class in this way:

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)
    {

    }
}

You can use form properties. Add the following to each form:

public Control clsThis { get { return this; } }

then you can reference the control by using clsThis and send it to the method as a parameter

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top