Can I use a Balloon tip from one class, while the NotifyIcon resides in a different class?

StackOverflow https://stackoverflow.com/questions/8554578

Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top