如何将项目从类添加到主表单? [英] How to add items form a class to main form?

查看:95
本文介绍了如何将项目从类添加到主表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个通过循环动态生成lebel的类.但是我在将这些等级添加到MainForm时遇到问题.谁能给我一些提示该怎么做?

Hi, i have write a class which generate lebels dynamically by a loop. But i am having problem to add those lebels to the MainForm. Can anyone give me some hints how to do this?

推荐答案

this.Controls.Add(<您的label =" object =" here ="> ;);
this.Controls.Add(<your label="" object="" here="">);


Label label = new Label();
label.Text = "Some Text";
this.Controls.Add(label);


prionkor写道:
prionkor wrote:

当我在MainForm.cs中编写此代码时,它工作正常.但是当我在课堂上尝试显示错误

when i write this code in MainForm.cs it work fine. But when i try in class it shows error



因为除非该类是容器控件(从必需的基类继承),否则它将无法显示控件.




Because unless the class is a container control (inheriting from the required base class) then it will not be able to display controls.


prionkor写道:
prionkor wrote:

我还尝试了MainForm.Controls.Add(levels);

I also tried MainForm.Controls.Add(levels);



当您访问MainForm类而不是其实例时,此方法将不起作用.

您的另一个类应该引发一个事件,该事件具有一个从System.EventArgs派生的类的适当实例,该实例的属性类型为Control[]List<Control>ReadOnlyCollection<Control>.然后MainForm可以订阅您的类的事件,并通过事件将控件传递回该实例



This won''t work as you are accessing the MainForm class and not it''s instance.

Your other class should raise an event with a suitable instance of a class dreived from System.EventArgs that has a property of type Control[], List<Control> or ReadOnlyCollection<Control>. MainForm can then subscribe to your class''s event and pass the controls via the event back to that instance


这篇关于如何将项目从类添加到主表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆