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


Franz Heyn26 reputationPosted Jul 31, 2024, 10:33 AM EDT
thanks for your answer
the only difference between your suggestion and mine is the MenuStrips public state.
In my DLL I do not use a public Method instead I use a public property DbMenuOwner to pass the MenuStrip-Object to the DLL
Setting the MenuStrip public however did not solve my problem, there's still a nullreference-exception
Tuhin PaulRank #7 · 71.9K reputationPosted Jul 30, 2024, 10:06 AM EDT
In your DLL, create a public method that will add the MenuItem to the MenuStrip. This method should take the MenuStrip as a parameter.
public class MyDll { public void AddMenuItem(MenuStrip menuStrip) { // Create a new MenuItem ToolStripMenuItem menuItem = new ToolStripMenuItem("My MenuItem"); // Add the MenuItem to the MenuStrip menuStrip.Items.Add(menuItem); } }In your main project, add a reference to the DLL. In your main project, call the
AddMenuItemmethod and pass the MenuStrip as a parameter.public class MainForm : Form { private MenuStrip menuStrip1; public MainForm() { InitializeComponent(); // Create an instance of the DLL MyDll myDll = new MyDll(); // Call the AddMenuItem method myDll.AddMenuItem(menuStrip1); } }Make the MenuStrip accessible
public class MainForm : Form { public MenuStrip menuStrip1; }