C# TableLayoutPanel MouseLeave [duplicate]

五迷三道 提交于 2019-12-25 01:49:18

问题


greetings, i am developing a battleships clone game and i have an issue with TableLayoutPanel MouseLeave event.

first MouseMove:

    private PictureBox HomeLastPicBox = new PictureBox();

    private TableLayoutPanelCellPosition homeLastPosition = new TableLayoutPanelCellPosition(0, 0);

    private void HomeTableLayoutPanel_MouseMove(object sender, MouseEventArgs e)
    {

        PictureBox NowPicControl = (PictureBox)(HomeTableLayoutPanel.GetChildAtPoint(e.Location));

        if ((NowPicControl != null) && (NowPicControl != HomeLastPicBox))
        {

            HomeLastPicBox = (PictureBox)(HomeTableLayoutPanel.GetControlFromPosition(homeLastPosition.Column, homeLastPosition.Row));

            if (GameModel.HomeCellStatus(homeLastPosition.Column, homeLastPosition.Row) == Cell.cellState.WATER)
            {
                HomeLastPicBox.Image = Properties.Resources.water;
            }

            TableLayoutPanelCellPosition homeCurrentPosition = HomeTableLayoutPanel.GetCellPosition(NowPicControl);

            if (GameModel.HomeCellStatus(homeCurrentPosition.Column, homeCurrentPosition.Row) == Cell.cellState.WATER)
            {
                NowPicControl.Image = Properties.Resources.scan;
            }
            homeLastPosition = homeCurrentPosition;
        }
    }

this appears to function properly.

now the MouseLeave event:

     private void HomeTableLayoutPanel_MouseLeave(object sender, EventArgs e)
    {
        MessageBox.Show("col " + homeLastPosition.Column.ToString() + " row " + homeLastPosition.Row.ToString());
        if (GameModel.HomeCellStatus(homeLastPosition.Column, homeLastPosition.Row) == Cell.cellState.WATER)
        {
            HomeLastPicBox.Image = Properties.Resources.water;
            MessageBox.Show("hi");
        }
        HomeLastPicBox = new PictureBox();
    }

this is acting strange. it goes through the code and even a "HI" is displayed but the PictureBox image is not changed to water. any ideas as to why? this does not happen all the time, only from time to time.

what the above code is doing is basically scanning through the table cells and if the cell content is WATER then it updates the table cell image to SCAN and as the user moves onwards it is switching the cell image back to WATER.

hope this is enough information. please ask if more is needed.

thank you in advance.

p.s. this is attempting a javascript like mouseover and mouseexit.

i have made some progress. turns out after MouseLeave, MouseMove is called again therefore the result im experiencing.

just need to logic it in another manner i think.


回答1:


Your code for MouseLeave assumes that HomeLastPicBox is already set to the proper cell's picture box. Is it possible that HomeLastPicBox is being updated by something else? I would suggest making sure that the variable is set correctly before you assign something to its image.



来源:https://stackoverflow.com/questions/2267142/c-sharp-tablelayoutpanel-mouseleave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!