【问题标题】:Issue adding form controls to form from another class c#问题将表单控件添加到另一个类 c# 中的表单
【发布时间】:2014-05-29 09:13:12
【问题描述】:

您好,我正在创建一个 Windows 窗体应用程序,我正在尝试创建并添加一个新控件,在这种情况下,它是来自不同类的面板,但是当我创建面板并将其添加到表单时,它没有出现吗? [注意:我可以使用 F.text("Some text"); 更改表单的文本。但是 F.Controls.Add(panel1);不起作用]。

这是我的代码:

public static void Create(Form1 F)
    {
         //this works:
         F.Text = "DEFAULT TEXT";
         Panel test = new Panel();
         test.Dock = DockStyle.Fill;
         test.BackColor = System.Drawing.Color.Black;
         test.Show();
         //this does not:
         F.Controls.Add(test);
    }

【问题讨论】:

  • 设置 Form1 的 .Text 属性是否有效?
  • 在什么情况下不起作用?
  • @FredrikMörk 我可以用 F.text("Some Text"); 设置表单的标题;

标签: c# forms controls panel


【解决方案1】:

如果您要从其他类添加控件,请使用 控制 c = fr.Controls["controlname"];

然后将其添加到您想要的表单中。 F.controls.add(c);

【讨论】:

    【解决方案2】:

    这对我来说很好用

        public Form1(){
     InitializeComponent();
                Create(this);
    
            }
    
            public static void Create(Form1 F)
            {
                //this works:
                F.Text = "DEFAULT TEXT";
                Panel test = new Panel();
                test.Dock = DockStyle.Fill;
                test.BackColor = Color.Blue;
    
                //test.Show(); this is irrelevant
                //this does not:
                F.Controls.Add(test);
    

    【讨论】:

      猜你喜欢
      • 2015-11-15
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      相关资源
      最近更新 更多