如何确定EventHandler中的Control属于哪个类?
这是我的类,其中有各种属性以及面板控件:
public class Square
{
private Panel _pSquare;
public Panel PSquare
{
get { return _pSquare; }
set { _pSquare = value; }
}
....
这是表单加载事件处理程序,其中创建了一堆方形对象:
private void Form1_Load(object sender, EventArgs e)
{
for (var n = 0; n < gridSize; n++)
{
for (var m = 0; m < gridSize; m++)
{
Square squareboard = new Square(n, m);
squareboard.PSquare.Click += squareEvent;
...
当用户单击面板时,调用 pSquare_Click 事件处理程序,以便该部分工作。
private void pSquare_Click(object sender, EventArgs e)
{
我遇到的问题是:如何在此 EventHandler 中访问类 Square 的属性?
This is my class, where I have various properties and also the Panel control:
public class Square
{
private Panel _pSquare;
public Panel PSquare
{
get { return _pSquare; }
set { _pSquare = value; }
}
....
This is the Form Load EventHandler, where a bunch of Square Objects are created:
private void Form1_Load(object sender, EventArgs e)
{
for (var n = 0; n < gridSize; n++)
{
for (var m = 0; m < gridSize; m++)
{
Square squareboard = new Square(n, m);
squareboard.PSquare.Click += squareEvent;
...
When the user clicks on a Panel, the pSquare_Click EventHandler is called, so that part works.
private void pSquare_Click(object sender, EventArgs e)
{
The problem I have is: how to access the properties of class Square in this EventHandler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每当创建面板时,请使用
panel.Tag
将每个面板与其方块链接:Whenever create a panel, Use the
panel.Tag
to links each panel with its square:您的 Square 类可能如下所示:
在您的表单中,您可以首先制作面板,然后通过循环面板生成正方形集合:
Your Square class could look like this:
In your form you could make the panels first, and then generate a collection of squares by looping through the panels: