【问题标题】:How can I get the index/event of a class when I have multiple instances of that class?当我有该类的多个实例时,如何获取该类的索引/事件?
【发布时间】:2019-04-13 13:07:02
【问题描述】:

我有一个创建Label 并显示文本的类。
我还有一个Click 事件,当它触发时,Label 文本会发生变化。

我有另一个类,它将一个方法传递给第一个显示MessageBox的方法。
在主 Form 中,我正在运行一个 for 循环,该循环在随机位置创建该类的 2 个实例。

问题是,当我单击Label 时,Label 上的文本不会改变我想要的。它更改为创建的第一个 Label(类)。

我该如何改变呢?

class J1
{
    public Label texto;
    public static int a = 0;

    //Calls the Method that Creates the Label
    public void Spawn(Form form, int _X, int _Y)
    {
        LL(form, _X, _Y);
    }

    //Creates the label
    public void LL(Form form, int _X, int _Y) 
    {
        texto = new Label()
        {
            Size = new System.Drawing.Size(35, 50),
            Left = _X,
            Top = _Y,
            Text = "nova label"
        };
        texto.Click += new EventHandler(Label_Clicada);
        form.Controls.Add(texto);
    }

    void Label_Clicada(object sender, EventArgs e) //Click event when fires
    {
        J2.M(); //2nd Class that shows a MessageBox
        //Changes Text, but doenst change the one that was clicked on
        texto.Text = texto.GetType().ToString(); 
    }
}

二等(J2):

class J2
{
    public static void M()//Method that I pass to 1nd class(J1)
    {
        J1.a++;
        MessageBox.Show(J1.a.ToString());
    }
}

【问题讨论】:

  • 在点击事件中使用(sender as Label).Text = ...
  • 谢谢伙计,工作就像一个魅力!你能解释一下发件人在这种情况下做了什么吗?为什么它在我的代码中起作用?
  • sender 是生成事件的对象。 Label,在这种情况下。然后,您可以将 cast(考虑/查看对象)作为标签。由于sender 实际上是Label,因此您可以访问它的属性。您也可以将其转换为Control(sender as Control).Text,因为Label 继承自Control,它提供了.Text 属性(在System.Windows.Forms.Control 中)。
  • 这意味着sender 也可以是ButtonTextBox。全部来自控制。当您必须为具有共同(有意义的)属性的不同控件处理单个事件时,这可能很有用。 PictureBox 也派生自 Control 并具有 Click 事件,但 Text 属性对于该 Control 没有意义。如果是事实,它是隐藏的。但是如果你设置:pictureBox1.Text = "SomeString";,它会被接受的事件,虽然属性没有列出。但是,它不会产生任何效果,因为它对于该控件来说不是有意义的属性(未应用)。

标签: c# winforms class indexing


【解决方案1】:

正如我在评论解决方案中发布的那样,在点击事件中使用(sender as Label).Text

senderobject 的类型,其中包含触发事件的 object

如果我们看看这段代码是如何触发事件的:

EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
if (handler != null)
{
    handler(this, e);
}

我们可以看到hanlder(this, e); 正在传递触发它的this 对象和e 作为一些参数(事件参数)。

现在this / object 如果您将多个对象绑定到同一个事件,则传递的事件可以是任何对象。

例如,如果您执行yourButton.Click += ClickEvent;yourLabel.Click += ClickEvent,则里面的两个按钮都有上面的代码,但它们都会传递不同的this (themselves)e (events if there are any)

所以在我们的事件中我们可以这样做:

private void ClickEvent(object sender, EventArgs e)
{
    if(sender is Label)
    {
        Label l = sender as Label;
        //Do anything with label
    }
    else if(sender is Button)
    {
        Button b = sender as Button;
        //Do anything with button
    }
    else
    {
        MessageBox.Show("Unknown component");
        //Or
        throw new Exception("Unknown component");
    }
}

【讨论】:

    猜你喜欢
    • 2011-01-03
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 2023-01-19
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    相关资源
    最近更新 更多