this is my first post here. First off, some background, I'm new to C# and am literally in my teething stages. I'm a chemical engineer and mostly have experience with languages like MATLAB and Mathematica, but i have always liked programming and have decided to learn C# to create some user friendly interfaces for some programs I have been using. Note that i am using windows forms and not WPF.
What i would like to do is have a main menu screen linking to various forms. Now to make it look nicer, what i want to do is this; when i hover over a picturebox(the picture is of a button) in the main window i would like for the button to 'grow' a little bit, and then when i leave it should 'shrink' to its original size. So far my method has been to try and load a gif of this growth animation on the mouseEnter event and then load a shrink animation on the mouseLeave, but this just loops the respective gif over and over. How can i get the gif to play once only?
i tried loading the frames sequentially with a thread sleep in between them as well but all i see when i do this is the last image i try to load. here's an example of the code used for this method, where i try to show one image, and then show another after 0.1 seconds
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
((PictureBox)sender).Image =Image.FromFile("C:/Users/michael/Desktop/131.bmp");
Thread.Sleep(100);
((PictureBox)sender).Image = Image.FromFile("C:/Users/michael/Desktop/131a.bmp");
}
Also is there a way to do this without a gif, such as by using a for loop to increase the size of the button or picturebox?
EDIT 1: where do i put the timer stop in so that when i start the second animation, the first one stops?
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
Timer timeraf = new Timer();
timeraf.Interval = 10;
timeraf.Tick += new EventHandler(timerAfwd_Tick);
timeraf.Start();
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
Timer timerab = new Timer();
timerab.Interval = 10;
timerab.Tick += new EventHandler(timerAbwd_Tick);
timerab.Start();
}
If you make your main thread sleep, I'm pretty sure it both locks out user input and it blocks the thread that handles painting. So your image can't paint any animations. Try using a timer like this:
EDIT: Changed code to add a animation when you hover over the PictureBox. The animation will stay on the final frame while you stay hovered. When the mouse leaves, it resets to the 1st frame. You can use any number of frames you want. You should also handle MouseDown and MouseUp, and create 1 more image to show the depressed or "down" state.