C#在设计时如何实现拖放自定义控件?

zhoutiance 2007-09-07 02:49:02
想实现这样一个功能:在设计时拖放可以同时放入多个控件,放入控件后,自动更改控件的属性(要更改的值是已知的)
举个例子,拖放一个项目后,自动在界面上生成一个Label和一个TextBox,Label的Text显示为AAA,TextBox的Text显示为BBB。
请高手指点
...全文
1694 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhoutiance 2007-09-27
  • 打赏
  • 举报
回复
感谢啊!
PTPVP 2007-09-25
  • 打赏
  • 举报
回复
.net 2.0下可以用下面的方法:

创建一个自定义控件包含Label和TextBox,自定义一个DesignSurface:
class myDesignerSurface : DesignSurface
{
public myDesignerSurface()
{
myToolBox toolBox = new myToolBox();
this.ServiceContainer.AddService(typeof(IToolboxService), toolBox);
this.ServiceContainer.AddService(typeof(INameCreationService),
new NameCreationService());
}
}

NameCreationService类实现INameCreationService接口。

写一个myToolBox类,继承ListBox和IToolboxService。用他来存放自定义控件

class myToolBox : ListBox, IToolboxService

{

private IDesignerHost m_designerHost;



public IDesignerHost DesignerHost

{

set { m_designerHost = value; }

get { return m_designerHost; }

}



public myToolBox()

{

this.DrawMode = DrawMode.OwnerDrawFixed;

this.SelectionMode = SelectionMode.One;

this.populateItems();

}



private void populateItems()

{

this.Items.Add(typeof(Button));

this.Items.Add(typeof(TextBox));

this.Items.Add(typeof(MonthCalendar));

this.Items.Add(typeof(RichTextBox));

//......

}



protected override void OnDrawItem(DrawItemEventArgs e)

{

e.DrawBackground();

Type type = this.Items[e.Index] as Type;

System.Drawing.ToolboxBitmapAttribute tba =

TypeDescriptor.GetAttributes(type)[typeof(System.Drawing.ToolboxBitmapAttribute)]

as System.Drawing.ToolboxBitmapAttribute;

Bitmap bmp = (System.Drawing.Bitmap)tba.GetImage(type);

Rectangle imgRect = e.Bounds;

imgRect.Width = 17;

e.Graphics.DrawImage(bmp, imgRect);

Rectangle rect = e.Bounds;

rect.X += 17;

e.Graphics.DrawString(type.Name, e.Font, new SolidBrush(e.ForeColor), rect);

base.OnDrawItem(e);

}



protected override void OnMouseDown(MouseEventArgs e)

{

if (this.SelectedIndex != -1)

{

Type type = this.SelectedItem as Type;

ToolboxItem tbi = new ToolboxItem(type);

// The IToolboxService serializes ToolboxItems by packaging them in DataObjects.

DataObject d = this.SerializeToolboxItem(tbi) as DataObject;

try

{

this.DoDragDrop(d, DragDropEffects.Copy);

}

catch { }

}



base.OnMouseDown(e);

}



#region IToolboxService Members



public System.Drawing.Design.ToolboxItem DeserializeToolboxItem(object serializedObject, IDesignerHost host)

{

ToolboxItem tbi = (ToolboxItem)((DataObject)serializedObject).GetData

(typeof(System.Drawing.Design.ToolboxItem));

myUserControl uc = new myUserControl();

uc.ControlName = "something";

uc.ControlText = "something";

return new ToolboxItem(typeof(myUserControl));

}

//

}

///郁闷的排版。。。。

这样的方法应该可以实现你想要的功能。
Miamiiii 2007-09-19
  • 打赏
  • 举报
回复
学习
北京的雾霾天 2007-09-19
  • 打赏
  • 举报
回复
通过用 .NET 生成自定义窗体设计器来定制应用程序

参考:
http://www.microsoft.com/china/MSDN/library/netFramework/netframework/CustomFormsDesigner.mspx?mfr=true


CustomFormsDesigner
下载地址:
http://download.microsoft.com/download/d/3/1/d31fff33-fd97-488f-9bbd-4b7402905716/CustomFormsDesigner.exe
zfwdf 2007-09-17
  • 打赏
  • 举报
回复
关注
zhoutiance 2007-09-17
  • 打赏
  • 举报
回复
一周了。。。
zhoutiance 2007-09-13
  • 打赏
  • 举报
回复
zhaochong12(超级大笨鸟)
这个里面也没有拖放多个控件的例子。。。。
hcs2007 2007-09-13
  • 打赏
  • 举报
回复
学习 刚学
wdzr_826 2007-09-13
  • 打赏
  • 举报
回复
mark
Sonny_liu_RPT 2007-09-13
  • 打赏
  • 举报
回复
好啊!
Optione 2007-09-10
  • 打赏
  • 举报
回复
删除自己?删除PanelEx么?
zhaochong12 2007-09-10
  • 打赏
  • 举报
回复
http://www.codeproject.com/csharp/SharpFormEditorDemo.asp
建立一个类似C#的环境, 实现控件拖拉,属性
Crafting a C# forms Editor From scratch
zhoutiance 2007-09-10
  • 打赏
  • 举报
回复
周一了啊,哪位大大帮帮忙啊
Tensionli 2007-09-10
  • 打赏
  • 举报
回复
关注
symbol441 2007-09-10
  • 打赏
  • 举报
回复
up
Optione 2007-09-10
  • 打赏
  • 举报
回复
这个方法不是很好,因为布局一直在发生变化,所以就不能在对Text属性赋值了(赋值了,事件一改变又还原了)
1. 自己删除了, 添加的控件往哪里放呢?
2. 什么时候,自己才能删除啊?
heqi915 2007-09-10
  • 打赏
  • 举报
回复
有意思,关注之中,帮顶一下
zhoutiance 2007-09-10
  • 打赏
  • 举报
回复
是啊
chuxue1342 2007-09-07
  • 打赏
  • 举报
回复
mark!!
monkey2307 2007-09-07
  • 打赏
  • 举报
回复
学习
加载更多回复(10)

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧