With reference to my article How to make pop-up / Notification bar with Message Ticker (smooth message scroller) over the Task Bar in C#.NET , I have been getting inquiries on how to handle certain mouse events. so in this post I will explain how you can make the scroller stop/start on mouse hover/leave events respectively.
before we go ahead, I feel its important for you as reader to know that this is the third article in the chain.following are reference to the previous articles ( for those who are interested )
- How to make pop-up / Notification bar with Message Ticker (smooth message scroller) over the Task Bar in C#.NET | Download Source Code
- How to create (make a Windows Form act like) a Docking Window without a container in C#.NET | Download Source Code
moving on.
we have two labels which are working as a smooth vertically scrolling set of messages, this scroll effect is given by a Thread we created with the name BeginScroll, so in order to make the labels stop we have to some how make the infinite loop in the BeginScroll thread to stop and vice versa in case of start it again. for that to happen we are going to use a a concept straight out of the operating system textbook i.e. Semaphore/Mutex.
we have to develope a machanism that would allow us to use Signal/Wait strategy. For that .NET Framework has a handy feature called ManualResetEvent(details of this is out of the scope of this article, so I would prefer you to look here for further information )
(You can use any of the source code mentioned in the above two articles,I will be using source code from the second article mentioned above in this example)
- Add the following two global declarations to the code (NotificationTiker.cs)
public ManualResetEvent signalThread = new ManualResetEvent(false);
private bool keepScrolling;
- now add the MouseHover and MouseLeave events for the both the labels such that they should match the following code.
private void lblMessageOne_MouseHover(object sender, EventArgs e)
{
//Set KeepSrcolling variable to false so that the loop
//in the thread terminates
keepScrolling = false;
//Reset Event of the ManualResetEvent will make WaitOne
//Event in the BeginScroll Thread to block and effectively
//stopping the scroll effect
signalThread.Reset();
}
private void lblMessageOne_MouseLeave(object sender, EventArgs e)
{
//Set Event of the ManualResetEvent will release the blocked
//Thread (blocked by the WaitOne Method)
signalThread.Set();
}
private void lblMessageTwo_MouseHover(object sender, EventArgs e)
{
//Set KeepSrcolling variable to false so that the loop
//in the thread terminates
keepScrolling = false;
//Reset Event of the ManualResetEvent will make WaitOne
//Event in the BeginScroll Thread to block and effectively
//stopping the scroll effect
signalThread.Reset();
}
private void lblMessageTwo_MouseLeave(object sender, EventArgs e)
{
//Set Event of the ManualResetEvent will release the blocked
//Thread (blocked by the WaitOne Method)
signalThread.Set();
}
now after making some changes to the scroll function, it should contain the following code
private void BeginScroll()
{
//This variable will take label out of view
int LabelHeight = -lblMessageOne.Height;
//This variable will be used to assign out of view variable to bottom of the panel, to re-enter
int PanelBottom = panel1.Height;
bool firstEntry = true;
while (true)
{
if (!firstEntry)
signalThread.WaitOne();
keepScrolling = true;
while (keepScrolling)
{
firstEntry = false;
if (lblMessageOne.Location.Y == LabelHeight)
{
//when label one goes out of view, it is configured to re enter again
lblMessageOne.Location = new Point(lblMessageOne.Location.X, PanelBottom);
}
else
{
//Scrolling label by decrementing it y-axis location
int MessageOneY = lblMessageOne.Location.Y - 1;
lblMessageOne.Location = new Point(lblMessageOne.Location.X, MessageOneY);
}
if (lblMessageTwo.Location.Y == LabelHeight)
{
//when label one goes out of view, it is configured to re enter again
lblMessageTwo.Location = new Point(lblMessageTwo.Location.X, PanelBottom);
}
else
{
//Scrolling label by decrementing it y-axis location
int MessageTwoY = lblMessageTwo.Location.Y - 1;
lblMessageTwo.Location = new Point(lblMessageTwo.Location.X, MessageTwoY);
}
this.Refresh();
Thread.Sleep(50);
}
}
}
- this is it, now Run(F5) the application and test the mouse over and leave events.
- Cheers, dont forget to leave comments/feedback.
- Download Source Code Here.



[...] How to make pop-up / Notification bar with Message Ticker (smooth message scroller) over the Task Ba… [...]
[...] How to make pop-up / Notification bar with Message Ticker (smooth message scroller) over the Task Ba… [...]
Hello, thanks for great work!
My question before was about this,
I’m programing for my server management,
see below
1. Program is showing on tray with context menu :
– DB Replication Monitoring Start
– DB Replication Monitoring Stop
2. When monitoring start, Timer(Windows.Forms.Timer) starts and check DB status in every 60 seconds
3. When Something is wrong, Notify status to the administrator which is me~ using your program! in every 60 seconds.(also using another timer, every 60 seconds it shows up and down)
But the problem is that
while this Notification Ticker is showing up
I can’t click on my context menuitem.
I think it’s because of [For] statements. Can you gimme any idea? I’m still working on it…..
Thanks,
my guess would be that you have put in the for loops which bring up the notification bar in the Timer’s Tick Event which for obvious reasons keeps the status of the notification bar busy because the thread which is running your application is totally dedicated to move those notifications.
The trick is to handle the ticker in a separate thread which is created when Timer’s Tick event is called. This would mean that the main thread is free to handle other calls while the other thread you created will keep the ticker going.
Hi~ It’s me again~
I’ve solved the problem I told you,
But the door is still closed..
If I seperate main application into two threads, which the first thread is for Ticker itself and the other one is for the event that includs [FOR] Statements I thought It would work, I could give an mouse over event on it.
But still I can see sand-timer icon when that ticker is showing up~.
What should I do..,
Need your help, but still thanks!