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?

0

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

2 Comments

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"; ???
No, WPF is different, but this question may help you there: stackoverflow.com/questions/636383/wpf-ways-to-find-controls And yes, once you have Control c, you can change the text if you want.
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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.