Friday, November 20, 2009

Dynamically Creat Menu C#

Creating Menu Item at Run Time C# .net Code


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


namespace Reports
{
public partial class Reports : Form
{
OdbcDataReader od;
Connect c = new Connect();
string[] menuitem=new string[20];
MenuStrip menu = new MenuStrip();
int id = 0;

public Reports()
{
InitializeComponent();
}

private void Reports_Load(object sender, EventArgs e)
{
c.cmd().CommandText = "select distinct r.undermenu,r.undername from rptlist as r ,user_auth as u where r.menuname=u.repotname and u.uid='02' and u.prm='1' ";
c.cmd().Connection = c.cn();
od = c.cmd().ExecuteReader();

while (od.Read())
{

string s = od["undermenu"].ToString();
// menuStrip1.Items.Add(s);
//menuStrip1.Items.Add(s);
menuitem.SetValue(s, id);
id++;

}
od.Close();
for (int i =0; i< id ; i++)
{
ToolStripMenuItem item = new ToolStripMenuItem(menuitem[i].ToString());

// string menuname = menuStrip1.Items[i].Text.ToString();
c.cmd().CommandText = "select r.menuname from rptlist as r ,user_auth as u where r.menuname=u.repotname and u.uid='02' and r.undermenu like '" + menuitem[i].ToString() + "'and u.prm='1' order by r.undermenu,r.menuname";
c.cmd().Connection = c.cn();
od = c.cmd().ExecuteReader();
while (od.Read())
{

string sa = od["menuname"].ToString();
// menuStrip1.Items.Add(s);
item.DropDownItems.Add(sa.ToString(),null,new System.EventHandler(menuitem_click));
menu.Items.Add(item);
// item.Click += new EventHandler (this.menuitem_click);
}
od.Close();
}
this.Controls.Add(menu);


}

No comments:

Post a Comment