有沒有一種方式,可以讓一個控件,如一個文本框,拖動可棄在C#中?C#:在表面拖放控件
我希望用戶必須點擊並按住鼠標控制,並拖動它在其表面上和表面內的任何地方拖放的能力。
任何人有任何想法如何實現這一點?
有沒有一種方式,可以讓一個控件,如一個文本框,拖動可棄在C#中?C#:在表面拖放控件
我希望用戶必須點擊並按住鼠標控制,並拖動它在其表面上和表面內的任何地方拖放的能力。
任何人有任何想法如何實現這一點?
This answer幫了我很多。它在任何類型的控制和容器上都很棒。
如果你控制一個容器(例如面板)中移動,則可以覆蓋onmousedown事件/事件的OnMouseMove,並調整控制的位置屬性。
根據您的問題,它似乎並不認爲你需要完整的拖和拖放(移動不同的控制,甚至應用程序之間的數據)。
沒錯,我不需要在任何控件或應用程序之間移動數據,只需將控件移動到其容器中... – 2009-11-26 10:15:50
如果你想從Silverlight的容器外拖動一個項目,那麼最好的辦法是檢查出silverlight 4 beta
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
// wire up the various Drop events
InstallButton.Drop += new DragEventHandler(InstallButton_Drop);
InstallButton.DragOver += new DragEventHandler(InstallButton_DragOver);
InstallButton.DragEnter += new DragEventHandler(InstallButton_DragEnter);
InstallButton.DragLeave += new DragEventHandler(InstallButton_DragLeave);
}
void InstallButton_Drop(object sender, DragEventArgs e)
{
IDataObject foo = e.Data; // do something with data
}
這在VB6中過去很容易。但現在我們真的只有過去被稱爲OleDrag的東西。
不管怎樣,下面的代碼應該告訴您如何。您只需要一個標籤(dragDropLabel),並將AllowDrop屬性的表單(DragDropTestForm)設置爲True。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DragDropTest
{
public partial class DragDropTestForm : Form
{
// Negative offset to drop location, to adjust for position where a drag starts
// on a label.
private Point _labelOffset;
// Save the full type name for a label, since this is used to test for the control type.
private string labelTypeName = typeof(Label).FullName;
public DragDropTestForm()
{
InitializeComponent();
}
private void dragDropLabel_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_labelOffset = new Point(-e.X, -e.Y);
}
}
private void dragDropLabel_MouseMove(object sender, MouseEventArgs e)
{
const double minimumDragDistance = 4;
const double minimumDragDistanceSquared = minimumDragDistance * minimumDragDistance;
if (e.Button == MouseButtons.Left)
{
// Minimum n pixel movement before drag starts.
if (((Math.Pow(_labelOffset.X - e.X, 2)) + Math.Pow(_labelOffset.Y - e.Y, 2)) >= minimumDragDistanceSquared)
{
dragDropLabel.DoDragDrop(dragDropLabel, DragDropEffects.Move);
}
}
}
private void DragDropTestForm_DragOver(object sender, DragEventArgs e)
{
IDataObject data = e.Data;
string[] formats = data.GetFormats();
if (formats[0] == labelTypeName)
{
e.Effect = DragDropEffects.Move;
}
}
private void DragDropTestForm_DragDrop(object sender, DragEventArgs e)
{
IDataObject data = e.Data;
string[] formats = data.GetFormats();
if (formats[0] == labelTypeName)
{
Label label = (Label) data.GetData(formats[0]);
if (label == dragDropLabel)
{
Point newLocation = new Point(e.X, e.Y);
newLocation.Offset(_labelOffset);
dragDropLabel.Location = this.PointToClient(newLocation);
}
}
}
}
}
好的 - 是我的想象,還是這與Silverlight本來沒有關係?在這種情況下,忽略這一點 - 它只是WinForms。 – 2009-11-26 11:26:36
你使用的是asp.net或桌面應用程序嗎? – Kamal 2009-11-26 10:13:33
我想在Silverlight應用程序中實現,如果可能的話?? – 2009-11-26 10:16:26