自己使用listbox实现toolbox

//首先定义可以在设计时编译的底层代码。windowmessagedesigner. [DesignerAttribute(typeof(WindowMessageDesigner), typeof(IDesigner))] [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,Name="FullTrust")] public partial class UserControlMy : UserControl { //工具箱管理的方法集合。该类型在本例的主要的作用是:添加各类工具。 IToolboxService toolbarService; ListBox lb; ListBox lb2; //所有工具集合 ToolboxItemCollection items; public UserControlMy() { InitializeComponent(); lb = new ListBox(); this.Controls.Add(lb); lb.Dock = DockStyle.Left; lb.DoubleClick += new EventHandler(lb_DoubleClick); lb2 = new ListBox(); lb.Width = 200; lb.Height = 200; this.Controls.Add(lb2); lb2.Dock = DockStyle.Right; } void lb2_DoubleClick(object sender, EventArgs e) { Form parentForm = this.FindForm(); MessageBox.Show(parentForm.Name); //获取设计器的宿主,用于在设计器上添加控件。 IDesignerHost host= parentForm.Site.GetService(typeof(IDesignerHost)) as IDesignerHost; if (host != null) { if (toolbarService != null) { toolbarService.SetSelectedToolboxItem(items[lb2.SelectedIndex]); MessageBox.Show(lb2.SelectedIndex.ToString()); //这个方法不好用,怎么调试都返回空。暂时还不理解其中原理。 ToolboxItem tbi = toolbarService.GetSelectedToolboxItem(); if (tbi== null) { MessageBox.Show("select is null"); tbi = items[lb2.SelectedIndex]; MessageBox.Show("component begin"); //在界面上创建控件。 IComponent[] comp= tbi.CreateComponents(host); //创建完控件后,没有下面的代码 MessageBox.Show("component end"); if (comp!=null) { MessageBox.Show("component count is : "+comp.Count().ToString()); } else { MessageBox.Show("component null"); } } else { MessageBox.Show("select is good"); // The IToolboxService serializes ToolboxItems by packaging them in DataObjects. DataObject d = toolbarService.SerializeToolboxItem(tbi) as DataObject; MessageBox.Show(d.ToString()); try { lb2.DoDragDrop(d, DragDropEffects.Copy); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } //this.toolbarService.GetSelectedToolboxItem().CreateComponents(host); } } else { MessageBox.Show("host is null"); } } void lb_DoubleClick(object sender, EventArgs e) { toolbarService.SelectedCategory = lb.SelectedItem.ToString(); MessageBox.Show("Initial Lb2" + this.toolbarService.SelectedCategory); InitialLB2(this.toolbarService.SelectedCategory); } void lb_KeyDown(object sender, KeyEventArgs e) { } private void InitialSite() { if (base.Site != null) { //Site站点。里面存储了控件需要用的各种服务。这个是微软给实现好的。只有继承自usercontrol类的都已经具有值。 toolbarService = base.Site.GetService(typeof(IToolboxService)) as IToolboxService; lb2.DoubleClick += new EventHandler(lb2_DoubleClick); MessageBox.Show("Site is Good"); } else { //在编译刚结束后,site是为空的。必须重载site. MessageBox.Show("abc"); } } public override ISite Site { get { return base.Site; } set { base.Site = value; InitialSite(); if (toolbarService!=null) { InitialListboxItem(); } else { MessageBox.Show("Toolbar is null"); } } } private void InitialListboxItem() { if (toolbarService==null) { MessageBox.Show("Toolbar is null"); return; } for (int i = 0; i < toolbarService.CategoryNames.Count; i++) { this.lb.Items.Add(toolbarService.CategoryNames[i]); } MessageBox.Show("ToolbarService is Good"); } private void InitialLB2(string categoryName) { this.lb2.Items.Clear(); if (items == null) { items = new ToolboxItemCollection(toolbarService.GetToolboxItems(categoryName)); } else { items = toolbarService.GetToolboxItems(categoryName); } MessageBox.Show("Count : "+items.Count.ToString()); foreach (var item in items) { lb2.Items.Add(item); } } } 本例主要设计的类型: IToolboxService ISite IDesignerHost
posted @ 2012-10-30 19:56  viola  阅读(200)  评论(0)    收藏  举报