Ask a questionAsk a question
 

AnswerHow do I add controls at runtime?

  • Friday, November 06, 2009 5:11 PMk0065126 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I would like to add controls at run time, but cannot get the code to work.  I have the following :-

                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
                    }
    
    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.

    string imageName;
    imageName = "image5";
    string path2 = "file:///C:/Users/Viv/Pictures/images/Green.png"
    
    imageName.Visibility = Visibility.Visible;
    imageName.Source = path2;
    
    Any help appreciated,

    Viv

Answers

  • Friday, November 06, 2009 7:13 PMBigTuna99 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.

All Replies

  • Friday, November 06, 2009 5:46 PMDeborahKMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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!
  • Friday, November 06, 2009 7:05 PMk0065126 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    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
  • Friday, November 06, 2009 7:13 PMBigTuna99 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.
  • Friday, November 06, 2009 7:55 PMk0065126 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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