creating an array of existing WinForm controls
-
how can i create an array of controls (well, pointers to existing controls that i made on my windows form) i want to be able to iterate through a collection of existing controls and modify their properties
r -€
-
how can i create an array of controls (well, pointers to existing controls that i made on my windows form) i want to be able to iterate through a collection of existing controls and modify their properties
r -€
as a side note, can i get an example of how to manage a pointer to a control like i want to have a pointer named pSelectedControl or something which will point to some text box or something but i get an error something about unsafe blocks
r -€
-
how can i create an array of controls (well, pointers to existing controls that i made on my windows form) i want to be able to iterate through a collection of existing controls and modify their properties
r -€
Hi Roman. Well, can you just iterate over the
Controls
collection for your form? -
as a side note, can i get an example of how to manage a pointer to a control like i want to have a pointer named pSelectedControl or something which will point to some text box or something but i get an error something about unsafe blocks
r -€
So, you're a former C++ developer then? ;-) First up, there's no need to create pointers as controls are
reference types
: a declarationControl ctl;
creates a reference to a control, not an actual control - it acts like a C++ pointer, in that you can change which actual object it points to, but you can't actually find out the address. Any class (created using the
class
keyword) in C# is a reference type. The pointer syntax in C# is for when you want to do some direct bit manipulation of the object - when you want to perform unsafe casting or pointer arithmetic. C# forces you to say, using anunsafe
block, when you want to do this - and you also have to pass the/unsafe
switch to the compiler. Code compiled/unsafe
can't be verified and must run with Full Trust - typically this means it must be copied to the local machine and run from there (this applies to the full Framework - the current version of the Compact Framework does not do security verification in this way). Stability. What an interesting concept. -- Chris Maunder -
how can i create an array of controls (well, pointers to existing controls that i made on my windows form) i want to be able to iterate through a collection of existing controls and modify their properties
r -€
You can either enumerate (or iterate) over the
Controls
collection property which already contains your controls that are displayed on a form, or add specific control references (as Mike mentioned, not "pointers"). If you only want to deal with controls of a specific type, for instance, then you could do something like this:foreach (Control c in Controls)
{
if (c is Button)
{
Button b = (Button)c;
b.Text = "Click me!";
}
}When you enumerate a collection, list, or any other
IEnumerable
implementation, do not change the underlying enumerable otherwise an exception will be thrown. If you must change the collection or list or whatever, then iterate (the ol'for
loop) over the collection or list instead and update your current index accordingly. There have been times when I wanted to keep a separate list or array (which is a static list, BTW) of certain controls in my form so I could deal with them in a loop as well. You could easily do something like this:Control[] controls = new Control[] {
this.textBox1,
this.textBox2,
thix.button5
};
// Later...
foreach (Control c in controls)
// ...
// -- OR --
for (int i=0; i<controls.Length; i++)
// ...Microsoft MVP, Visual C# My Articles