简体   繁体   English

从Mousehover(C#VS)获取网格中按钮的位置

[英]Getting location of button in grid from Mousehover (C# VS)

in school I'm currently working on a game of battleships for AP Comp Sci and I have been trying to get it so that during the ship placing stage a picturebox (a ship) changes location to that of whichever button the mouse happens to be hovering over. 在学校里,我目前正在为AP Comp Sci制作战舰游戏,并且我一直在尝试获取它,以便在船舶放置阶段中,图片框(船舶)将位置更改为鼠标悬停的任意按钮的位置过度。 This is supposed to act like a ghost icon for where you would be placing the ship. 这应该像鬼图标一样显示您将船放置的位置。 The buttons are in an array as they are made in a grid using a 2D forloop. 这些按钮位于阵列中,就像它们是使用2D forloop在网格中制成的一样。

I couldn't figure out how to make MouseHover work with getting a buttons location from an array of buttons. 我不知道如何使MouseHover通过从按钮数组中获取按钮位置来工作。 This is mostly because I don't know how to give the xy values for the buttons location in the array to the MouseHover method. 这主要是因为我不知道如何将数组中按钮位置的xy值提供给MouseHover方法。

I tried using a timer that checked each button in the array for whether it had focus and it changed the location of the picturebox to that button successfully: 我尝试使用计时器检查数组中的每个按钮是否具有焦点,并成功将图片框的位置更改为该按钮:

private void MouseXYCheckTimer_Tick_1(object sender, EventArgs e)
    {
        for (int x = 0; x < 15; x++)
        {
            for (int y = 0; y < 15; y++)
            {
                if (b[x, y].Focused)
                {
                    ShipImage1.Location = b[x, y].Location;
                }
            }

        }
    }

however this required clicking the button to give it focus (thus placing the ship), defeating the purpose. 但是,这需要单击按钮以使其具有焦点(从而放置飞船),从而破坏了目标。

I've looked quite extensively at different posts here on MouseHover and I still cant get it to work for my problem, help would be greatly appreciated. 我已经在MouseHover上的不同文章中进行了广泛的研究,但仍然无法解决我的问题,我们将不胜感激。 Thanks! 谢谢!

There are built in events for handling MouseEnter 有用于处理MouseEnter的内置事件

// Wire up the MouseEnter event when creating your buttons
button.MouseEnter += button_MouseEnter;

// Method that gets called
private void button_MouseEnter(object sender, EventArgs e)
{
    var button = sender as Button;
    ShipImage1.Location = button.Location;
}

If I understand correctly, and assuming you are using Winforms, you just need to subscribe your picture position logic to the MouseMove event of each one of your buttons. 如果我理解正确,并且假设您使用的是Winforms,则只需将图片位置逻辑订阅到每个按钮的MouseMove事件。 So try the following: 因此,请尝试以下操作:

Define this method in your class: 在您的课程中定义此方法:

private void button_MouseMove(object sender, MouseEventArgs e)
{
    ShipImage1.Location = e.Location;
}

Then in the logic responsible for creating the instance of each button stored into your b array, subscribe that method to the MouseMove event: 然后在负责创建存储在b数组中的每个按钮的实例的逻辑中,将该方法订阅MouseMove事件:

...
for (int x = 0; x < 15; x++)
{
    for (int y = 0; y < 15; y++)
    {
        var myButton = new Button();

        myButton.MouseMove += button_MouseMove;

        // More awesome stuff around myButton...

        b[x, y] = myButton;
    }
}
...

Also if needed, you can get the current screen mouse coordinates from the Form.MousePosition static property. 另外,如果需要,您可以从Form.MousePosition静态属性中获取当前屏幕鼠标坐标。

Good luck! 祝好运!

EDIT Or use instead the MouseEnter event as indicated by @Jerry in the other answer, that should work better. 编辑或使用@Jerry在另一个答案中指示的MouseEnter事件代替,它应该更好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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