简体   繁体   English

Visual Studio 2010-C#UserControl事件触发

[英]Visual Studio 2010 - C# UserControl event firing

I'm attempting to make a grid based dungeon system at the moment in Visual Studio 2010. I have a main user control which contains 64 other smaller user control objects, which I've called GridSquares, organised into an 8x8 grid. 目前,我正在尝试在Visual Studio 2010中构建基于网格的地牢系统。我有一个主用户控件,其中包含64个其他较小的用户控件对象(我称为GridSquares),组织为8x8网格。 The idea behind the grid squares is to act as potential movement spaces within the 'dungeon'. 网格正方形背后的想法是充当“地牢”内潜在的移动空间。 The problem I have at the moment is that I need to be able to call a click event on the user controls (GridSquares) themselves, which have been placed on screen so I can retrieve their coordinate (name) for comparison. 我目前遇到的问题是,我需要能够调用用户控件(GridSquares)本身的click事件,该控件已放置在屏幕上,因此我可以检索其坐标(名称)以进行比较。 However the event does not work when I call it (through clicking). 但是,当我调用该事件(通过单击)时,该事件不起作用。

I am aware that the events work when I place them within the usercontrol (GridSquare object) but I need the click even to work when the user control itself is clicked. 我知道,将事件放在用户控件(GridSquare对象)中时,这些事件就起作用了,但是当用户控件本身被单击时,甚至需要单击才能起作用。

Given that all 64 objects placed are the same type I can not work within the GridSquare class as I require the name of the user control to be returned through the event. 鉴于所有放置的64个对象都是同一类型,因此我无法在GridSquare类中工作,因为我要求通过事件返回用户控件的名称。

I hope this makes sense but please ask if I need to explain further. 我希望这是有道理的,但请问是否需要进一步解释。

Many thanks, Liam 非常感谢,利亚姆

EDIT: I'm not sure how much this will help or what code to display but the GridSpace controls have already been added to the 'dungeon' user control. 编辑:我不确定这将有多少帮助或要显示什么代码,但GridSpace控件已被添加到“地牢”用户控件中。 Then within I add all 64 to a dictionary: 然后在其中将所有64个添加到字典中:

gridSpaces.Add(gs11.Name, gs11);

Where gs11 is the name of the GridSquare. 其中gs11是GridSquare的名称。

From here I tried creating event handlers for the individual user controls on the dungeon screen, which failed to call. 从这里开始,我尝试在地牢屏幕上为单个用户控件创建事件处理程序,但调用失败。

You can use the same handler for each GridSquare and use the sender parameter to decide which one was clicked: 您可以对每个GridSquare使用相同的处理程序,并使用sender参数来确定单击了哪个:

protected void Page_Load(object sender, EventArgs e)
{
   for (int i = 0; i < 64; i++)
   {
      GridSquare square = new GridSquare();
      square.Click += new EventHandler(gridSquare_Click);
      grid.Add(gridSquare);
   }
}

void gridSquare_Click(object sender, EventArgs e)
{
   GridSquare square = (GridSquare)sender;
   // do something cool with the clicked square here
}

I think i get what your saying. 我想我明白你的意思。 Add this code to your user control: 将此代码添加到您的用户控件:

public new event EventHandler Click {
    Add {
        base.Click += value;
        foreach(Control i in Controls) {
            i.Click+=value;
        }
    }
    remove {
        base.Click -= value;
        foreach(Control i in Controls) {
            i.Click -= value;
        }
    }
    }

this will add the click event to everything in your user control, i hope i didnt make any errors, and that this helps 这会将click事件添加到用户控件中的所有内容,我希望我没有犯任何错误,这对您有所帮助

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

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