如何从菜单栏填充组合框 [英] How to populate combobox from menustrip

查看:90
本文介绍了如何从菜单栏填充组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi codeproject ..
我想用菜单栏层次结构填充组合框
如何使用下面显示的示例从menustrip填充组合框

http://i54.tinypic.com/selymc.jpg

如果有人可以向我展示方式或向我展示相关文章


请注意thx

hi codeproject..
I want to populate the combobox with menustrip hierarchy
how to fill combobox from menustrip with the example shown below

http://i54.tinypic.com/selymc.jpg

if anyone can show me the way or show me the relevant article


thx for attention

推荐答案

假定一个WinForms项目带有一个名为"menuStrip1"的MenuStrip和一个名为"comboBox1"的ComboBox:假定递归命令填充"comboBox1 is在表单加载事件中调用:
Assume a WinForms project with a MenuStrip named ''menuStrip1, and a ComboBox named ''comboBox1: Assume the recursive command to populate ''comboBox1 is called in the Form Load event:
private void Form1_Load(object sender, System.EventArgs e)
{
    // fill the ComboBox recursively
    parseMenuItems(menuStrip1.Items);
}

// recursive procedure
private void parseMenuItems(ToolStripItemCollection toolStripItemCollection)
{
    foreach (ToolStripMenuItem theToolStripMenuItem in toolStripItemCollection)
    {
        // add the Item itself: you get a blank entry
        // in the ComboBox, so we use the Text property
        comboBox1.Items.Add(theToolStripMenuItem.Text);

        if (theToolStripMenuItem.HasDropDownItems)
        {
            parseMenuItems(theToolStripMenuItem.DropDownItems);
        }
    }
}

免责声明:未在插入了其他类型对象的菜单条上测试(RadioButtons?).

讨论:和SAK一样,我很好奇您为什么要这么做.

Disclaimer: not tested on a MenuStrip with other types of objects inserted in it (RadioButtons ?).

Discussion: Like SAK, I am curious as to why you would want to do this.


这篇关于如何从菜单栏填充组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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