Search code examples
c#listviewruntimeflowpanel

flow panel with listview


I'm creating listviews in a flowpanel at run time which later will accept drag and dropped files. the reason being is i want these to act as folders so a user double clicks and gets a window displaying the contents.

i'm having difficulty setting up the events for my listviews as they are added.

how do i create some events (like MouseDoubleClick and DragDrop) dynamically for each added listview? can i create a single function for both of these events and have listview1, listview2, listviewX use it?

i have a button that is adding the listviews, which works fine. please advise, i apologize if this is too conceptual and not exact enough.

private void addNewWOButton_Click(object sender, EventArgs e)
        {
            ListView newListView = new ListView();
            newListView.AllowDrop = true;
            flowPanel.Controls.Add(newListView);
        }

Solution

  • You would have to have the routine already created in your code:

    private void listView_DragDrop(object sender, DragEventArgs e) {
      // do stuff
    }
    
    private void listView_DragEnter(object sender, DragEventArgs e) {
      // do stuff
    }
    

    and then in your routine, your wire it up:

    private void addNewWOButton_Click(object sender, EventArgs e)
    {
      ListView newListView = new ListView();
      newListView.AllowDrop = true;
      newListView.DragDrop += listView_DragDrop;
      newListView.DragEnter += listView_DragEnter;
    
      flowPanel.Controls.Add(newListView);
    }
    

    You would have to check who the "sender" is if you need to know which ListView control is firing the event.

    You can also just use a lambda function for simple things:

    newListView.DragEnter += (s, de) => de.Effect = DragDropEffects.Copy;