Microsoft Developer Network >
Forums Home
>
Visual Studio Express Editions Forums
>
Visual C# Express Edition
>
How do I add controls at runtime?
How do I add controls at runtime?
- I would like to add controls at run time, but cannot get the code to work. I have the following :-
I would also like to be able to change the visibility of some images and amend the image displayed. If I use the following I am guessing that it will not work, but am unsure how to code it.int numImages = 15; string path = "file:///C:/Users/Viv/images/Blank.png"; int leftPosn; for (int i = 1; i <= numImages; i++) { Image myImage = new Image(); leftPosn = 100 + i * 50; // Set the image's properties. myImage.Name = "image" + i.ToString(); myImage.Visibility = Visibility.Visible; myImage.Width = 43; myImage.Height = 55; myImage.Margin = leftPosn,0,0,0; // this does not work myImage.Source = path; // this does not work }
Any help appreciated,string imageName; imageName = "image5"; string path2 = "file:///C:/Users/Viv/Pictures/images/Green.png" imageName.Visibility = Visibility.Visible; imageName.Source = path2;
Viv
Answers
- You can't add it directly to the Window, you need to add it to your layout control. If you didn't change it from the default it would be a Grid. However, you will to go into your Xaml and give the Grid an x:name in order to reference it from your code-behind. Once you do that you can add the control by saying Grid1.Children.Add(myImage);I would also suggest posting these types of questions in the WPF forums, you'll find more experts there.
If this answers your question, please mark the question as answered.- Marked As Answer byHarry ZhuMSFT, ModeratorFriday, November 13, 2009 2:04 AM
All Replies
You need to add the image to the controls for the form:
myForm.Controls.Add(myImage);
Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!- Thanks for the suggestion Deborah, I tried adding the following :-
Window1.Controls.Add(myImage);
to my code but there is no definition for Controls in Window1, (I accepted the default name when I created my WPF application).
Viv - You can't add it directly to the Window, you need to add it to your layout control. If you didn't change it from the default it would be a Grid. However, you will to go into your Xaml and give the Grid an x:name in order to reference it from your code-behind. Once you do that you can add the control by saying Grid1.Children.Add(myImage);I would also suggest posting these types of questions in the WPF forums, you'll find more experts there.
If this answers your question, please mark the question as answered.- Marked As Answer byHarry ZhuMSFT, ModeratorFriday, November 13, 2009 2:04 AM
You can't add it directly to the Window, you need to add it to your layout control. If you didn't change it from the default it would be a Grid. However, you will to go into your Xaml and give the Grid an x:name in order to reference it from your code-behind. Once you do that you can add the control by saying Grid1.Children.Add(myImage);
I would also suggest posting these types of questions in the WPF forums, you'll find more experts there.
If this answers your question, please mark the question as answered.
Thanks for the suggestion, I tried you code and at least it compiles and runs but I cannot see any images on the form when I run it. I will follow your suggestion to use the WPF forum.
Viv

