Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# C#4.0
Hai to all,
 
I have done one task but it is not proper, just you guys fine tune my task.
 
I am using Windows Application with C# Languvage.
 
I have one Listview (Listview contain 5 Items), 2 Buttons (Play, Pause)
 
Aim : My goal is when I click the Play Button the listview top first item will select and the selection will moves one by one of some particular timing interval.
 
I have done: What I have done is, Listview item will select using forloop, it will moves one by one for particular timing interval using thread.sleep(3000) method , and the same time, From listview selection change event I have to select that particular item.
 
Error :
1. But when I start Play Button listview items will select and moves one by one, But I am using messagebox (See coding below) in listview selection change event, For that only it will select one by one, Without messagebox it won't select one by one.
 
(I need without using messagebox it will select one by one)
 
2. Secondly, When I Click the Pause Button the process won't stop. It is continuous running.
 
(I need to Stop the process when I click the pause Button)
 

My Coding:
 
------------------------------- Coding Starts --------------------------------------
// Play Button Click Event
 
private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0, j = 0; i < listView1.Items.Count && j < listView1.Items.Count; j++)
            {
                if (j != 0)
                {
                    listView1.Items[j - 1].Selected = false;
                }
 
                listView1.Items[j].Selected = true;                            
 
                Thread.Sleep(3000);
 
                if (j == listView1.Items.Count - 1)
                {
                    listView1.Items[j].Selected = false;                    
                    j = -1;
 
                }
               
            }
        }
 
//List view Selection index Change coding
 
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {              
                listView1.Select();
                MessageBox.Show(listView1.SelectedIndices[0].ToString());
            }
        }
 

//Pause Button Click Event
 
private void button2_Click(object sender, EventArgs e)
        {
             //I Don't Know what coding write to Stop that process.

        }

--------------------------- Coding Finished ---------------------------
 
I Hope Every one Understand what I am asking, If any one not able to understood i am ready to explain once again. But I need the Solution.
 
Regards,
Vasanth
Posted 17-Aug-12 2:13am
Comments
Wes Aday at 17-Aug-12 8:31am
   
Stop what process? You do not have a process to stop. Your "play" is probably working like you want but you are artificially introducing a delay in there that allows you to see what is going on.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The first problem I see is that the loop through the listView1 items (in button1_Click) is happening in the UI thread! This is probably why you don't see the listView1 selection updating without the MessageBox. (Showing the MessageBox allows the UI thread to process the selection change visual changes.)
 
What I'd suggest is that button1_Click start a background/worker thread that changes the listView1 selections at the appropriate intervals. (I'm guessing from the "Play Button Click Event" comment, that you will be playing an audio or video file based on each item in the list, not simply sleeping for awhile.)
You cannot directly update the listView1 selection across threads, so you will need to use listView1.Invoke().
Inside the loop, at each iteration check to see if it is "paused", and exit the loop.
 
The thread should be held in a class field (not just local to button1_Click method) so it exists after the method exits.
The Pause button Click handler can just set the "paused" class field.
 
(By the way, your for loop declares and checks the variable i, but never uses it anywhere else!)
  Permalink  
Comments
vasanthkumarmk at 17-Aug-12 13:22pm
   
can you post the codings please?
vasanthkumarmk at 17-Aug-12 13:29pm
   
Make alteration of my code, and implement your ideas please put the coding here. I try out but it shows some errors. I know its my probs. If you give example coding i have understand. So I request give the coding what you said above solution.
Matt T Heffron at 17-Aug-12 14:32pm
   
You said you tried... What did you try?
Post your code as a new solution.
Instead of me re-implementing the whole thing, I may be able to help you with your implementation. And you'll learn more doing it yourself.
Sergey Alexandrovich Kryukov at 17-Aug-12 17:54pm
   
That is correct, my 5.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you can add a tracking flag:
 
1. Add a global flag (Boolean) to your code behind. This will default to "false".
2. In your "Play" event handler, before the for-loop, set the tracking flag to "true".
3. In the for-loop of the "Play" event handler, add a condition to only do the processing if the tracking flag is "true". If it is "False", then "break" out of the for-loop.
4. In your "Pause" event handler, set this flag to "false".
 
This will give you start-stop functionality that you are asking for.
 
The problem is, this doesn't "Pause" it.."Stops". If you want "Pause" functionality, you will need to keep track of the currently selected item and then go to that selected item when the user clicks "Play" again.
  Permalink  
Comments
vasanthkumarmk at 17-Aug-12 13:21pm
   
k i can try this one.
vasanthkumarmk at 17-Aug-12 13:33pm
   
I hope it will working, But i couldn't understand your explanation. I suggest you post the example coding what you said the above solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,656
1 Sergey Alexandrovich Kryukov 6,400
2 CPallini 5,240
3 George Jonsson 3,584
4 Gihan Liyanage 2,625


Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 17 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100