-2

I have a Windows Forms form on which there are a few listview controls. Currently, if I ever need to add some changes to those listviews, I have to specify all names in my code. It is troublesome if I have to list a few names at each time. So I am wondering if there is a way to retrieve a listview control just by providing a name string. Something like:

Controls.find(name of listview)

I tried

Controls.Find(name,true)

But it returns an error. What is the reason?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ikel
  • 1,790
  • 6
  • 31
  • 61

2 Answers2

0

Try it like this:

Control[] controls =  Controls.Find("listView1", true);
ListView control = controls[0] as ListView;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
0

Your example will work, just be aware that it is case sensitive and returns a ControlCollection.

var tmp = Controls.Find("ListView1", true);

enter image description here

Mark Hall
  • 53,938
  • 9
  • 94
  • 111