问题
I'm creating a custom control (e.g custom LookUpEdit) and I'm wondering if I can generate code (e.g EditValueChanged event in form.cs file or some rows of code e.g in Form_Load event) automatically while I drag and drop this control inside a form designer, giving this way to any developer the opportunity to easily see and extend this code if he/she wants.
My question is: How to generate code or events automatically in a form (form.cs) when you drag-drop a custom control from the toolbox at design time?
回答1:
Some times you may want to generate some code when dropping a component on form. For example, you can see a similar behavior when you drag a data source from data source window and drop it on form.
The behavior is more common for extensions and add-ins, but you can implement such behavior for your component when dropping from toolbox as well.
The key points in the answer are:
- Creating a new designer for the control by deriving from ControlDesigner
- Creating the event handler using IEventBindingService
- Adding code to the body of the event handler using CodeTypeDeclaration service.
Example
I've created a MyControl
control in this example. When you drop an instance of the control on the form, the designer will generate the following code for its Click
event:
private void myControl1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hi!");
}
Here is the code of MyControl
and MyControlDesigner
. You may want to add null checking to the code.
using System.CodeDom;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.Design;
[Designer(typeof(MyControlDesigner))]
public partial class MyControl : Control
{
}
public class MyControlDesigner : ControlDesigner
{
public override void InitializeNewComponent(IDictionary defaultValues)
{
base.InitializeNewComponent(defaultValues);
var component = this.Component;
var eventBindingService = (IEventBindingService)this.GetService(
typeof(IEventBindingService));
var componentChangeService = (IComponentChangeService)this.GetService(
typeof(IComponentChangeService));
var designerHostService = (IDesignerHost)GetService(typeof(IDesignerHost));
var uiService = (IUIService)GetService(typeof(IUIService));
var designerTransaction = (DesignerTransaction)null;
try
{
designerTransaction = designerHostService.CreateTransaction();
var e = TypeDescriptor.GetEvents(component)["Click"];
if (e != null)
{
var methodName = eventBindingService.CreateUniqueMethodName(component, e);
var eventProperty = eventBindingService.GetEventProperty(e);
if (eventProperty.GetValue(component) == null)
{
eventProperty.SetValue(component, methodName);
var code = this.GetService(typeof(CodeTypeDeclaration))
as CodeTypeDeclaration;
CodeMemberMethod method = null;
var member = code.Members.Cast<CodeTypeMember>()
.Where(x => x.Name == methodName).FirstOrDefault();
if (member != null)
{
method = (CodeMemberMethod)member;
method.Statements.Add(
new CodeSnippetStatement("MessageBox.Show(\"Hi!\");"));
}
componentChangeService.OnComponentChanged(component,
eventProperty, null, null);
eventBindingService.ShowCode(component, e);
}
}
designerTransaction.Commit();
}
catch (System.Exception ex)
{
if (designerTransaction != null)
designerTransaction.Cancel();
uiService.ShowError(ex);
}
}
}
来源:https://stackoverflow.com/questions/54212439/generate-code-or-events-when-drag-drop-a-custom-control-from-the-toolbox