从工具箱拖放自定义控件时生成代码或事件 [英] Generate code or events when drag-drop a custom control from the toolbox
问题描述
我正在创建一个自定义控件(例如自定义LookUpEdit),我想知道是否可以在拖放时自动生成代码(例如,form.cs文件中的EditValueChanged事件或某些代码行,例如,Form_Load事件中的代码)表单设计器中的此控件,从而使任何开发人员都可以根据需要轻松地查看和扩展此代码。
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.
我的问题是:在设计时从工具箱中拖放自定义控件时,如何在表单(form.cs)中自动生成代码或事件? / p>
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?
推荐答案
有时,您可能希望在将组件放到窗体上时生成一些代码。例如,当您从数据源窗口中拖动数据源并将其放在窗体上时,您会看到类似的行为。
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.
答案的关键点是:
- 为...创建新的设计师通过从
ControlDesigner
- 使用
IEventBindingService
- 使用
CodeTypeDeclaration
服务。
- 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.
示例
我创建了一个 MyControl
合作伙伴在此示例中为ntrol。当您在窗体上放置控件的实例时,设计器将为其 Click
事件生成以下代码:
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!");
}
这是 MyControl的代码
和 MyControlDesigner
。您可能想在代码中添加空检查。
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);
}
}
}
这篇关于从工具箱拖放自定义控件时生成代码或事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!