1. Generate a continuous call by pressing and holding the Left or Right button
During development, we sometimes need to generate a continuous event after the mouse is pressed. For example, the up and down arrow buttons of the scroll bar will scroll continuously after being pressed. So how to make such a call to a common button? There are many ways to solve such problems as clock, loop, thread and application. doevent. However, it is better to use background threads in a simple way, so I will only use the thread mode here.
For example, if you have a button named _ pgdnbtn, you want to perform continuous processing by pressing the left button. The processing function is toscroll. Private Button _ pgdnbtn;
Private Thread _ loopmousedownthread;
Private Bool _ Isdown;
Void _ Pgdnbtn_mousedown ( Object Sender, mouseeventargs E)
{
If (E. Button = Mousebuttons. Left)
{
_ Isdown = True ;
_ Isscrolldown = True ;
_ Loopmousedownthread = New Thread ( New Threadstart (toscroll ));
_ Loopmousedownthread. isbackground = True ;
_ Loopmousedownthread. Start ();
}
}
Private Void Toscroll ()
{
// When the mouse is keep down on the button, we will scroll this view
// Until mouse up.
While (_ Isdown)
{
System. Threading. thread. Sleep (thread_sleep_interval );
}
}
Private Void _ Pgdnbtn_mouseup ( Object Sender, mouseeventargs E)
{
_ Isdown= False;
}
Finally, you need to define a mouse up event to exit the thread.
2. Data Synchronization and Interface
Sometimes we need to synchronize data to the interface when the data changes, mainly to call the interface Program . There are also a variety of solutions, such as through attributes, events and representatives. Here I think it is better to use events, so I will focus on how the event method is implemented.
For example, if we have a data set called paw.bjectcollection, we need to tell the interface that the collection element has changed when the set data is added, Public Class Paw.bjectcollection: List < Paw.bject >
{
// This event handler is used to call the dmpaintview updateitemslocation
// Function. That can keep the items location in the correct postion
//
Public Event Eventhandler oncollectionchanged;
Public New Void Add (paw.bject OBJ)
{< br> base . add (OBJ);
If ( null ! = oncollectionchanged)
oncollectionchanged ( This , New eventargs ();
}
}
Add
_ Items. oncollectionchanged + = New Eventhandler (_ items_oncollectionchanged );
Private Void _ Items_oncollectionchanged ( Object Sender, eventargs E)
{
Onpaint ();
}