开发者

C# dynamic object creation/modification?

开发者 https://www.devze.com 2023-01-21 18:10 出处:网络
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,

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 g开发者_C百科etElementById('image1').style.color = #ffffff;

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


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?


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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消