c# - How to find a programatically created button by text? -


So my program is creating a set of buttons like this:

  foreach (Var in subdir dir.GetDirectories ()) {var path = subdir.Name; Var button = new button {text = getFlavor (path) + "\ t (" + path + "), width = width, height = 35, top = y}; Click button + click the button; Controls.Add (Button); if (i.Text.Contains ("kittens") i ++;}   

I want to do something like this

 { [Button] button ("kitten"]. Click here;}   

"This continuation" is not the actual way. How do I get the references to the buttons I created according to the program Do you?

You can use the OfType & lt; button & gt; to find all the buttons in the Tanner Control, where you have added them (a panel ). The Little Bit LINQ Power gives you the right button:

  var kittenButtons = panel.Controls.OfType & gt; button & gt; (). Where (BTN => BTN test. ("Kittens"); foreach (button kitten buttons in BTN) BTN. PerformClick ();   

If you just want to click first:

  button kitten But Ton = panel.Controls.OfType & lt; Button & gt; () .FirstOrDefault (btn = & gt; btn.Text.Contains ("Kittens")); If (kittenButton! = Faucet) Kitten Button.PerformClick ();   

What is the value for this, here too there is an extension method that gives continuous control through deferred execution, which only gets the first button Allows access to or consume all under the road:

  Public stable IEnumerable & lt; T & gt; GetChildControlsRecursive & lt; T & gt; (This control root) where T: control {if (root == null) throw the new ArgumentNullException ("root"); var stack = new stack & lt; Control & gt; (); stack.Push (root); While (stack.count> gt; 0) {control parent = stack.pop (); External hair (control the parents) control (if) (if the child is t) yield returns (t) children; stack.Push (child); }} Break the yield; }   

You can now use the same code as above, for example the first matching button or all:

  var kittenButtons = this.GetChildControlsRecursive & Lt; Button & gt; (). Where (b = & gt; b. Lessons. ("Kittens"); // search is found in the first button yet button first KittenButton = Kitten Button.FirstOrDefault (); If (First Ketton! = Null) first Ketton Button, click on; // Loop All Buttons (Buttons Buttons buttons in buttons) BTN Purfclic ();    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -