How do I pass along variable in a custom control?
I've created a custom control in C# in VSE, and it appears on the list on the left. But I want to pass along a reference to my main form, and I'm currently doing so in the constructor. And then I've manually edited, the VSE Designer generated code to pass along the form itself. But hereafter when i try to with the designview VSE shows an errormessage. Have i done this the wrong way?
class canvas : Control
{
public 开发者_运维问答canvas(Form1 theForm)
{
}
}
And then in the designer generated code:
this.canvas2 = new panel2.canvas(this);
you could just use a public property.
public Form1 mainForm { get; set; }
You can then pass it in as a parameter on your canvas object.
<canvas mainForm='<%= this %>'></canvas>
Or from code:
this.canvas2.mainForm = this;
Possibly, instead of passing the Form through your Control's constructor, you can use the Control's FindForm()
method in order to get the form in which the control has been placed.
精彩评论