Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / May 2004

Tip: Looking for answers? Try searching our database.

How to add events to  dynamic contextmenu ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Priya - 06 May 2004 23:21 GMT
Hi
I am looking for some help in adding events to dynamic contextmenu.I want to rise the click event for each of the menu ite
that I am creating.

Below is my code for adding dynamic context menu

System.Windows.Forms.MenuItem MnuItem
MnuItem= new System.Windows.Forms.MenuItem()
MnuItem.Text = comboBox1.Text;       
cmFavorites.MenuItems.Add(cmFavorites.MenuItems.Count,MnuItem)

can someone help me with the sample lines of code ,how to add the events for the dynamic menu
Thanks for your help in advanc
Priya
Wiktor Zychla - 07 May 2004 15:50 GMT
> System.Windows.Forms.MenuItem MnuItem;
> MnuItem= new System.Windows.Forms.MenuItem();
> MnuItem.Text = comboBox1.Text;

MnuItem.Click += new EventHandler( method_name );

where

void method_name( object sender, EventArgs e )
{
 ...
}

Regards,
Wiktor Zychla
Priya - 07 May 2004 16:46 GMT
Thanks I am able to get the click event working.One more question in this context
How will I reference the object name inside the click event,since I am creating the context menu dynamically
I cannot reference it using MnuItem.Text any more

System.Windows.Forms.MenuItem MnuItem
MnuItem= new System.Windows.Forms.MenuItem()
MnuItem.Text = comboBox1.Text
cmFavorites.MenuItems.Add(cmFavorites.MenuItems.Count,MnuItem)
MnuItem.Click += new EventHandler( MnuItem_Click )

private void MnuItem_Click( object sender, EventArgs e

//How will I reference MnuItem.Text here.

Thanks for your help
Regards
Priy
Priya - 07 May 2004 17:31 GMT
Thanks I found the solution
For other who is looking for an answer ,here is how it can be done
private void MnuItem_Click( object sender, EventArgs e
       
            MenuItem mi = (MenuItem)sender
            MessageBox.Show(mi.Text)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.