I think this should be working but I do not know why I try and do anything to set the current position or return the current position of a playing audio using the WMPLib inside of VS 2019 here is my code (sorry spaghetti) but basically I'm trying to make a pause function in my windows form thing
PictureOfWindowsFormApp
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine(paused);
playsound();
if(paused == true)
{
wmpPlayer.controls.currentPosition = currenttime;
paused = false;
}
}
private void button3_Click(object sender, EventArgs e)
{
double currenttime = wmpPlayer.controls.currentPosition;
wmpPlayer.controls.stop();
paused = true;
}
}
}
I've been looking for hours for different methods seeing if there were current position changes or some shit like that
Instead of trying to keep the currentPosition and setting it back, you can use the Pause() and Play() functions. Using Play(), when it is paused, will just resume where it was currently at.
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine(paused);
playsound();
wmpPlayer.controls.play();
}
private void button3_Click(object sender, EventArgs e)
{
wmpPlayer.controls.pause();
}
Related
I'm building a Flappy Bird game in C#. So far, the code works perfectly, however I added a GIF saying Game Over, and this image only shows once timer1 is stopped.
How can I achieve that?
Below is the code snippet for the GIF image (I tried both snippets of code, and they didn't work though there was no error):
private void pictureBox5_Click(object sender, EventArgs e)
{
if (timer2.Enabled)
{
pictureBox5.Show();
}
else
{
pictureBox5.Hide();
}
}
And here's the code snippet for timer2
private void timer2_Tick(object sender, EventArgs e)
{
if (!timer1.Enabled)
{
timer2.Enabled = true;
timer2.Start();
}
}
To disable Timer1, you must first enable Timer2, then disable Timer1.
private void Timer1_Tick(object sender, EventArgs e)
{
//For example, the gamer has 20 seconds
//So, we set the value of Label1.text to 20
if (int.Parse(Label1.Text) > 0)
{
int Counter = int.Parse(Label1.Text);
Counter--;
Label1.Text = Counter.ToString();
}
else
{
Timer2.Enabled = true;
Timer1.Enabled = false;
}
}
private void pictureBox4_Click(object sender, EventArgs e)
{
if (Timer2.Enabled == true)
{
pictureBox5.Show();
}
else
{
pictureBox5.Hide();
}
}
Tested in:
Visual Studio 2017
.NET Framework 4.5.2
Windows Forms
Thanks
How to use timers to trigger click button event every 3 seconds?
I'm trying to rotate 2 pictures in pictureboxes by triggering the rotate button automaticly using timer but it seems doesnt works. I never used timer before so this is my first time. Anyone know whats wrong with my code or any other code suggestion for it? Thanks
Code I'm using
private void timer1_Tick(object sender, EventArgs e)
{
rotateRightButton_Click(null, null);
pictureBox1.Refresh();
pictureBox2.Refresh();
}
private void timerStartButton_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timerStopButton_Click(object sender, EventArgs e)
{
timer1.Stop();
}
It's even possible (and more simple) with tasks
public partial class Form1 : Form
{
// variable to keep track if the timer is running.
private bool _timerRunning;
public Form1()
{
InitializeComponent();
}
private async Task StartTimer()
{
// set it to true
_timerRunning = true;
while (_timerRunning)
{
// call the rotateRightButton_Click (what you want)
rotateRightButton_Click(this, EventArgs.Empty);
pictureBox1.Refresh();
pictureBox2.Refresh();
// wait for 3 seconds (but don't block the GUI thread)
await Task.Delay(3000);
}
}
private void rotateRightButton_Click(Form1 form1, EventArgs empty)
{
// do your thing
}
private async void buttonStart_Click(object sender, EventArgs e)
{
// if it's already started, don't start it again.
if (_timerRunning)
return;
// start it.
await StartTimer();
}
private void buttonStop_Click(object sender, EventArgs e)
{
// stop it.
_timerRunning = false;
}
}
timer1.Interval = 3000; // set interval to 3 seconds and then call Time Elapsed event
timer1.Elapsed += Time_Elapsed;
//Event
private void Time_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// will be triggered in every 3 seconds
rotateRightButton_Click(null, null);
pictureBox1.Refresh();
pictureBox2.Refresh();
}
Hope this helps!
I want to hide the cursor after a certain time (if the mouse is not moving), and I want to show the cursor when I move the mouse in a picturebox. I just can't get it to work... This is what I have tried:
// this Never seem to hide the cursor
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
Cursor.Show();
tim.Stop();
tim.Start();
}
private void tim_Tick(object sender, EventArgs e)
{
Cursor.Hide();
tim.Stop();
}
-
// works but in this case I want cursor.ico to be a resource
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
Cursor.Current = Cursors.Default;
tim.Stop();
tim.Start();
}
private void tim_Tick(object sender, EventArgs e)
{
Cursor.Current = new Cursor("cursor.ico");
tim.Stop();
}
-
// Properties.Resources.cursor gives an error even though I added it to my resources
// cannot convert from 'System.Drawing.Icon' to 'System.IntPtr'
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
Cursor.Current = Cursors.Default;
tim.Stop();
tim.Start();
}
private void tim_Tick(object sender, EventArgs e)
{
Cursor.Current = new Cursor(Properties.Resources.cursor);
tim.Stop();
}
You need to have a timer and handle its Tick event. In the Tick event, check if the last movement of the mouse was before the certain time, then hide the cursor using Cursor.Hide(). Also handle MouseMove of the PictureBox and show the cursor using Cursor.Show() method.
Note: Don't forget to enable the timer and set Interval of timer to a short value, for example 300 and change duration value in the following code, for a shorter/longer inactive time:
DateTime? lastMovement;
bool hidden = false;
int duration = 2;
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
lastMovement = DateTime.Now;
if (hidden)
{
Cursor.Show();
hidden = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!lastMovement.HasValue)
return;
TimeSpan elaped = DateTime.Now - lastMovement.Value;
if (elaped >= TimeSpan.FromSeconds(duration) && !hidden)
{
Cursor.Hide();
hidden = true;
}
}
Basically in the code below where i highlight, i would like to know how to use drawBricks method in the timer tick when that button(btnDisplayBricks) is pressed. Because i am using a timer tick and picturebox for paper drawings etc i cannot just simply call the method from within the button click event because the paper then clears in the timer only allowing me to display the bricks before timer1 starts any ideas.
private void timer1_Tick(object sender, EventArgs e)
{
paper.Clear(Color.LightSteelBlue);
DrawBall();
MoveBall();
DrawBat(paper);
if (btnDisplayBricks_Click[0] = true) ///code here problem
//then call method
DrawBricks(paper);
private void btnDisplayBricks_Click(object sender, EventArgs e)
{
DrawBricks(paper);
}
}
}
problem is in your equation, you should use == instead of =
if (btnDisplayBricks_Click[0] == true)
also move methodbtnDisplayBricks_Click outside of timer1_Tick
private bool buttonClicked = false;
private void timer1_Tick(object sender, EventArgs e)
{
paper.Clear(Color.LightSteelBlue);
DrawBall();
MoveBall();
DrawBat(paper);
if (buttonClicked)
{
DrawBricks(paper);
// maybe you want to set buttonclicked to false again, but specs are not clear to me
// buttonClicked = false;
}
}
private void btnDisplayBricks_Click(object sender, EventArgs e)
{
DrawBricks(paper);
buttonClicked = true;
}
I want to use MediaElement to play music, and when music played to some position, do some action. The code is like this:
private void button1_Click(object sender, RoutedEventArgs e)
{
mediaElement1.Play();
game_pose_poller.RunWorkerAsync(); // game_pose_poller is a BackgroundWorker object
button1.IsEnabled = false;
}
private void game_pose_poller_DoWork(object sender, DoWorkEventArgs e)
{
while(true)
{
if (mediaElement1.Position >= sometime)
{
// do something
However I found the program did nothing at all. When debugging I found that mediaElement1.Position is always zero. Why is it always zero even after Play() called? mediaElement1.Source is an mp3 file which included as resource in project, and LoadedBehavior is Manual(or the Play() raise exception).
I've made something similar some times ago using a DispatchTimer instead of a BackgroundWorker. This is how I've got it to work:
private DispatcherTimer positionTimer;
private void button1_Click(object sender, RoutedEventArgs e)
{
// Create a timer that will check your condition (every second for example)
positionTimer= new DispatcherTimer();
positionTimer.Interval = TimeSpan.FromSeconds(1);
positionTimer.Tick += new EventHandler(positionTimerTick);
positionTimer.Start();
mediaElement1.Play();
button1.IsEnabled = false;
}
void positionTimerTick(object sender, EventArgs e)
{
if (mediaElement1.Position.TotalSeconds >= sometime)
{
// do something
}
}