Franz Heyn

Franz Heyn

  • 1.7k
  • 24
  • 1.5k

How do I add a Menuitem to a Menustrip from a DLL

Jul 30 2024 12:49 PM

hi,

well half of the question I've already answered, there's no problem modifying an Application's Menustrip, you just have to send the DLL

the Apps Menustrip-object. The dropdownitems Click-Event does not work.

How I did it:

DbMenuOwner receives the MenuStrip-Object

When the DLL is loaded, I check DbMenuOwner and call AddDbMenu().

        public override void Load(string databasename)
         {
            if (DbMenuOwner != null)
                AddDbMenu();
        }

The DLL-Method AddDbMenu looks like this :

        protected void AddDbMenu()
        {
            System.Windows.Forms.ToolStripMenuItem tsmi = new System.Windows.Forms.ToolStripMenuItem();
            tsmi.Text = "Database";
            tsiSelect = tsmi.DropDownItems.Add("Select");
            tsiSelect.Click += new EventHandler(tsiDatabaseSelect_Click);

            DbMenuOwner.Items.Insert(1, tsmi);
        }

        public void tsiDatabaseSelect_Click(object sender, EventArgs e)
        {
            this.DatabaseUI();
        }

When I run the Application and click the "Select"-Item, the App crashes.

Any ideas what I'm doing wrong here, or is there something missing?

One important note: I do not want to add any Code to my Application.

for your answers many thanks in advance

Franz


Answers (2)