简体   繁体   中英

C# Image RollOver

greetings, im trying to implement an image rollover on a collection of PictureBox (es) that are place in a tablelayoutpanel and one in each table cell.

here is my code:

HomePicBox[picBoxCount].MouseEnter += new System.EventHandler(this.PictureBox_MouseEnter);

HomePicBox[picBoxCount].MouseLeave += new System.EventHandler(this.PictureBox_MouseLeave);

==================

    private void PictureBox_MouseEnter(object sender, EventArgs e)
    {
        Point p = HomeTableLayoutPanel.PointToClient(Control.MousePosition);
        PictureBox HomeCurrentPicBox = (PictureBox)(HomeTableLayoutPanel.GetChildAtPoint(p));

        if (HomeCurrentPicBox == null)
            return;

        TableLayoutPanelCellPosition HomeCurrentPosition = HomeTableLayoutPanel.GetCellPosition(HomeCurrentPicBox);

        if (GameModel.HomeCellStatus(HomeCurrentPosition.Column, HomeCurrentPosition.Row) == Cell.cellState.Water)
        {
            HomeCurrentPicBox.Image = Properties.Resources.Scan;
            HomeCurrentPicBox.Refresh();

            gameFormToolTip.SetToolTip(HomeCurrentPicBox, GameModel.alphaCoords(HomeCurrentPosition.Column) + "," + HomeCurrentPosition.Row);

        }
    }

    private void PictureBox_MouseLeave(object sender, EventArgs e)
    {
        Point p = HomeTableLayoutPanel.PointToClient(Control.MousePosition);
        PictureBox HomeCurrentPicBox = (PictureBox)(HomeTableLayoutPanel.GetChildAtPoint(p));

        if (HomeCurrentPicBox == null)
            return;

        TableLayoutPanelCellPosition HomeCurrentPosition = HomeTableLayoutPanel.GetCellPosition(HomeCurrentPicBox);

        if (GameModel.HomeCellStatus(HomeCurrentPosition.Column, HomeCurrentPosition.Row) == Cell.cellState.Water)
        {
            HomeCurrentPicBox.Image = Properties.Resources.Water;
            HomeCurrentPicBox.Refresh();

            gameFormToolTip.SetToolTip(HomeCurrentPicBox, GameModel.alphaCoords(HomeCurrentPosition.Column) + "," + HomeCurrentPosition.Row);
        }
    }

but the rollover is not working! any ideas as to how to implement this correctly?

thanks in advance.

the following:

    private void PictureBox_MouseEnter(object sender, EventArgs e)
    {
        PictureBox HomeCurrentPicBox = ((PictureBox)(sender));
        HomeCurrentPicBox.Image = Properties.Resources.Scan;

        TableLayoutPanelCellPosition HomeCurrentPosition = HomeTableLayoutPanel.GetCellPosition(HomeCurrentPicBox);
        gameFormToolTip.SetToolTip(HomeTableLayoutPanel, GameModel.alphaCoords(HomeCurrentPosition.Column) + "," + HomeCurrentPosition.Row);
    }

    private void PictureBox_MouseLeave(object sender, EventArgs e)
    {
        PictureBox HomeCurrentPicBox = ((PictureBox)(sender));
        HomeCurrentPicBox.Image = Properties.Resources.Water;

        TableLayoutPanelCellPosition HomeCurrentPosition = HomeTableLayoutPanel.GetCellPosition(HomeCurrentPicBox);

        gameFormToolTip.SetToolTip(HomeTableLayoutPanel, GameModel.alphaCoords(HomeCurrentPosition.Column) + "," + HomeCurrentPosition.Row);
    }

it does the image rollover right but does not display the tooltip. if i state HomeCurrentPicBox instead of HomeTableLayoutPanel on the tooltip it diplays wrongly.

okay no it works i think. i had to change the AutomaticDelay value of the tooltip.

thanks everyone.

The Following should work:

    private void pictureBox1_MouseEnter(object sender, EventArgs e)
    {
        ((PictureBox)sender).ImageLocation = "Resources/logo.png";
    }

    private void pictureBox1_MouseLeave(object sender, EventArgs e)
    {
        ((PictureBox)sender).ImageLocation = "Resources/logoonly.png";
    }

Edit: Note that I am using ImageLocation to change the pictures, you can use anything you like, you can use the 'Image' property instead of the ImageLocation and assign an image to it if you wish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM