Как добавить кнопки в WinForm в среде выполнения? - PullRequest
3 голосов
/ 02 июля 2010

У меня есть следующий код:

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 )

            }
        }

Я не хочу создавать форму во время выполнения, но добавляю динамически созданные кнопки в мою текущую Winform, т.е.: GUIWevDav

Спасибо

Ответы [ 3 ]

7 голосов
/ 02 июля 2010

Просто используйте this.Controls.Add(folderButton). this это ваша форма.

6 голосов
/ 02 июля 2010

Проблема в вашем коде в том, что вы пытаетесь вызвать Controls.Add() метод для GUIWevbDav, который является типом вашей формы, и вы не можете получить Control.Add для типа, это не статический метод. Работает только в случаях.

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

}
3 голосов
/ 02 июля 2010

Вам необходимо работать с Control.Controls свойством.В Form Class Members вы можете видеть Controls свойство.

Используйте его так:

this.Controls.Add(folderButton);  // "this" is your form class object. 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...