tags:

views:

658

answers:

3

Hi all,

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

+1  A: 

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

Stuart Dunkeld
A: 

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

StressChicken
A: 

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.

JaredPar
Or he could change the `Modifiers` in the designer to `public`.
configurator