Jade M
Jade M

Reputation:

C# How can I assign a context menu to a tray icon when the tray icon is not on the same form?

I used the code below to programmatically create a system tray icon, this code lives in class file and not on my main form. I have dragged a contextMenuStrip control on to my main form, I now need to link the two but as the control if private I can't see it. What is the best way to link these two?

trayIcon = new NotifyIcon();
trayIcon.Icon = mainForm.Icon;
trayIcon.Text = "Test";
trayIcon.MouseDoubleClick += new MouseEventHandler(this.trayIcon_MouseDoubleClick);
trayIcon.ContextMenuStrip = //help needed here???

Thanks all

Upvotes: 0

Views: 1438

Answers (3)

JaredPar
JaredPar

Reputation: 754843

If you're worried about making the actual ContextMenuStrip field public, why not instead provide a non-private read only field. This will still maintain a level of encapsulation in your main form object.

public class MainForm { ...
  public ContextmenuStrip MyMenuStrip { 
    get { return contextMenuStrip; }
  }
}

Then you could just access mainForm.MyMenuStrip for the tray icon.

Upvotes: 0

Fiacc
Fiacc

Reputation: 1322

Nothing to stop you making the contextMenuStrip on the main form public, apart from the sheer nastiness of it.

Upvotes: 0

stuartd
stuartd

Reputation: 73253

You can set the Modifiers property of the contextMenuStrip to public.

Upvotes: 1

Related Questions