Get a Windows Forms control by name in C#

后端 未结 14 1699
野性不改
野性不改 2020-11-22 09:22

I have a ToolStripMenuItem called myMenu. How can I access this like so:

/* Normally, I would do: */
this.myMenu... etc.

/* But ho         


        
14条回答
  •  萌比男神i
    2020-11-22 10:05

    this.Controls["name"];
    

    This is the actual code that is ran:

    public virtual Control this[string key]
    {
        get
        {
            if (!string.IsNullOrEmpty(key))
            {
                int index = this.IndexOfKey(key);
                if (this.IsValidIndex(index))
                {
                    return this[index];
                }
            }
            return null;
        }
    }
    

    vs:

    public Control[] Find(string key, bool searchAllChildren)
    {
        if (string.IsNullOrEmpty(key))
        {
            throw new ArgumentNullException("key", SR.GetString("FindKeyMayNotBeEmptyOrNull"));
        }
        ArrayList list = this.FindInternal(key, searchAllChildren, this, new ArrayList());
        Control[] array = new Control[list.Count];
        list.CopyTo(array, 0);
        return array;
    }
    
    private ArrayList FindInternal(string key, bool searchAllChildren, Control.ControlCollection controlsToLookIn, ArrayList foundControls)
    {
        if ((controlsToLookIn == null) || (foundControls == null))
        {
            return null;
        }
        try
        {
            for (int i = 0; i < controlsToLookIn.Count; i++)
            {
                if ((controlsToLookIn[i] != null) && WindowsFormsUtils.SafeCompareStrings(controlsToLookIn[i].Name, key, true))
                {
                    foundControls.Add(controlsToLookIn[i]);
                }
            }
            if (!searchAllChildren)
            {
                return foundControls;
            }
            for (int j = 0; j < controlsToLookIn.Count; j++)
            {
                if (((controlsToLookIn[j] != null) && (controlsToLookIn[j].Controls != null)) && (controlsToLookIn[j].Controls.Count > 0))
                {
                    foundControls = this.FindInternal(key, searchAllChildren, controlsToLookIn[j].Controls, foundControls);
                }
            }
        }
        catch (Exception exception)
        {
            if (ClientUtils.IsSecurityOrCriticalException(exception))
            {
                throw;
            }
        }
        return foundControls;
    }
    

提交回复
热议问题