簡體   English   中英

將一個控件拖放到Winform中的另一個控件

[英]Drag and Drop one control to another control in winform

我做的事情非常非常簡單。

我有一個列表框,其事件設置如下:

    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.

    }

但什么也沒發生。 列表框DragEnter事件從不觸發。 實際上,拖曳永遠不會發生。 每當我嘗試拖動標簽(文本)時,出現不允許的Windows光標,而不是' DragDropEffects.Copy的光標

拖放不會發生。

當我修改列表框(及其關聯的代碼)以接受要從任何其他窗口拖放到其上的文件時,效果很好。

so..am無法執行從窗體上保留的控件到同一窗體上另一個控件的拖動。

我錯過了什么嗎? 正在運行Windows XP。

我經歷了這個 ,並通過這種

請幫忙...

您的代碼確實可以正常工作。 您只需要在事件處理程序中設置正確的拖動效果即可。

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

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

檢查ListBox.AllowDrop是否設置為TRUE

以下是所有代碼的示例(將其添加到此處的人員)。

#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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM