C # Tip 2

Source: Internet
Author: User
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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.