Juan
Juan

Reputation: 118

C# control.find explanation

so i have been using the control.find for a while now, and it works like a charm. I like to understand the things that i use.

Control txt = this.Controls.Find(x + "_" + y + "_" + z, true)[0] as TextBox;

txt.Text = string.Empty;

The above is an example of one of the ways i use this method,where x, y, z are of type int (or even string sometimes), it works REALLY well with my procedurally generated controls, since i have so many of them and some methods of mine require a specific text box to be accessed.

Now MSDN - Control.Find, is mostly very straightforward with regards to this method, except for one thing, which brings me to my question.

[0] : this badboy, is where im trying to better my understanding of this method.

I'v always just left it as it is, since the method bombs out if its deleted. I would like to know at least 3 things, anything else would be a bonus, with regards to [0], firstly, what is it, why is it there and what possible useful scenarios are there for this guy?

In closing, i have no problems using this method, i use it a lot for different types of controls, this is purely a fact finding question.

Upvotes: 1

Views: 1809

Answers (2)

Mong Zhu
Mong Zhu

Reputation: 23732

what is it, why is it there

If you look at the return value: Control[] of the Find method, you see that it returns an array of all possible matches. with [0] you take the first element. You could also access the first element by using

this.Controls.Find(x + "_" + y + "_" + z, true).First() as TextBox;

EDIT: this version above is not really save. A safer version would check whether a match was really found:

Control[] asd = this.Controls.Find(x + "_" + y + "_" + z, true);

if(asd.Any())  // if you have found any elements 
{
    TextBox t = asd.First() as TextBox;
    t.Text = "Whatever";
}

If you want to get multiple Controls of the same type you could use the OfType method:

List<Button> asd = this.Controls.OfType<Button>().ToList();

This will get you all Buttons in your form.

Imaginge a case where you have 10 buttons and 4 of them have a special ending in the name(maybe for saving different formats) like:

btn_save_txt
btn_save_dat
btn_save_xml
...

If you want to search for all buttons with a similar name you could use this:

List<Button> asd = this.Controls.OfType<Button>().Where(x => x.Name.Contains("save")).ToList();

Now this would give you only the buttons that are for saving

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460108

ControlCollection.Find returns a Control[], so possibly multiple controls. The reason is that you could have multiple controls on the form with the same name.

Your second parameter is a bool which you have set to true. It indicates whether this control should be searched recursively or only in the parent control.

So your code is safe if you never have two controls with the same name.

Otherwise you should

  1. avoid duplicate names
  2. or, if it is desired, use a loop to iterate these controls instead of accesssing only the first

Note that you can't add controls with the same name via designer which detects duplicate names. But you are free to add controls with the same name programmatically.

Upvotes: 3

Related Questions