2

I can successfully create objects dynamically and name them dynamically. Example: click on a canvas to create an image named 'image1', click somewhere else, and create 'image2' etc. But after that, what if I want to change an attribute based on the name? In my javascript days I would getElementById('image1').style.color = #ffffff;

What about in c#? where is the 'getElementById()' so to speak?

Phil
  • 87
  • 6

2 Answers2

2

In Winforms you can use either

Control c = Form1.Controls.Find("image1", true);

or

int i = Form1.Controls.IndexOfKey("image1")
Control c = Form1.Controls[i];

For WPF things are different, this SO question looks like it might be helpful in that case: How can I find WPF controls by name or type?

Community
  • 1
  • 1
WildCrustacean
  • 5,896
  • 2
  • 31
  • 42
  • Will this work in wpf? Also, if I'm understanding correctly, after setting up Control c, I can then do something like c.Text = "test"; ??? – Phil Oct 25 '10 at 22:55
  • No, WPF is different, but this question may help you there: http://stackoverflow.com/questions/636383/wpf-ways-to-find-controls And yes, once you have `Control c`, you can change the text if you want. – WildCrustacean Oct 25 '10 at 22:58
0

I don't know if I am understanding you correctly.

Usually, working with .NET related technologies, objects are living inside collections. For example, in a windows form project, controls are placed in "Controls" collection (you can access it with this.Controls).

Then, how is it possible to retrieve one specific object? Depends on what you are searching. All objects have some kind of property that you can use as ID.

foreach (Control c in this.Controls)
{
    if (c.ClientID == 'ID than I am looking for')
        performsomeaction();
}
Jacob
  • 1,886
  • 2
  • 25
  • 40