如何确定EventHandler中的Control属于哪个类?

发布于 2024-11-26 06:01:07 字数 741 浏览 0 评论 0原文

这是我的类,其中有各种属性以及面板控件:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小女人ら 2024-12-03 06:01:07

每当创建面板时,请使用 panel.Tag 将每个面板与其方块链接:

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);

            squaredboard.PSquare.Tag = squareboard;
            squareboard.PSquare.Click += squareEvent;
            ...

private void pSquare_Click(object sender, EventArgs e)
{
    Panel panel = (Panel)sender;
    Square square = (Square)panel.Tag;//access to the underlying square object
}

Whenever create a panel, Use the panel.Tag to links each panel with its square:

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);

            squaredboard.PSquare.Tag = squareboard;
            squareboard.PSquare.Click += squareEvent;
            ...

private void pSquare_Click(object sender, EventArgs e)
{
    Panel panel = (Panel)sender;
    Square square = (Square)panel.Tag;//access to the underlying square object
}
司马昭之心 2024-12-03 06:01:07

您的 Square 类可能如下所示:

public class Square
{
    private readonly Panel _pSquare;

    public Square(Panel pSquare)
    {
        this._pSquare = pSquare;
        this._pSquare.Click += this._pSquare_Click;
    }

    void _pSquare_Click(object sender, EventArgs e)
    {
        this.onSquareClicked();
    }

    public event EventHandler SquareClicked;

    private void onSquareClicked()
    {
        EventHandler eventHandler = this.SquareClicked;
        if (eventHandler != null)
        {
            eventHandler(this, EventArgs.Empty);
        }
    }
}

在您的表单中,您可以首先制作面板,然后通过循环面板生成正方形集合:

    foreach (Panel panel in this.Controls.OfType<Panel>())
    {
        var square = new Square(panel);
        square.SquareClicked += this.squareClicked;
    }

Your Square class could look like this:

public class Square
{
    private readonly Panel _pSquare;

    public Square(Panel pSquare)
    {
        this._pSquare = pSquare;
        this._pSquare.Click += this._pSquare_Click;
    }

    void _pSquare_Click(object sender, EventArgs e)
    {
        this.onSquareClicked();
    }

    public event EventHandler SquareClicked;

    private void onSquareClicked()
    {
        EventHandler eventHandler = this.SquareClicked;
        if (eventHandler != null)
        {
            eventHandler(this, EventArgs.Empty);
        }
    }
}

In your form you could make the panels first, and then generate a collection of squares by looping through the panels:

    foreach (Panel panel in this.Controls.OfType<Panel>())
    {
        var square = new Square(panel);
        square.SquareClicked += this.squareClicked;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文