Visual C#, Add Remove ToolStripMenuItems in MenuStrip using Variable as Instance name/object name

Hi Friends,

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.

this.mnusMain.Items.Clear();

(mnusMain is a name of MenuStrip)

after successfully login those items are restored e.g.

ToolStripMenuItem tmp;
tmp = tsmiPatientCare;
this.mnusMain.Items.Add(tmp);
tmp = tsmiManage;
this.mnusMain.Items.Add(tmp);
tmp = tsmiTools;
this.mnusMain.Items.Add(tmp);
tmp = tsmiHelp;
this.mnusMain.Items.Add(tmp);

(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");
               
                this.mnusMain.Items.Clear();
                ToolStripMenuItem tmp;

                foreach (DataRow dr in temp)
                {
                    tmp =dr["ItemName"].ToString();                    
                    this.mnusMain.Items.Add(tmp);
                }

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.

Thanks,
ArunVashistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Solar_FlareCommented:
You are dealing with objects of type ToolStripMenuItem in your first bit of code and adding them to your toolstrip, but in the second you are dealing with strings and adding those strings to the toolstrip...

In order to find a control on your form by a given name you will need to use the Find method of a controlcollection, so I think you want to do something like this:



foreach (DataRow dr in temp)
{
   string controlName =dr["ItemName"].ToString();  
   ToolStripMenuItem m = (ToolStripMenuItem)this.Controls.Find(controlName, true);
   this.mnusMain.Items.Add(m);
}

Open in new window

0
ArunVashistAuthor Commented:
Hi Solar Flare,

I am getting following error for the line

"ToolStripMenuItem m = (ToolStripMenuItem)this.Controls.Find(controlName, true);"


Error    1    Cannot convert type 'System.Windows.Forms.Control[]' to System.Windows.Forms.ToolStripMenuItem'    


0
Solar_FlareCommented:
ok if it is returning an array you need to put:

ToolStripMenuItem m = (ToolStripMenuItem)this.Controls.Find(controlName, true)[0];

you should probably wrap a try...catch block around it too, in case no control is found by controlName.
0
ArunVashistAuthor Commented:
Hi Solar Flare,

Now the error turn out to be as "Error : Cannot convert type 'System.Windows.Forms.Control' to 'System.Windows.Forms.ToolStripMenuItem' "
0
Alexandre SimõesManager / Technology SpecialistCommented:
Hi mate,

as a solution for your scenario I would use the attached code.
Note that you can also have multiple ToolStrips and hide/show them as needed. Imagine having one ToolStrip for the login and another for the application.

I have 3 buttons on my test form and these are their click events:

btnRemove_Click
Removes all items from the toolstrip and stores the buttons on a collection

btnAdd_Click
Adds ALL the buttons again to the Toolstrip

btnAddByName_Click
Adds the buttons to the toolstrip based on a harcoded string List.
You can have whatever you want to hold this list of items names.

Cheers!
Alex

		List<ToolStripItem> items = new List<ToolStripItem>();
		private void btnRemove_Click(object sender, EventArgs e)
		{
			foreach (ToolStripItem item in this.toolStrip1.Items)
			{
				items.Add(item);
			}
			toolStrip1.Items.Clear();
		}
 
		private void btnAdd_Click(object sender, EventArgs e)
		{
			foreach (ToolStripItem item in items)
			{
				this.toolStrip1.Items.Add(item);	
			}
		}
 
		List<string> itemsToAdd = new List<string>() {"btn1", "btn3" };
		private void btnAddByName_Click(object sender, EventArgs e)
		{
			foreach (ToolStripItem item in items)
			{
				if (itemsToAdd.Contains(item.Name))
				{
					this.toolStrip1.Items.Add(item);
				}
			}
		}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.