[英]How to make this method reusable?
我已经将此方法复制粘贴到两个类中。 我宁愿从头等课重用它。 这是在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”仅适用于调用该方法的类,但是我仍然认为它是指定义该方法的类。将类作为参数简单地传递将是很棒的,但是我在想这是行不通的,因为我已经尝试过了。
任何帮助表示赞赏。 谢谢!
我会这样做:
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
}
您可以创建一个如下所示的帮助器类:
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
作为附加参数传递。
您可以这样写:
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)
{
}
}
您可以使用表单属性。 在每个表单中添加以下内容:
public Control clsThis {get {return this; }}
那么您可以使用clsThis引用该控件并将其作为参数发送给该方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.