简体   繁体   中英

Find textbox by name in a list of textboxes

Given a list of RichTextBox , I would like to access one by its name.

So for example:

EDIT I shouldn't have used the loop in the question. I am trying to access the name without a for loop.

public RichTextBox boxFinder(List<RichTextBox> boxes)
{
    return boxes.Find("boxname")
}

The simplest method would be to use FirstOrDefalut

return boxes.FirstOrDefault(b => b.Name == "MyName");

First will throw if there is no matching element. Note that this essentially performs a foreach that breaks after an element is found under the hood.

Find is a function which takes a predicate (that is, a delegate which returns a bool). You can call find using lambda:

public RichTextBox boxFinder(List<RichTextBox> boxes)
{
    return boxes.Find(box=>box.Name == "boxname");
}

Of course wrapping it in a function like this probably doesn't buy you much.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM