David Mszrs
David Mszrs

Reputation: 13

How to implement a timer which calls a button's event in WPF C#?

As I understand, the DispatcherTimer's Tick event needs to be called with EventArgs, whereas the same event calls the action of some buttons, which need to be called with RoutedEventArgs. Obviously, I cannot call the buttons' events directly. My question is: is there any way to implement this, by all means?

private void song_timer_Tick(object sender, EventArgs e)
    {
        if (playing == true)
        {
            lbl_curent_time.Text = audio_device.audioFileReader.CurrentTime.ToString();

            if (music_state_progress.Value < music_state_progress.Maximum)
            {
                music_state_progress.Value = music_state_progress.Value + 1;
                taskbar.SetProgressValue(Convert.ToInt32(music_state_progress.Value), Convert.ToInt32(music_state_progress.Maximum));
            }

            if (lbl_curent_time.Text == audio_device.audioFileReader.TotalTime.ToString())
                if (music_list.SelectedIndex < music_list.Items.Count - 1)
                {
                    btn_next_Click(sender, e);
                    music_list_DoubleClick(sender, e);
                }
                else if (repeat_checkbox.IsChecked == true)
                {
                    music_list.SelectedIndex = 0;
                    music_list_DoubleClick(sender, e);
                }
                else
                {
                    btn_stop_Click(sender, e);
                    lbl_curent_time.Text = "00:00:00.0000000";
                    lbl_max_time.Text = "00:00:00.0000000";
                }
        }
    }

Upvotes: 0

Views: 515

Answers (2)

Adam H
Adam H

Reputation: 551

Replace:

btn_stop_Click(sender, e);

with:

btn_stop.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Primitives.ButtonBase.ClickEvent));

Upvotes: 1

coolerfarmer
coolerfarmer

Reputation: 385

ButtonAutomationPeer peer =
  new ButtonAutomationPeer( someButton );
IInvokeProvider invokeProv =
  peer.GetPattern( PatternInterface.Invoke )
  as IInvokeProvider;
invokeProv.Invoke();

Taken from HERE

Upvotes: 1

Related Questions