In the development of Windows windows, a common scene is to dynamically access control according to the control name. This is very useful when a programming control or a complex interface is generated.
Question:
I have a TOOLSTRIPMENUITEM named "MyMenu". How to access it by programming, just like it is a member of the window?
Answer:
To access the Windows window control according to the name, you can use Control.ControlCollection.find method. This method accepts a string parameter, indicating the name of the control to be found.
Here, we first convert the results of Control.ControlCollection.find to the Toolstridonuitem we are looking for. We then access the first element in the array, because the Find method returns a matching array of matching controls.Example
<code class="language-csharp">ToolStripMenuItem myMenu = (ToolStripMenuItem)this.Controls.Find("myMenu", true)[0];</code>
The following code fragments demonstrate how to dynamically generate ToolStripMenuitems from the XML file and access them according to the names they generate:
In this example, you can access and operate MyMenu ToolStripMenuitem, as if it is a direct definition member of the window.
The above is the detailed content of How to Access Dynamically Generated Windows Forms Controls by Name in C#?. For more information, please follow other related articles on the PHP Chinese website!