如何动态添加表单... [英] How to Add forms dynamically...
问题描述
嗨...
我要动态添加表单...我正在开发一个应用程序,该应用程序可以在PC上获取COM端口的名称.因此,对于每个COM端口,我想要一个单独的形式&我想动态地执行此操作.
为了使COM出现在我的PC上,我正在使用这条线...现在我要为每一个创建单独的表单名称与COM端口名称相同的COM端口.在这种情况下,我在变量 COMPORTNAME 中获得了所有命令名称,因此我必须使用相同的名称来创建一个表单
foreach (SerialPort.GetPortNames()中的字符串 COMPORTNAME
{
//在运行我的应用程序时,应该动态创建表单 >
}
有人可以帮我吗……
谢谢
Vinay
Vinay,
您可以在设计时&在rutime时,您会为每个命令加载该表单的新实例.
要尝试的是,只需在解决方案中添加一个表单即可(不添加任何代码).默认情况下,它将称为Form1.然后,使用以下代码:
foreach (字符串 COMPORTNAME in 中.GetPortNames()){
新 Form1();
frm.Show();
Hi...
I want to add Forms dynamically...i'm developing an application which gets names of COM ports present on a PC. So for each COM port i want one seperate form & i want to do this dynamically.
For getting COM present on my PC i am using this line...now in this i want to create seperate forms for each COM port with the names same as COM port names. In this i am getting all my comports names in the variable COMPORTNAME so with the same name i've to create a Form
foreach (string COMPORTNAME in SerialPort.GetPortNames()){
//Here form should get created dynamically when i run my application
}
can anybody help me in this...
Thanks,
Vinay
解决方案Hi Vinay,
You can create one form in designtime & at rutime you load an new instance of that form for each comport.
To try is, just add a form to your solution (don't add any code). It will be called Form1 by default. Then, use the following code:
foreach (string COMPORTNAME in SerialPort.GetPortNames()) {
Form1 frm = new Form1();
frm.Text = COMPORTNAME;
frm.Show();
}
This will open one instance of form one for each comport and set the caption of the form to the name of the comport.
Hope this helps,
这篇关于如何动态添加表单...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!