English 中文(简体)
c. 物体的反对和排 to形式?
原标题:Form to object and loop through objects in c#?

我有以下主要形式:(frmMain),有的则有:(button1, button2......)

然后我这样做:

object objectsContainer = frmMain; // <--now the object contains form base, button1, button2...

我能如何通过我反对的所有含有物品进入Tito1,但顿2......?

我这样做,但这不是我想要的东西。

foreach (PropertyInfo pInfo in objectsContainer.GetType().GetProperties())     
{
               
}

我想做的是:

foreach (object objectFromForm in objectsContainer)     // <--- how to do this looping an object (type of form)
{
    //here is objectFromForm = base, button1, button2...
    foreach (PropertyInfo pInfo in objectFromForm .GetType().GetProperties())     
    {
               //here it s possible to access pInfo and its properties like size, name ...
    }
}

当Im debuggin和审视物体的内容时 我想要的是集装箱。

一些建议?

最好。

** 本文件迟交。 本文件迟交。

UPDATE:

** 本文件迟交。 本文件迟交。

OK, I made a test project. There you could see what I want to do. In the project is an image with the objects... Here you can download it: http://www.mediafire.com/download.php?ik5j3ejnzm2

最好。

最佳回答

每一<代码>Control都有Controls的收集资料,经您通过全面分类可检索,但不幸的是 ToolStrip。 这些项目使用不同的物体模型(即:tt allControl);因此,你可以iterate这种装置(也包括菜单项目),但只字塔;这里的例子有:

    IEnumerable RecurseObjects(object root) {
        Queue items = new Queue();
        items.Enqueue(root);
        while (items.Count > 0) {
            object obj = items.Dequeue();
            yield return obj;
            Control control = obj as Control;
            if (control != null) {
                // regular controls and sub-controls
                foreach (Control item in control.Controls) {
                    items.Enqueue(item);
                }
                // top-level menu items
                ToolStrip ts = control as ToolStrip;
                if (ts != null) {
                    foreach(ToolStripItem tsi in ts.Items) {
                        items.Enqueue(tsi);
                    }
                }
            }
            // child menus
            ToolStripDropDownItem tsddi = obj as ToolStripDropDownItem;
            if (tsddi != null && tsddi.HasDropDownItems) {
                foreach (ToolStripItem item in tsddi.DropDownItems) {
                    items.Enqueue(item);
                }
            }
        }            
    }

例如,你可以这样说:

    foreach (object obj in RecurseObjects(this)) {
        Console.WriteLine(obj);
    }

当然,问题是:你想要与每个项目做些什么?

问题回答

你可以选择控制会。

诚然,如果这些控制属于小组,就可以加以控制。

private void RecusiceControls(ControlCollection controls)
        {
            foreach (Control control in controls)
            {
                RecusiceControls((ControlCollection)control.Controls);
                if (control is Button)
                {
                }
            }
        }

仔细研究

rel=“nofollow noreferer”> 在C#中重新设定控制权。

public IEnumerable<Control> Get (object o)  
{  
    if (o is System.Windows.Forms.Control)  
    {  
        System.Windows.Forms.Control f =(System.Windows.Forms.Control)o;  
        foreach(System.Windows.Forms.Control c in f.Controls)  
        {  
            yield return c;
            foreach(System.Windows.Forms.Control c2 in Get(c))  
            {
                yield return c2;
            }
        }  
    }  
}

如何做到这一点(类似于其他答案,但简单易用):

using System.Windows.Forms;

public static class ControlExtensions 
{
    public IEnumerable<Control> DescendantControls(this Control control)  
    {   
        foreach(var child in control.Controls)  
        {  
            yield return child;
            foreach(var descendant in child.DescendantControls())  
            {
                yield return descendant;
            }
        }
    }
}

我放弃了像我经常使用的那种公用事业职能。

我决定进行这一尝试。 我下载了你的样本,并修改了你的主要形式,以包括我认为你要求采用的算法。 希望:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();

         //if you debug this code then the objects is holding all controls on this form
         object objects = this;

         Dictionary<Control, string> allControls = GetIt(objects);
      }

      /// <summary>
      /// How to get all control names an .Text values if availible (included this form)???
      /// </summary>
      private Dictionary<Control, string> GetIt(object objects)
      {
          Dictionary<Control, string> found = new Dictionary<Control, string>();
          Queue<Control> controlQueue = new Queue<Control>();
          controlQueue.Enqueue(objects as Control);

          while (controlQueue.Count > 0)
          {
              Control item = controlQueue.Dequeue();
              foreach (Control ctrl in item.Controls)
              {
                  controlQueue.Enqueue(ctrl);
              }
              found.Add(item, item.Text);
          }

          return found;
      }
   }
}




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签