tip2tail
tip2tail

Reputation: 463

Event when finished selecting items?

I have a ListView control that is displaying a list of files in "Details" mode. I will allow the users to select one or a number of these files at the same time.

I have an action I want to carry out on the selected files, however as it seems logical to me that I only initiate this action once I know what files are selected.

To clarify:

User selects one file - onSelectionFinished is fired and doThisAction(selectedFile[0]) can proceed.

User selects multiple files - onSelectionFinished is fired and doThisAction(selectedFile[0]) can proceed follwed by doThisAction(selectedFile[1]) etc...

I have tried using SelectedIndexChanged but when the user selects eg 3 files, my action routine is fired 6 times: Index 0, Indices 0, 1 and then Indices 0,1,2 - a very inefficient program!

Upvotes: 1

Views: 162

Answers (3)

terrybozzio
terrybozzio

Reputation: 4532

Dont set the selection event instead let user select items needed and place the listview mouseclick event and check if its the right mousebutton,and inside do the work

        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                //do work here
            }
        }

you can add a contextmenu to the listview so when user right-clicks it will add more flexibility(with more options on what to do to that selection):

        MenuItem menuItem1 = new MenuItem("&Copy");
        MenuItem menuItem2 = new MenuItem("&Delete");
        contextMenu1.MenuItems.AddRange(new MenuItem[] { menuItem1, menuItem2 });
        listView1.ContextMenu = contextMenu1;

Upvotes: 0

user1914530
user1914530

Reputation:

You could throttle the SelectedIndexChanged event. If the user has not changed their selection in a certain time period then assume they are done and call your method. See here for an example.

However, it may be better for you to let the user decide when he/she is done as described by @Paul Sasik by means of a button click.

Upvotes: 1

Paul Sasik
Paul Sasik

Reputation: 81537

If you allow the user to select multiple files then you're not going to know when the user is done selecting and you certainly don't went to run the operation with every selection change.

Instead of trying to react to selection events you should have a button (or some other control) that runs the operation(s) on the items selected in the list view. Only the user knows when he/she is done and will tell you.

Upvotes: 4

Related Questions