What you have to say about event processing-do not trigger the event without any need.

Source: Internet
Author: User
In the winform programming process, event processing is a very common programming activity, but I found that events that do not want to be executed frequently appear in my project. Program But it is often executed. A common example is the selectedindexchanged event of ComboBox. This event occurs when a new item is selected from the ComboBox list, the purpose of processing this event is to perform other processing when selecting a new project, such as verifying or querying other data.
In another business logic of our program, the function code and usage code of different IDT components change with IDT. The IDT code and use code are both enumeration values. Therefore, the ComboBox is selected on the interface to display the values of the three, and the selectedindexchanged event is used to implement the constraints of its business rules. In the process of data creation, the event processing is no problem. The problem is that the same window is used for both data input and data display, at the same time, when data is modified, event processing becomes problematic. For example, when querying the attributes of a component, its IDT, function code, and usage code have been stored in the database. In this case, you only need to display the Code. However, as there is no control, so when the IDT value changes, the selectedindexchanged event will also be executed, which is equivalent to a useless task and affects the program performance.
So what is the correct approach? The following provides a control policy for your reference:
Private bool ischanging = false;

Private void cmbpartchart2_dropdownclosed (Object sender, eventargs E)
{
Ischanging = true;
}

Private void cmbpartchart2_selectedindexchanged (Object sender, eventargs E)
{
If (ischanging) // The event is activated only when the selected change occurs.
{
If (this. cmbpartchart2.selectedindex! =-1)
{
Mselectiontext = (listitem <string, string>) This. cmbpartchart2.selecteditem). dispmember;
Mselectionvalue = (listitem <string, string>) This. cmbpartchart2.selecteditem). valuemember;
}
If (_ tyselectedindexchanged! = NULL)
{
_ Tyselectedindexchanged (sender, e );
}
}
}

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.