• 0

[C#] Switching Bitmaps on Mouse Click


Question

Basically, I have a string array that I'll use as the filenames of images embedded in my application. For example, file[0] = "image1.png", file[1] = "image2.png", etc.

I'm trying to make it to where when the user clicks the mouse, it will switch images. However, so far I have had no luck. It will switch images once, and only once. Also, I confirmed with a MessageBox that pindex is successfully incrementing.

This is what I've tried so far:

int pindex = 0;
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
     pictureBox1.Image = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("images." + file[pindex]));
     pindex++;
}

Any ideas?

Thanks!

-Koto

Link to comment
https://www.neowin.net/forum/topic/900024-c-switching-bitmaps-on-mouse-click/
Share on other sites

5 answers to this question

Recommended Posts

  • 0
  On 11/05/2010 at 22:35, luke_smily_face said:

Excellent smile.gif Just out of curiosity do you mind sharing what the bug was?

Sure. I was actually trying to randomize the images displayed, but I was using the randomize function wrong, so it randomized once and kept with that number every time. So it was actually changing images correctly, it just happened to change to the same image. I originally opted not to mention the randomize function and other complexities of my program to keep my question simple.

To make it work now I have a separate function to randomize a number.

private static readonly Random randN = new Random();
private static readonly object syncLock = new object();

private int randNumber()
{
     lock (syncLock)
     {
          return randN.Next(72);
     }
}

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.