0

I have my main form which has a NotifyIcon attached for balloon tips. I have a separate BalloonTip class which does some processing to determine what goes into the balloon. How can I use the notifyIcon in my main form/class from my BalloonTip class?

EDIT: I'm not sure how to pass the notify icon by reference

//passing data to my balloon class
ShowBalloonTip(data, ref notifyIcon1);

//not sure how to receive the notifyicon here
public void ShowBalloonTip(string s, object notifyicon)

Any suggestions?

Fuzz Evans
  • 2,893
  • 11
  • 44
  • 63

1 Answers1

1

You just need to pass a reference to the NotifyIcon to the BalloonTip class. I don't know what your BalloonTip class looks like but basically you need to add a method like this:

void DoSomething(NotifyIcon notifyIcon);

Another option may be to pass the notify icon to the BalloonTip constructor, in exactly the same way, and make a copy of the reference. That could make sense if you needed access to the notify icon repeatedly.

However, this couples the two classes closely together and may be limiting if ever you need to use your balloon tip without a notify icon.

So yet another option would be to extract the information from the notify icon in your main form class and pass that to your balloon tip class. This keeps the two classes decoupled and independent. Of course, if the balloon tip needs to invoke methods of the notify icon then that approach won't work.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I do need to access the notifyicon repeatedly. What do you mean by extract the information from the notify icon? – Fuzz Evans Dec 18 '11 at 21:04
  • If you just need to know a couple of properties of the NotifyIcon then you could pass them. If you need to call methods of the NotifyIcon then that won't work. I'm rambling a little bit because I'm finding it a little hard to work out exactly what it is you are trying to do. – David Heffernan Dec 18 '11 at 21:05
  • I have a main form and a settings form. Attached to the Main form I have notifyIcon1. I am trying to use that notifyIcon1 to display balloon prompts. The trouble I'm running into is that I have a class called BalloonTip. From either form different events send different strings to the BalloonTip class. Within that class there is a switch statement that assigns a title and body of text based on the incoming data. I thought this would be the cleanest way to deal with multiple forms throwing multiple balloon tips. – Fuzz Evans Dec 18 '11 at 21:13
  • Actually upon further review I think it might be ok with your original answer. I have a dozen different events that trigger a specific balloon tip by sending a specific string to this method which passes the string as well as the notifyIcon1 to my BalloonTip class so only 1 method actually has to send the notifyIcon. – Fuzz Evans Dec 18 '11 at 21:29