得到一个ToolStripDropDownMenu的(ToolStrip的)父 [英] Get the (ToolStrip) parent of a ToolStripDropDownMenu

查看:796
本文介绍了得到一个ToolStripDropDownMenu的(ToolStrip的)父的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个自定义的 ToolStripProfessionalRenderer

举个例子,下面覆盖:

protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
{
    if(e.ToolStrip is MenuStrip)
    {
        // It never is. It's always ToolStripDropDownMenu
    }
}

我想这 OnRenderImageMargin 被称为下拉菜单,因为这是会被渲染,但是我想父的ToolStrip / 的MenuStrip / StatusStrip中引起 OnRenderImageMargin 电话。

I guess that OnRenderImageMargin is called by the drop down menu since this is what will be rendered, however I want to get the parent ToolStrip/MenuStrip/StatusStrip that caused the OnRenderImageMargin call.

这可能吗?

推荐答案

我想到了 e.ToolStrip.Parent 属性将是关键,但它总是

I thought the e.ToolStrip.Parent property would be the key, but it's always null.

一种选择是建立在构造函数中的 ToolStripProfessionalRenderer ,并传递一个引用控制。

One option is to create a constructor in your ToolStripProfessionalRenderer, and pass in a reference to the control.

class CustomRenderer : ToolStripProfessionalRenderer
{
    // All those controls derive from ToolStrip so we can use the base class here
    private ToolStrip ts;

    public CustomRenderer(ToolStrip ts)
    {
        this.ts = ts;
    }

    protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
    {
        if (ts is MenuStrip)  
        {
        }
        else if (ts is StatusStrip)
        {
        }
        else  // ts is ToolStrip
        {
        }
    }

再通,当你将它实例化一个参考:

Then pass a reference in when you instantiate it:

toolStrip1.Renderer = new CustomRenderer(toolStrip1);

statusStrip1.Renderer = new CustomRenderer(statusStrip1);


这是另一种选择,从修改这个答案

忘了构造函数和测试所有者重复,直到你得到了正确的父控件:

Forget the ctor and test the Owner repeatedly until you get the correct parent control:

protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
{
    ToolStrip owner = e.ToolStrip;
    while (owner is ToolStripDropDownMenu)
        owner = (owner as ToolStripDropDownMenu).OwnerItem.Owner;

    if (ts is MenuStrip)  
    {
    }
    else if (ts is StatusStrip)
    {
    }
    else  // ts is ToolStrip
    {
    }      
}

这篇关于得到一个ToolStripDropDownMenu的(ToolStrip的)父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆