Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am creating an Audio recording where I want to display the counter time in my list box as soon as the recording gets start..eg: 0: 0:1, 0: 0:2, 0: 0:3..

The following code starts with the current computer time..eg:11:55,11:56,11:57...
can anyone please tell me how to start with the 0: 0: 0..time in my list box
C#
 private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
            dispatcherTimer.Start();
        }

private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            listBox1.Items.Add(DateTime.Now.Hour.ToString() + ":" +
            DateTime.Now.Second.ToString());

            CommandManager.InvalidateRequerySuggested();
            listBox1.Items.MoveCurrentToLast();
            listBox1.SelectedItem = listBox1.Items.CurrentItem;
            listBox1.ScrollIntoView(listBox1.Items.CurrentItem);
         }
Posted
Updated 29-Apr-11 21:18pm
v2
Comments
Sandeep Mewara 30-Apr-11 3:18am    
Always use PRE tags to format your code aprt. It makes the question readable.

1 solution

Hi,

you must iterate a Timespan and display it

C#
TimeSpan total;
TimeSpan interval;

private void Window_Loaded(object sender, RoutedEventArgs e)
{
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

interval = new TimeSpan(0,0,1);
dispatcherTimer.Interval = interval;
dispatcherTimer.Start();

}
 
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
total = total.Add(interval);
listBox1.Items.Add(total.ToString()); 
CommandManager.InvalidateRequerySuggested();
listBox1.Items.MoveCurrentToLast();
listBox1.SelectedItem = listBox1.Items.CurrentItem;
listBox1.ScrollIntoView(listBox1.Items.CurrentItem);
} 


Hope it helps
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900