如何在C#中获取带有字符串名称的表单 [英] How to get form with string name in c#

查看:87
本文介绍了如何在C#中获取带有字符串名称的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用基于c#.net窗口的应用程序,我的问题是我想检测表单或使用字符串名称获取表单,因为存在项目要求.

假设我添加了一个名为frmMaster.cs的表单,那么如何检测字符串名称为"frmMaster"的表单.


假设我的项目中有15个表单,并且我想通过字符串名称检测特定表单.

或者我怎样才能在formcollection类中获取所有表格


所以请提供任何建议

谢谢..
Amit Tank.

Hi Everyone,

I am working with c#.net window based application and my problem is that I want to detect the form or get the form with the string name because There is requirement of project.

suppose I have added a form named frmMaster.cs then how can i detect that form with string name "frmMaster".


Suppose I have 15 forms in my project and I want to detect a particular form by string name.

Or how can i get all the forms in formcollection class


So please provide any kind of suggestion

Thank You..
Amit Tank.

推荐答案

迭代打开的表单,如下所示:

Iterate through the open forms like this:

foreach (Form form in Application.OpenForms)
{
    if (form is frmMaster)
    {
       // do something
       break;
    }
}



稍作谷歌搜索将为您节省数月的时间...



A little googling would have saved you months of time...


将myAssembly设置为System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
昏暗的类型为Type()= myAssembly.GetTypes()
将昏暗的mytype作为类型
对于每个myType输入类型
如果mytype.BaseType.FullName ="System.Windows.Forms.Form";然后
MessageBox.Show(mytype.Name)
如果(mytype.Name ="Backup"),则
MessageBox.Show("这是给你的")
退出
如果结束
如果结束
Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim types As Type() = myAssembly.GetTypes()
Dim mytype As Type
For Each myType In types
If mytype.BaseType.FullName = "System.Windows.Forms.Form" Then
MessageBox.Show(mytype.Name)
If (mytype.Name = "Backup") Then
MessageBox.Show("This is for u ")
Exit For
End If
End If




有一个名为名称"的属性,该属性检索您应该比较以识别表单的表单名称.

请让我知道您是否可以完成此任务.
Hi,

There are property named "Name" which retrieves the form name that you should compare to identify a form.

Please let me know if you could get this done.


这篇关于如何在C#中获取带有字符串名称的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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