简体   繁体   中英

Copy item from one listbox to another on double click. Doubleclick event not fired. Winform C#

I am quite new to Winform dev. I have two list boxes. When the user double clicks an item in the first listbox, I want this to be copied to the second list box. The problem is that my double click method never gets fired. here's my code:

//here I register the event
this.fieldsArea.MouseDoubleClick += new MouseEventHandler(fieldsArea_MouseDoubleClick);

Then here is the double click method:

    private void fieldsArea_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        MessageBox.Show("from method");
        int index = fieldsArea.IndexFromPoint(e.Location);
        string s = fieldsArea.Items[index].ToString();

        selectedFieldsArea.Items.Add(s);
    }

So I want the element from fieldsArea to be copied to selectedFieldsArea... The messagebox never shows and in debug I see that I never enter this method... Am I missing something here?

ps: I have drag drop implemented which works well.

UPDATE : The problem comes from the MouseDown event also being implemented. So here's my mousedown event.

private void fieldsArea_MouseDown(object sender, MouseEventArgs e)
    {
        if (fieldsArea.Items.Count == 0)
            return;
        int index = fieldsArea.IndexFromPoint(e.Location);
        string s = fieldsArea.Items[index].ToString();
        DragDropEffects dde1 = DoDragDrop(s,
            DragDropEffects.All);
    }

ps: I have drag drop implemented which works well.

That means probably that you have registered a MouseDown event, which interfere with the MouseDoubleclick .

Just for testing purpose, try to delete the Drag&Drop implementation ( unregister the MouseDown event) and then the MouseDoubleclick should work.

Make sure you don't have other Mouse event like MouseClick MouseDown event registered, which could interfere with MouseDoubleclick event.

Update :

Add following code in your MouseDown event handler, you can check if it is a double-click first.

if(e.Clicks>1)
{
   int index = fieldsArea.IndexFromPoint(e.Location);
   string s = fieldsArea.Items[index].ToString();
   selectedFieldsArea.Items.Add(s); 
}

so here is your new handler:

private void fieldsArea_MouseDown(object sender, MouseEventArgs e)
{
  if (fieldsArea.Items.Count == 0)
            return;
  int index = fieldsArea.IndexFromPoint(e.Location);
  string s = fieldsArea.Items[index].ToString();

  if(e.Clicks>1)
  {          
       selectedFieldsArea.Items.Add(s); 
  }
  else
  {
        DragDropEffects dde1 = DoDragDrop(s,
        DragDropEffects.All);
  }
}

I believe you may have either "MouseClick/MouseDown" event or "SelectedIndexChanged" event, these events resist to get fire of "MouseDoubleclick" event, so you need to handle them properly. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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