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.








