質問

テキストボックスなどのコントロールをC#でドラッグアンドドロップできるようにする方法はありますか?

ユーザーがマウスでコントロールをクリックしてホールドし、そのサーフェス上でドラッグして、そのサーフェス内の任意の場所にドロップできるようにする必要があります。

これを実装する方法はありますか?

役に立ちましたか?

解決

この回答は、たくさん。どのタイプのコントロールとコンテナでもうまく機能しています。

他のヒント

コントロールが1つのコンテナ(パネルなど)内で移動している場合、OnMouseDown / OnMouseMoveイベントをオーバーライドし、コントロールのLocationプロパティを調整できます。

質問に基づいて、完全なドラッグアンドドロップ(異なるコントロール間またはアプリケーション間でのデータの移動)が必要ではないようです。

silverlightコンテナの外側からアイテムをドラッグしようとしている場合、最善の策は silverlight 4ベータ

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と呼ばれていたものしかありません。

とにかく、次のコードがその方法を示しているはずです。必要なラベルは1つ( 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);
                }
            }
        }
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top