Visual C#, Add Remove ToolStripMenuItems in MenuStrip using Variable as Instance name/object name
Posted on 2009-05-20
I am developing a windows application in VS 2008 using C#, I have a MenuStrip in my Application that contains few ToolStripMenuItems added at design time. When I run my application user need to login using Username and password which is verified against a record in Database.
Before Login when application loads I remove all ToolStripMenuItems using ".Clear()" e.g.
(mnusMain is a name of MenuStrip)
after successfully login those items are restored e.g.
tmp = tsmiPatientCare;
tmp = tsmiManage;
tmp = tsmiTools;
tmp = tsmiHelp;
(tsmiPatientCare, tsmiManage, tsmiTools, tsmiHelp are ToolStripMenuItems and these names are given at Design Time)
Now what I am trying to do is, rather restoring all ToolStripMenuItems, a user will get a datatable filled with the name of ToolStripMenuItems which he required and I want to loop through the datatable and use those values as name of ToolStripMenuItems to restore.
I am trying following code but it never works :
DataRow temp = Login.dsLogin.Tables["UserRights"].Select("Type=1");
foreach (DataRow dr in temp)
Suppose "dr["ItemName"].ToString()" contains "tsmiPatientCare" as value, then how can I achieve the same result as i get when using direct name of ToolStripMenuItem.