1

在我的 onClick 事件处理程序上,我正在做类似的事情:

private void myToolStripItem_Click(object sender, EventArgs e)
    {
        Form form = (Form)(((taskBarToolStripMenuItem)sender).formReference);

        if (form != this.ActiveMdiChild)
        {
            if (!form.Visible)
                form.Show();
            form.Activate();

            ((taskBarToolStripMenuItem)sender).isSelected = true; //set it to true, so the renderer will draw them differently
        }
    }

taskBarToolStripMenuItem 是 ToolStripMenuItem 的子类。当我到达我的自定义 ToolStripSystemRenderer 时,我确定将其“isSelected”属性更改为 true 的项目仍然保留为 false。这发生在其他属性上,我尝试了标签、文本等……为什么?谢谢

4

1 回答 1

0

What you're trying to do is to cast the sender to an instance of the ToolStripMenuItem.
Try casting to the type instead like so.

((ToolStripMenuItem)sender).Selected = true;

If you're going to use it in a number of places, you can set it to a temporary variable to avoid unboxing every time.

private void myToolStripItem_Click(object sender, EventArgs e)
{
    var item = (ToolStripMenuItem)sender; // temp variable
    Form form = (Form)(item.formReference);

    if (form != this.ActiveMdiChild)
    {
        if (!form.Visible)
            form.Show();
        form.Activate();

        item.Selected = true; //set it to true, so the renderer will draw them differently
    }
}
于 2012-11-05T11:06:38.653 回答