简体   繁体   English

拖放winform控件

[英]drag and drop winform controls

I want to drag and drop a control (label for example) in a winform application. 我想在winform应用程序中拖放一个控件(例如标签)。 I saw some examples on dragging and dropping text, but this is not what I want. 我看到了一些关于拖放文本的例子,但这不是我想要的。 I want to enable the user to move a control around. 我想让用户移动控件。 Can anyone direct me to some resources or examples? 任何人都可以指导我一些资源或例子吗? Thanks. 谢谢。

you should look at examples on how to make draggable controls. 你应该看看如何制作可拖动控件的例子。

There are some answers here in SO as well. SO中也有一些答案。

See this Move controls when Drag and drop on panel in C# 在C#中拖放面板时看到此移动控件

this is a complete example on how to host the Form Designer: 这是关于如何托管表单设计器的完整示例:

Tailor Your Application by Building a Custom Forms Designer with .NET 通过使用.NET构建自定义窗体设计器来定制应用程序

I did something similar in Delphi long time ago, will search the source code, convert it into .NET C# and make a wiki page on that matter, as it is becoming such popular question recently :) 我很久以前在Delphi中做了类似的事情,将搜索源代码,将其转换为.NET C#并在此问题上制作一个wiki页面,因为它最近变得如此受欢迎的问题:)

As far as i understand, where you wish to drop a control is called a container, infact any control can act as a container. 据我所知,你想放弃一个控件的地方称为容器,实际上任何控件都可以作为一个容器。 So first that container, you need to enable the drop property as well as the drag property of the controls which you need to drag. 首先是容器,您需要启用drop属性以及需要拖动的控件的drag属性。

Then write events (Candrag, candrop, controladded, etc.) for each control where in which, some logic to hold the objects and display them as you may want. 然后为每个控件编写事件(Candrag,candrop,controladded等),其中包含一些逻辑来保存对象并按照您的需要显示它们。

Say, ill take an example where in which, you wish to drag imagetext from combombox into a picturebox and then make the picturebox analyze the text and fine related file name in a directory and load that image into its if its present. 比如说,生病了一个例子,你希望将图像文本从combombox拖到图片框中,然后让图片框分析目录中的文本和精细相关的文件名,并将该图像加载到其中。

So here, when you start dragging the text from combombox, you have to write some logic in event candrag. 所以在这里,当你开始从combombox中拖动文本时,你必须在事件candrag中写一些逻辑。 Then once you drop, you have to write logic to understand what kinda object was added and get the text related to it (kinda deciphering) in the control where you drop other control. 然后,一旦你放弃,你必须编写逻辑来理解添加了什么类型的对象,并获得与控制器相关的文本(有点解密)。

Sorry, i have no code to give you now, but i hope you got the idea how its done. 对不起,我现在没有代码可以给你,但我希望你知道它是如何完成的。 May be this article can help you? 可能这篇文章可以帮到你吗? http://vicky4147.wordpress.com/2007/02/04/a-simple-drag-drop-in-winforms/ http://vicky4147.wordpress.com/2007/02/04/a-simple-drag-drop-in-winforms/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM