Question

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);
        }
Was it helpful?

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;

OTHER TIPS

Just make sure to unwire the events with -= if you also remove the ListViews dynamically.

To answer the other half of your question, you can use a single handler for any event, from any source, that has the handler's signature. In the body of the handler, you just have to check the sender argument to determine which control raised the event.

You need a way to tell one control from a different one of the same class, however. One way to do this is to make sure to set the Name property on each control when you create it; e.g., newListView.Name = "FilesListView".

Then, before you do anything else in your event handler, check the sender.

private void listView_DragDrop(object sender, DragEventArgs e) {
    ListView sendingListView = sender as ListView;
    if(sendingListView == null) {
        // Sender wasn't a ListView.  (But bear in mind it could be any class of
        // control that you've wired to this handler, so check those classes if
        // need be.)
        return;
    }
    switch(sendingListView.Name) {
        case "FilesListView":
            // do stuff for a dropped file
            break;
        case "TextListView":
            // do stuff for dropped text
            break;
        .....
    }  
}     
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top