tags:

views:

56

answers:

3

Hi, I have the following Code :

public GUIWevbDav()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    try
    {
        //My XML Loading and other Code Here

        //Trying to add Buttons here
        if (DisplayNameNodes.Count > 0)
        {
            for (int i = 0; i < DisplayNameNodes.Count; i++)
            {
                Button folderButton = new Button();
                folderButton.Width = 150;
                folderButton.Height = 70;
                folderButton.ForeColor = Color.Black;
                folderButton.Text = DisplayNameNodes[i].InnerText;

                Now trying to do  GUIWevbDav.Controls.Add
                (unable to get GUIWevbDav.Controls method )

            }
        }

I dont want to create a form at run time but add the dynamically created buttons to my Current Winform i.e: GUIWevDav

Thanks

+6  A: 

Just use this.Controls.Add(folderButton). this is your form.

Petar Minchev
`this` is the right answer.
this. __curious_geek
Thanks Petar, But m curiosu to know why can't we we use the name of the form as 'this' and 'GUIWevbDav' refer to the same form.
Subhen
"this" is a keyword for accessing the current object. If you use directly "GUIWevbDav" then you are not working with instances of "GUIWevbDav" anymore and you access its static members. But "Controls" is not a static member. And you get an error of course. Read more about static here in MSDN: http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx
Petar Minchev
+2  A: 

You need to work with Control.Controls property. In Form Class Members you can see Controls property.

Use it like this :

this.Controls.Add(folderButton);  // "this" is your form class object. 
Incognito
+2  A: 

Problem in your code is that you're trying to call Controls.Add() method on GUIWevbDav which is the type of your form and you can't get Control.Add on a type, it's not a static method. It only works on instances.

for (int i = 0; i < DisplayNameNodes.Count; i++) 
{ 

    Button folderButton = new Button(); 
    folderButton.Width = 150; 
    folderButton.Height = 70; 
    folderButton.ForeColor = Color.Black; 
    folderButton.Text = DisplayNameNodes[i].InnerText; 

    //This will work and add button to your Form.
    this.Controls.Add(folderButton );

    //you can't get Control.Add on a type, it's not a static method. It only works on instances.
    //GUIWevbDav.Controls.Add

}
this. __curious_geek