如何在Windows C#中的运行时创建菜单

问题描述:

你好,

我想在我的数据库表的Windows窗体上创建带有子menuitems的菜单.....

我有一个数据库表,其中包含菜单字段,子菜单,索引,菜单名称列等...

请任何人有任何文章或简单代码(示例)或任何提示,我可以通过这些提示在运行时从数据库创建菜单

谢谢.

Hello,

i want to create a menu with sub menuitems... on windows form from my database table.....

i have a database table with the menu filed,submenu,index,menu name columns etc...

pls any one have any article or simple code(example) or any hint by which i can create the menu at runtime from database

thank u .



这是示例代码.试试这个,

Hi,

Here is the sample code. try this,

MenuItem[] mi = new MenuItem[1];
mi[0] = new MenuItem();
mi[0].Index = 0;
mi[0].Text = "New";
mi[0].MergeType = MenuMerge.MergeItems;
          
MainMenu MM = new MainMenu();
MM.MenuItems.Add("File", mi);
                      
this.Menu = MM;



问候,
Suresh



Regards,
Suresh


我假设您已经知道如何连接到数据库,并且您已经拥有一个包含数据库表中数据的数据集.将下面的代码放在窗体的OnShown事件中
I assume you already know how to connect to the database and you already have a DataSet containing the data from the database table. Put the code below in the OnShown event of the form
DataTable dt = dataSet1.Tables[0];
MainMenu menu = new MainMenu();
List<menuitem> list = new List<menuitem>();

foreach(DataRow row in dt.Rows)
{
   MenuItem item  = new MenuItem();
   item.Text = row["menu_name"];
   list.Add(item);
}

menu.MenuItems.Add("Products", list.ToArray());
                      
this.Menu = MM;

</menuitem></menuitem>



我不太确定确切的语法,但是使用Visual Studio应该不是问题.



I''m not very sure about the exact syntax, but with visual studio that shouldn''t be a problem.