列出组合框中的表单控件

发布于 2024-09-04 20:55:46 字数 413 浏览 2 评论 0原文

[.NET 2]

我应该如何在相同表单的组合框中列出表单控件(就像 VS 设计器那样)?

替代文本 http://lh6.ggpht.com/_1TPOP7DzY1E/ TBIDC_uA7NI/AAAAAAADPM/VAPieyHFzEw/s400/Capture2.gif

我尝试过:

cboObjectSelection.DataSource = Me.Controls

但这不起作用。

是否可以过滤(自定义)此列表?

[.NET 2]

how should I list a form controls in a Combobox of the same form(like VS designer does)?

alt text http://lh6.ggpht.com/_1TPOP7DzY1E/TBIDC_uA7NI/AAAAAAAADPM/VAPieyHFzEw/s400/Capture2.gif

I tried:

cboObjectSelection.DataSource = Me.Controls

but this does not work.

Is there a possibility to filter(customize) this list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

看海 2024-09-11 20:55:47

如果您将 ComboBox.DisplayMember 设置为 "Name",您也许可以做到这一点,但您不会获得其他控件中包含的任何控件,所以我认为您' d 必须(递归地)获取所有控件的名称并将它们插入到集合中,然后将其作为 DataSource 传递。

You might be able to do it if you set the ComboBox.DisplayMember to "Name" but you'd then not get any controls contained in other controls so I think you'd have to get out the names of all the controls (recursively) and insert them into a collection and then pass that as the DataSource.

心在旅行 2024-09-11 20:55:47

我把代码放在了按钮的点击事件中,你可以根据你的要求修改它。希望这会对您有所帮助。

private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.Controls.Count; i++)
            {
                s = this.Controls[i].GetType().ToString();
                comboBox1.Items.Add(s);

            }

        }

I have put the code in a button's click event, you can modify it according to you requirement. Hope this will help you.

private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.Controls.Count; i++)
            {
                s = this.Controls[i].GetType().ToString();
                comboBox1.Items.Add(s);

            }

        }
好菇凉咱不稀罕他 2024-09-11 20:55:47

看起来您在这里使用的是 VB - 恐怕我的答案将是 C#。

这是我的解决方案,您可以在这里看到它的屏幕截图(希望,如果这个该死的链接有效!)。

您需要递归地迭代表单上的所有控件,如果每个控件有子控件,则向下遍历到每个控件的 Controls 集合。

此解决方案使用一个私有类“ControlInfo”,其中放置了一个 Control 实例 - 它重写了 ToString() 操作,以便您可以轻松自定义组合中显示的文本。然后,在从表单的控制树生成组合后,将组合数据绑定到一堆组合。

要使用此代码创建一个新表单并在其中放置一个名为comboBox1的组合,那么您应该能够用以下内容替换所有内容:

public partial class Form1 : Form
{
  private class ControlInfo
  {
    public Control Control { get; set; }
    public override string ToString()
    {
      return string.Format("{0} ({1})", Control.Name, Control.GetType());
    }
  }
  public Form1()
  {
    InitializeComponent();
    comboBox1.DataSource = 
      GetControls(this.Controls.Cast<Control>()).OrderBy(c => c.Name).
        Select(c => new ControlInfo() { Control = c }).ToList();
  }

  private IEnumerable<Control> GetControls(IEnumerable<Control> controls)
  {
    foreach (var control in controls)
    {
      yield return control;
      if (control.Controls.Count > 0)
      {
        foreach (var childControl in GetControls(
          control.Controls.Cast<Control>()))
        {
          yield return childControl;
        }
      }
    }
  }

It looks like you're using VB here - and my answer will be in C#, I'm afraid.

Here's my solution, and you can see a screenshot (hopefully, if this bloody link works!) of it working here.

You need to recursively iterate through all the controls on the form, descending into each one's Controls collection if it has children.

This solution uses a private class 'ControlInfo' inside which a Control instance is place - which overrides the ToString() operation so you can easily customise the text that is displayed in the combo. You then Databind the combo to a bunch of these after producing them from the Form's control tree.

To use this code create a new form and place a combo called comboBox1 on there, then you should be able to replace everything with this:

public partial class Form1 : Form
{
  private class ControlInfo
  {
    public Control Control { get; set; }
    public override string ToString()
    {
      return string.Format("{0} ({1})", Control.Name, Control.GetType());
    }
  }
  public Form1()
  {
    InitializeComponent();
    comboBox1.DataSource = 
      GetControls(this.Controls.Cast<Control>()).OrderBy(c => c.Name).
        Select(c => new ControlInfo() { Control = c }).ToList();
  }

  private IEnumerable<Control> GetControls(IEnumerable<Control> controls)
  {
    foreach (var control in controls)
    {
      yield return control;
      if (control.Controls.Count > 0)
      {
        foreach (var childControl in GetControls(
          control.Controls.Cast<Control>()))
        {
          yield return childControl;
        }
      }
    }
  }
梦冥 2024-09-11 20:55:47

您必须迭代 Controls 集合中的每个项目并将其添加到 ComboBox 的 Items 集合中。最简单的代码如下所示:

For Each c As Control in Me.Controls
    cboObjectSelection.Items.Add(c.Name)
Next

这里的问题是 Me.Controls 是分层的。在这种情况下,您的表单上的面板内的 IE 控件将会丢失。您需要添加所有面板控件才能获取表单上的所有内容。这是递归的理想应用:

Private Sub AddControls(ByVal Combo As ComboBox, ByVal Control As Control)
    For Each c As Control In Control.Controls
        Combo.Items.Add(c.Name)
        AddControls(Combo, c)
    Next
End Sub

要取回控件,您必须执行以下操作:

Dim c As Control = Me.Controls.Find(ComboBox1.SelectedItem.ToString(), True)(0)

第二个参数告诉查找控件是否通过控件的层次结构进行递归。 Find 方法返回一个控件数组,因此使用末尾的 (0) 来获取第一个元素。对于越界异常(IE,Find 方法找不到任何内容),您应该是安全的,因为 ComboBox 中的所有内容都将在几分钟前由代码添加。

希望这有帮助!

You'll have to iterate each item in the Controls collection and add it to the ComboBox's Items collection. The simplest code would look like this:

For Each c As Control in Me.Controls
    cboObjectSelection.Items.Add(c.Name)
Next

The issues here are that Me.Controls is hierarchical. IE, a the controls inside a Panel on your form will be missed with this case. You would need to add all the Panel's Controls to get EVERYTHING on the form. Which is an ideal application of recursion:

Private Sub AddControls(ByVal Combo As ComboBox, ByVal Control As Control)
    For Each c As Control In Control.Controls
        Combo.Items.Add(c.Name)
        AddControls(Combo, c)
    Next
End Sub

To get the control back, you have to do this:

Dim c As Control = Me.Controls.Find(ComboBox1.SelectedItem.ToString(), True)(0)

The second parameter tells the find controls whether to recurse through the hierarchy of controls. The Find method returns an array of controls, hence the (0) at the end to get the first element. You should be safe here regarding out of bounds exceptions (IE, the Find method doesn't find anything) because everything in the ComboBox will have been added by code a few minutes ago.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文