Click here to Skip to main content
15,911,762 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi everybody,

I am a newbie with c# and have been trying to implement a very basic dynamic programming of a button.
I googled to look for a solution, but I keep obtaining this error:
'System.Windows.Controls.WrapPanel' does not contain a definition for 'Controls' and no extension method 'Controls' accepting a first argument...

I cannot figure out how to have the button showing up on my window without using this Controls.Add() function.
What am I missing?

Thanks for your help!

C#
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Simple_Button_Feedback
{
    public partial class MainWindow : Window
    {   
        public MainWindow()
        {
            CreateButton();

            InitializeComponent();
        }

        private void CreateButton()
        {

            Button b = new Button();
            b.Content = "I'm a silly Button";
            b.Click += new RoutedEventHandler(this.ButtonClick);
            b.Height = 25;
            b.Width = 100;
      //      b.Location = new Point(50,50);

            LinearGradientBrush lgb = new LinearGradientBrush();
            lgb.GradientStops.Add(new GradientStop(Colors.Yellow, 0));
            lgb.GradientStops.Add(new GradientStop(Colors.Green, 1));
            b.Background = lgb;
 
      //          this.Controls.Add(b);
      //   A <WrapPanel> named "Panel1" is defined in the .xaml file 
            Panel1.Controls.Add(b);
        }

        protected void ButtonClick(object sender, EventArgs e)
        {
            MessageBox.Show("Click me again.  That felt good.");
        }

    }
}
Posted
Comments
[no name] 30-Mar-12 14:43pm    
Try panel1.Children.Add
Member 8771971 30-Mar-12 15:30pm    
The message error does disappear, but when I debug/run the program, I now have following alert: "targetinvocationexception was unhandled"
Is there a specific way I have to define the <WrapPanel name="Panel1"></WrapPanel> to make it work?
[no name] 30-Mar-12 15:36pm    
Yes. You are getting that error because your panel does not exist yet. You have to initialize your components first. InitializeComponent(); CreateButton();
Member 8771971 30-Mar-12 15:46pm    
Oh great - Thanks a lot!

Does InitializeComponent() mean that the .xaml.cs file is looking for what has been defined in the .xaml file? I am confused, why changing these 2 lines of code is so important.
[no name] 30-Mar-12 15:50pm    
The short answer is that that is the method and parses the xaml and instantiates your window and controls. At the time you was trying to create the button, the window and panel did not exist.

Not sure why your solution was deleted but for future reference:

C#
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Simple_Button_Feedback
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {      
            InitializeComponent();
            CreateButton();
        }

        private void CreateButton()
        {

            Button b = new Button();
            b.Content = "I'm a silly Button";
            b.Click += new RoutedEventHandler(this.ButtonClick);
            b.Height = 25;
            b.Width = 100;
      //      b.Location = new Point(50,50);

            LinearGradientBrush lgb = new LinearGradientBrush();
            lgb.GradientStops.Add(new GradientStop(Colors.Yellow, 0));
            lgb.GradientStops.Add(new GradientStop(Colors.Green, 1));
            b.Background = lgb;

      //          this.Controls.Add(b);
      //   A <WrapPanel> named "Panel1" is defined in the .xaml file
            Panel1.Children.Add(b);
        }

        protected void ButtonClick(object sender, EventArgs e)
        {
            MessageBox.Show("Click me again.  That felt good.");
        }

    }
}
 
Share this answer
 
you can use this code to add button dynamically.
C#
private void CreateButton()
{
    Button Button1 = new Button();
    Button1.Name = "Button1";// this is useful to identify the object
    Button1.Location = new Point(25, 25);
    this.Controls.Add(Button1);
}
 
Share this answer
 
Comments
Member 8771971 30-Mar-12 15:32pm    
Thanks for your help! But my problem keeps occuring: "Controls" is underlined and signaled as an error by Visual Studio...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900