How to add dynamically shortcuts from database table to Menuitems ? ? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to add dynamically shortcuts from database table to Menuitems ? ?

kishore_peddikishore_peddi Posts: 176Member

Hi,

Good day. I want to add short-cuts to menu-items which are dyanmically loaded from database table.

-------------------------------------------------------------------
MenuItemID | ParentMenuItemID | MenuItemName | Shortcut Key | Icon |
-------------------------------------------------------------------
1 | 0 | File | CtrlF | 0 |
-------------------------------------------------------------------
2 | 1 | Open | CtrlO | 1 |
-------------------------------------------------------------------
3 | 1 | Save | CtrlS | 1 |
-------------------------------------------------------------------
4 | 0 | Edit | CtrlE | 0 |
-------------------------------------------------------------------
5 | 4 | Cut | CtrlC | 1 |
-------------------------------------------------------------------
6 | 4 | Copy | CtrlY | 1 |
-------------------------------------------------------------------
7 | 4 | Paste | CtrlP | 1 |
-------------------------------------------------------------------

Can you please suggest me the way to perform the task. Then how to add the short-cuts to the Menu-Items ? Please respond ASAP.

I am giving the sample code what i have written for the dynamic menu creation without short-cuts.


private void Form1_Load(object sender, System.EventArgs e)
{
DataTable dt = new DataTable();
Hashtable ht = new Hashtable();

SqlConnection conn = new SqlConnection("server=SERVER;UID=nexalar;PWD=nexalar;Database=nexalar");

new SqlDataAdapter("SELECT * FROM TM ORDER BY ParentMenuItemID Asc", conn).Fill(dt);

foreach (DataRow r in dt.Rows)
{
int MenuItemID = int.Parse(r["MenuItemID"].ToString());
int ParentMenuItemID = int.Parse(r["ParentMenuItemID"].ToString());
string MenuItemName = r["MenuItemName"].ToString().Trim();


CustomMenuItem cMenuItem = new CustomMenuItem(MenuItemName, MenuItemID, ParentMenuItemID);
ht.Add(MenuItemID, cMenuItem);

if (ParentMenuItemID == 0)
{
mainMenu1.MenuItems.Add(cMenuItem);

}
else
{
((CustomMenuItem) ht[ParentMenuItemID]).MenuItems.Add(cMenuItem);
}
}
ht.Clear();
}

public class CustomMenuItem : MenuItem
{
private int MenuItemID;
public int ID
{
get {return MenuItemID;}
set {MenuItemID = value;}
}

private int ParentMenuItemID;
public int ParentID
{
get {return ParentMenuItemID;}
set {ParentMenuItemID = value;}
}

public CustomMenuItem(string name, int id, int parentid)
{
base.Text = name;
MenuItemID = id;
ParentMenuItemID = parentid;
}

public CustomMenuItem()
{
}
}



Advance ThanX for your time & patience.

Bye...
Cheers
Kishore
Sign In or Register to comment.