菜单项中的值填充问题 [英] Value filling problem in menu Item

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

问题描述

你好,
我递归地填充菜单....但是我没有在MenuItem"Item
"中获得值和Text 如何解决呢?这是示例代码...不完整

Hi there,
I''m filling a menu recursively....But I dont get the value and Text in MenuItem "Item
How to resolve this? It is the sample code...it s not complete

protected void FillMenuItems()
    {
        DataSet ds = objMenu.MainMenu();
        
        for (int i = 0; i<ds.Tables[0].Rows.Count  ;i++ )
        {
            MenuItem Item = new MenuItem();
            string MainMenuItem = ds.Tables[0].Rows[i]["mnuText"].ToString();
            string MainMenuValue = ds.Tables[0].Rows[i]["mnuChildId"].ToString();
            Item.ChildItems.Add(new MenuItem(MainMenuItem, MainMenuValue));
            Menu1.Items.Add(new MenuItem(MainMenuItem, MainMenuValue));
            FillSubMenuItems(MainMenuValue, Item);
         }
      }

    protected void FillSubMenuItems(string ParentId,MenuItem ParentMenu)
    {
        DataSet ds = objMenu.SubMenu(ParentId);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
             MenuItem SubItem = new MenuItem();

             string SubItemText  = ds.Tables[0].Rows[i]["mnuText"].ToString();
             string SubItemValue  = ds.Tables[0].Rows[i]["mnuChildId"].ToString();
             SubItem.ChildItems.Add(new MenuItem(SubItemText, SubItemValue));
             ParentMenu.ChildItems.Add(SubItem);
             FillSubMenuItems(SubItemValue,ParentMenu);
          //   Menu1.Items.Add(ParentMenu);
         }
        
    }



在此先感谢



Thanks in advance

推荐答案

Erhm,我想如果您像这样修改代码:

Erhm, I think if you modify the code like so :

protected void FillMenuItems()
    {
        DataSet ds = objMenu.MainMenu();
        
        for (int i = 0; i<ds.Tables[0].Rows.Count  ;i++ )
        {
            string MainMenuItem = ds.Tables[0].Rows[i]["mnuText"].ToString();
            string MainMenuValue = ds.Tables[0].Rows[i]["mnuChildId"].ToString();
            Item.ChildItems.Add();
            MenuItem mainItem MenuItem(MainMenuItem, MainMenuValue)
            Menu1.Items.Add(mainItem);
            FillSubMenuItems(MainMenuValue, mainItem);
         }
      }
    protected void FillSubMenuItems(string ParentId,MenuItem ParentMenu)
    {
        DataSet ds = objMenu.SubMenu(ParentId);
        for (int i = 0; i < ds.Tables[0].Select("mnuParentId = " + ParentId); i++)
        {
             string SubItemText  = ds.Tables[0].Rows[i]["mnuText"].ToString();
             string SubItemValue  = ds.Tables[0].Rows[i]["mnuChildId"].ToString();
             MenuItem subItem = new MenuItem(SubItemText, SubItemValue);
             ParentMenu.ChildItems.Add(subItem);
             FillSubMenuItems(SubItemValue,subItem);
          //   Menu1.Items.Add(ParentMenu);
         }
        
    }



您可能会有更多的运气.

注意:请检查我使用过的表列名称,因为由于我不知道您的数据源的结构,所以不确定名称是否正确.



You may have more luck.

Note : Check the table column names i''ve used because i''m not sure if the names are correct since I don''t know the structure of your data source.


MenuItem构造函数没有接受两个字符串的重载-我认为您需要查看MenuItem类的重载:

http://msdn.microsoft.com/en-us/library/system. windows.forms.menuitem.aspx [ ^ ]

除此之外,您可能需要在数据库中获取更多信息.我可能会有以下架构:

int ID(唯一标识符)
int ParentID
字符串名称
int序列
字符串工具提示

这样,您可以按任意顺序自由添加菜单项,将一个或多个菜单项与给定的父"菜单项的ID关联,并使它们以任何所需的顺序显示.所以你可以这样:

The MenuItem constructor doesn''t have an overload that accepts two strings - I think you need to review the overloads for the MenuItem class:

http://msdn.microsoft.com/en-us/library/system.windows.forms.menuitem.aspx[^]

Beyond that, you probably need to have more info in the database. I would probably have the following schema:

int ID (unique identifier)
int ParentID
string Name
int Sequence
string Tooltip

This way, you can freely add menu items in any order, associate one or more with a given Parent menu item''s ID, and cause them to appear in any desired order. So You could have this:

ID  PARENT  NAME  SEQ  TOOLTIP
1     0     File   0   ""
2     1     Open   0   "Open an existing file"
3     1     Save   1   "Save the currently open file"
4     1     Exit   2   "Exit program"
5     0     Edit   1   ""
6     5     Copy   0   "Copy selected  items to clipboard"



此时,您可以使用C#代码以更有条理的方式创建菜单. parentID为0表示一个根菜单项,您可以添加其他列来定义该菜单项是否被选中,启用或是分隔符.



At this point you could create your menu in a more organized fashion in your C# code. A parentID of 0 would indicate a root-level menu item, and you could add additional columns that defined whether or not the item is checked, enabled, or is a divider.


这篇关于菜单项中的值填充问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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