-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,030
  • 21
  • 100
  • 124
ikel
  • 1,620
  • 5
  • 29
  • 54

2 Answers2

0

Try it like this:

Control[] controls =  Controls.Find("listView1", true);
ListView control = controls[0] as ListView;
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Kurubaran
  • 8,342
  • 5
  • 37
  • 63
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
  • 52,990
  • 9
  • 92
  • 107