0

rightT is a timer which is supposed to change the image of the object (which inherits from PictureBox) when the code runs (after I turned on the timer) the image changes only once and never changes again anyone knows why it happens?

rightT.Tick += (EventHandler)delegate
{
    this.Location = new Point(this.Location.X + _movementSize, 
    this.Location.Y); // moves the character
    this.Image = this.rightState++ % 2 == 0 ? Properties.Resources.MarioAnimation2 : Properties.Resources.MarioAnimation1; // changes the photo
    this.Refresh();
};
Infinity
  • 41
  • 4
  • 2
    it won't even change if you press `ok` on the messagebox? – EpicKip Aug 02 '17 at 09:43
  • `Refresh()` calls `Invalidate()` and then `Update()`, I think you need to call `Update()` directly. – ChrisB Aug 02 '17 at 09:48
  • Update() doesnt work too – Infinity Aug 02 '17 at 09:53
  • That's becouse your code stays on the line MessageBox.Show("aa"); and waits it to be finished, then it waits for user to close this messagebox, and only then code will call this.Refresh(); – KamikyIT Aug 02 '17 at 10:06
  • what i meant was that without this MessageBox line, the image wont change – Infinity Aug 02 '17 at 10:08
  • 1
    Further to your issue, you shouldn't assign an event handler using `+= (EventHandler)delegate{ }` as you won't be able to remove it. (Once you get it working). It could also cause other issues. – Stuart Aug 02 '17 at 10:20
  • i assign it only once – Infinity Aug 02 '17 at 10:34
  • You'd probably be better off doing that work in the control's `Paint` event and then setting variables you can react to there. Then your `Tick` simply calls `Update`. – DonBoitnott Aug 02 '17 at 11:04
  • When you change the Image you should never need to call Invalidate or any other similar method. You do however need to let the UI thread work and not block it. You should also use the debugger to make sure the tick is or is not called and also check the value of rightState. – TaW Aug 02 '17 at 14:03

1 Answers1

-1

I think Application.DoEvents(); is better suited for forcing the gui to update. you can place it anywhere you like and as much as you like.

real_yggdrasil
  • 1,213
  • 4
  • 14
  • 27