如何通过表单名称获取表单标题(表单文本) - winform C# [英] How to get form title (form text) by form name - winform C#

查看:152
本文介绍了如何通过表单名称获取表单标题(表单文本) - winform C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码可以在我的项目中获得所有形式的NAME。



 尝试 
{
装配程序集= Assembly.Load( ProjectMyNameSpace);
类型[] types = assemblies.GetTypes();
foreach (类型t 类型)
{
if (t.BaseType == typeof (XtraForm)| t.BaseType == typeof (XtraUserControl))
{
checklistBox.Items.Add(t.Name);
// t.Name如frm_Login,frm_UserInfo ... etc
}
}
}
catch
{
}



但是现在,我希望通过表单名称获取Form.Text。我怎么做?



http://i.stack.imgur.com/WwNdY.png

解决方案

这里的代码,使用反射,将返回指向项目中当前类型定义的指针。但是,那些类型定义与当前项目中当前在运行时使用的实例类型不同!



I我猜你真正想要的是找到所有当前打开的(正在使用的实例)XtraForm和XtraUsercontrol。



XtraForm的情况,如果您的应用程序设计正确,很容易找到所有实例。在XtraUserControl的情况下,它更复杂,因为你可以在另一个ContainerControl内的Panel中有一个XtraUserControl实例,等等。



如果我有一个活动的WinForms应用程序在运行,我想查找'XtraForm的所有打开的表单(表单实例):

  //  要求Linq  
私人列表< XtraForm> openXtraForms = Application.OpenForms.OfType< XtraForm>()。ToList();

如果我想在'XtraForm的所有实例中找到'XtraUserControl的所有打开实例:

列表与LT; XtraUserControl> openXtraUserControls =  new  List< XtraUserControl>(); 

// 使用刚刚创建的XtraForm列表
< span class =code-keyword> foreach (XtraForm instanceOfXtraForm in openXtraForms)
{
findUserControls(instanceOfXtraForm.Controls);
}

// 必要的递归方法
private void findUserControls(Control.ControlCollection theControls)
{
foreach (在 theControls中控制theControl
{
if (theControl XtraUserControl)openXtraUserControls.Add(theControl as XtraUserControl);

if (theControl.Controls.Count > 0
{
findUserControls(theControl.Controls);
}
}
}

使用基于此代码找到的内容填充CheckedListBox很容易,但是,仍然存在的关键问题是:什么你想用

用户检查或取消选中其中一个条目吗?或者,根据CheckedListBox中的哪些条目被选中或未选中,您希望将来做什么。



回答这个问题会影响项目的确切内容你将插入CheckedListBox。


除了解决方案3:



整个想法几乎没用。你永远不应该依赖属性 Control.Name



此属性仅在设计器中生成并且可以做任何事。即使设计师自动生成代码保证了名称的唯一性,也无法保证。您可以通过将一些名称分配给一个空字符串来检查它 - 一切都将继续有效。某些表单可以在不使用设计器的情况下在代码中创建,并忽略此属性的任何命名模式(这将是处理此属性的最佳方式)。



除此之外,您的反射代码只查找公共类型;你需要的类型可能不公开。



我根本不知道你为什么要使用反射,但你可以只在你的复选框中添加类型名称名单。我也不确定它是否有意义。如果你解释了你的最终目标,我很可能会有一些建议。



-SA


< blockquote>这是我的解决方案。



但我只能打开表格。



表格f = Application.OpenForms [  formName]; 
if (f!= null ){
string s = f.Text; // 现在我可以获得表单文本,但只能打开表单!
}


I have a code to get all form NAME on my project.

try
{
    Assembly assemblies = Assembly.Load("ProjectMyNameSpace");
    Type[] types = assemblies.GetTypes();
    foreach (Type t in types)
    {
        if (t.BaseType == typeof(XtraForm) | t.BaseType == typeof(XtraUserControl))
        {   
            checklistBox.Items.Add(t.Name);
            //t.Name like "frm_Login, frm_UserInfo...etc"
        }
    }
 }
 catch
 {
 }


But now, i want get Form.Text by form name. How i can do it?

http://i.stack.imgur.com/WwNdY.png

解决方案

Your code here, using reflection, will return pointers to the current Type definitions in your Project. But, those Type definitions are not the same as instances of the Types currently in use at run-time in the current Project !

I am guessing what you really want is to find all the currently open (in-use instances of) XtraForm, and XtraUsercontrol.

In the case of XtraForm, if your app is designed properly it's easy to find all the instances. In the case of XtraUserControl, it's more complex because you could have an instance of XtraUserControl inside a Panel inside another ContainerControl, etc.

If I have an active WinForms application running, and I want to find all the open Forms (form instances) of 'XtraForm:

// requires Linq
private List<XtraForm> openXtraForms = Application.OpenForms.OfType<XtraForm>().ToList();

If I want to find all the open instances of 'XtraUserControl in all instances of 'XtraForm:

List<XtraUserControl> openXtraUserControls = new List<XtraUserControl>();

// uses the List of XtraForm just created
foreach (XtraForm instanceOfXtraForm in openXtraForms)
{
    findUserControls(instanceOfXtraForm.Controls);
}

// recursive method necessary
private void findUserControls(Control.ControlCollection theControls)
{
    foreach (Control theControl in theControls)
    {
        if (theControl is XtraUserControl) openXtraUserControls.Add(theControl as XtraUserControl);

        if (theControl.Controls.Count > 0)
        {
            findUserControls(theControl.Controls);
        }
    }
}

Filling the CheckedListBox with whatever based on what this code has found is easy, but, the key question that remains, however, is: what do you want to happen when
the user checks, or unchecks, one of these entries ? Or, what do you want to do in the future based on which entries in the CheckedListBox are checked, or unchecked.

Answering that question will influence exactly what the Items are you will insert in the CheckedListBox.


In addition to Solution 3:

The whole idea is pretty much useless. You should never rely on the property Control.Name.

This property is only generated in the designer and can be anything. Even if the automatic generation of code by designer guarantees the uniqueness of the names, it is never guaranteed. You can check it up by assigning some Name to, say, an empty string — everything will continue to work. Some forms could be created in code without using the designer and ignore any naming pattern for this property (which would be the best way to deal with this property).

Besides, your reflection code only looks for public types; and the type you need might not be public.

I have no idea why do you use reflection at all, but you could add just the type names to your check box list. I am not sure it makes sense, too. If you explain your ultimate goal, most likely I'll have some suggestions.

—SA


HERE IS MY SOLUTION.

BUT I ONLY WORK WITH FORM OPENNING.

Form f = Application.OpenForms["formName"];
if (f != null) {
  string s = f.Text; // now i can get form text, but only can get form openning!
}


这篇关于如何通过表单名称获取表单标题(表单文本) - winform C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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