1

非常に単純なことをしています。

イベントが次のように設定されているリストボックスがあります。

    public Form1()
    {
        InitializeComponent();
        this.listBox1.AllowDrop = true;
        this.listBox1.DragEnter += new DragEventHandler(listBox1_DragEnter);
        this.listBox1.DragDrop += new DragEventHandler(listBox1_DragDrop);
    }

    void listBox1_DragDrop(object sender, DragEventArgs e)
    {
       //code to add labelText to Items of ListBox
    }

    void listBox1_DragEnter(object sender, DragEventArgs e)
    {
        //set DragDropEffects;
    }

今私は次のようなラベル、コードを持っています:

    private void label1_MouseDown(object sender, MouseEventArgs e)
    {
        DoDragDrop((sender as Label).Text, DragDropEffects.Copy);
        //this.label1.DoDragDrop((sender as Label).Text, DragDropEffects.Copy);
        //used one of them at a time.

    }

しかし、何も起こりません。listboxDragEnterイベントが発生することはありません。実際、ドラッグは発生しません。ラベル(テキスト)をドラッグしようとすると、' DragDropEffects.Copy'のカーソルの代わりに許可されていないウィンドウカーソルが表示されます

ドラッグアンドドロップは行われません。

リストボックス(および関連するコード)を変更して、他のウィンドウからリストボックスにドロップするファイルを受け入れると、完全に機能します。

そのため、フォームに保持されているコントロールから同じフォームに保持されている別のコントロールへのドラッグを実行できません。

私は何かが足りないのですか?WindowsXPを実行しています。

私はこれとこれを通り抜けました

助けてください...

4

3 に答える 3

3

あなたのコードは実際に機能します。イベントハンドラーで適切なドラッグ効果を設定する必要があります。

void listBox1_DragDrop(object sender, DragEventArgs e)
{
  e.Effect = DragDropEffects.Copy;
}

void listBox1_DragEnter(object sender, DragEventArgs e)
{
  e.Effect = DragDropEffects.Copy;
}
于 2012-07-25T10:11:49.563 に答える
2

ListBox.AllowDropがTRUEに設定されているかどうかを確認します

于 2012-07-25T10:05:59.240 に答える
0

以下は、必要なものの例であり、すべてのコードが含まれています(この投稿を見つけた人のためにここに追加してください)。

#region Initial Values
//Constructor:
public Form1() {
   InitializeComponent();
}

private void Form1_Load( object sender, EventArgs e ) {
   InitialValues();
}

private void InitialValues() {
   PrepareDragAndDrop();
}
#endregion Initial Values

#region Drag & Drop

private void PrepareDragAndDrop() {
   //For the object that receives the other dragged element:
   TheSamplListBox.AllowDrop = true;
   TheSamplListBox.DragEnter += TheSamplListBox_DragEnter;
   TheSamplListBox.DragLeave += TheSamplListBox_DragLeave;
   TheSamplListBox.DragDrop  += TheSamplListBox_DragDrop;

   //For the object that will be dragged:
   TheSampleLabel.MouseDown += ( sender, args ) => DoDragDrop( TheSampleLabel.Text, DragDropEffects.Copy );
}

private void TheSamplListBox_DragEnter( object theReceiver, DragEventArgs theEventData ) {
   theEventData.Effect = DragDropEffects.Copy;

   //Only the code above is strictly for the Drag & Drop. The following is for user feedback:
   //You can use [TheSamplListBox] but this approach allows for multiple receivers of the same type:
   var theReceiverListBox = (ListBox) theReceiver;

   theReceiverListBox.BackColor = Color.LightSteelBlue;
}

private void TheSamplListBox_DragLeave( object theReceiver, EventArgs theEventData ) {
   //No code here for the Drag & Drop. The following is for user feedback:
   //You can use [TheSamplListBox] but this approach allows for multiple receivers of the same type:
   var theReceiverListBox = (ListBox) theReceiver;

   theReceiverListBox.BackColor = Color.White;
}

private void TheSamplListBox_DragDrop( object theReceiver, DragEventArgs theEventData ) {
   //You can use [TheSamplListBox] but this approach allows for multiple receivers of the same type:
   var theReceiverListBox = (ListBox) theReceiver;

   //Get the data being dropped. In this case, a string:
   var theStringBeingDropped = theEventData.Data.GetData( "System.String" );

   //Add the string to the ListBox:
   theReceiverListBox.Items.Add( theStringBeingDropped );

   //Only the code above is strictly for the Drag & Drop. The following is for user feedback:
   theReceiverListBox.BackColor = Color.White;
}
#endregion Drag & Drop

于 2016-08-05T00:22:03.700 に答える