Get a Windows Forms control by name in C#

后端 未结 14 1519
野性不改
野性不改 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条回答
  • 2020-11-22 09:48

    Since you're generating them dynamically, keep a map between a string and the menu item, that will allow fast retrieval.

    // in class scope
    private readonly Dictionary<string, ToolStripMenuItem> _menuItemsByName = new Dictionary<string, ToolStripMenuItem>();
    
    // in your method creating items
    ToolStripMenuItem createdItem = ...
    _menuItemsByName.Add("<name here>", createdItem);
    
    // to access it
    ToolStripMenuItem menuItem = _menuItemsByName["<name here>"];
    
    0 讨论(0)
  • 2020-11-22 09:49

    You can do the following:

    private ToolStripMenuItem getToolStripMenuItemByName(string nameParam)
       {
          foreach (Control ctn in this.Controls)
             {
                if (ctn is ToolStripMenuItem)
                   {
                       if (ctn.Name = nameParam)
                          {
                             return ctn;
                          }
                    }
             }
             return null;
        }
    
    0 讨论(0)
  • 2020-11-22 09:51

    One of the best way is a single row of code like this:

    In this example we search all PictureBox by name in a form

    PictureBox[] picSample = 
                        (PictureBox)this.Controls.Find(PIC_SAMPLE_NAME, true);
    

    Most important is the second paramenter of find.

    if you are certain that the control name exists you can directly use it:

      PictureBox picSample = 
                            (PictureBox)this.Controls.Find(PIC_SAMPLE_NAME, true)[0];
    
    0 讨论(0)
  • 2020-11-22 09:52

    this.Controls.Find(name, searchAllChildren) doesn't find ToolStripItem because ToolStripItem is not a Control

      using SWF = System.Windows.Forms;
      using NUF = NUnit.Framework;
      namespace workshop.findControlTest {
         [NUF.TestFixture]
         public class FormTest {
            [NUF.Test]public void Find_menu() {
               // == prepare ==
               var fileTool = new SWF.ToolStripMenuItem();
               fileTool.Name = "fileTool";
               fileTool.Text = "File";
    
               var menuStrip = new SWF.MenuStrip();
               menuStrip.Items.Add(fileTool);
    
               var form = new SWF.Form();
               form.Controls.Add(menuStrip);
    
               // == execute ==
               var ctrl = form.Controls.Find("fileTool", true);
    
               // == not found! ==
               NUF.Assert.That(ctrl.Length, NUF.Is.EqualTo(0)); 
            }
         }
      }
    
    0 讨论(0)
  • 2020-11-22 09:52

    Using the same approach of Philip Wallace, we can do like this:

        public Control GetControlByName(Control ParentCntl, string NameToSearch)
        {
            if (ParentCntl.Name == NameToSearch)
                return ParentCntl;
    
            foreach (Control ChildCntl in ParentCntl.Controls)
            {
                Control ResultCntl = GetControlByName(ChildCntl, NameToSearch);
                if (ResultCntl != null)
                    return ResultCntl;
            }
            return null;
        }
    

    Example:

        public void doSomething() 
        {
                TextBox myTextBox = (TextBox) this.GetControlByName(this, "mytextboxname");
                myTextBox.Text = "Hello!";
        }
    

    I hope it help! :)

    0 讨论(0)
  • 2020-11-22 09:57

    Have a look at the ToolStrip.Items collection. It even has a find method available.

    0 讨论(0)
提交回复
热议问题