0

这是新控件添加代码部分:

        for (int i = 0; i < 1; i++)
        {
            var newLabel = new Label();
            newLabel.Location = new Point(x, y);
            newLabel.Size = new System.Drawing.Size(25, 25);

            newLabel.Name = "lbl" + realpocsed.ToString();

            if (value2== "Value2")
            {
                newLabel.MouseMove += new MouseEventHandler(this.MyControl_MouseMove);
                newLabel.MouseDown += new MouseEventHandler(this.MyControl_MouseDown);
            }
            if (value== "Value1")
            {
                newLabel.MouseDown += new MouseEventHandler(this.MyControl_MouseHover);

            }

            panel1.Controls.Add(newLabel);

事件MouseHover如下所示:

        private void MyControl_MouseHover(object sender, MouseEventArgs e)
    {
        ToolTip ToolTip1 = new ToolTip();
        ToolTip1.ShowAlways = true;
        ToolTip1.Show("t", X); // X should be name of the newly created label
    }

请问有没有办法用 name"lbl" + realpocsed.ToString()而不是分配新创建的X

感谢您的时间。

4

3 回答 3

2

您正在为 MouseHover 代码分配一个 MouseDown 事件,但不确定您是否想要这样。

它应该如下所示:

newLabel.MouseHover += MyControl_MouseHover;

对于您的 X,您必须投射发件人:

ToolTip1.Show("Hover Message Here", (Label)sender);
于 2013-10-07T14:36:24.240 回答
1

您不能通过在字符串中创建其名称来访问任何对象。简单的方法是传递对象引用:

ToolTip1.Show("t", (sender as Label));
于 2013-10-07T14:40:39.330 回答
0

内部for

newLabel.MouseHover += delegate (object sender, EventArgs e)
{
    ToolTip ToolTip1 = new ToolTip();
    ToolTip1.ShowAlways = true;
    ToolTip1.Show("t", newLabel);
};

更新:

if (value == "Value1")
{
    newLabel.MouseHover += delegate (object sender, EventArgs e)
    {
        ToolTip ToolTip1 = new ToolTip();
        ToolTip1.ShowAlways = true;
        ToolTip1.Show("t", newLabel);
    };
}
于 2013-10-07T14:44:50.963 回答