473,237 Members | 1,384 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,237 software developers and data experts.

Disable events temporarily

Hello everybody,

I have a little "problem".

I have a DataGridView that is loaded automatically by code sometimes, and
manually by the user other times.

When the user introduces data in the DataGridView, there are some events
that control the introduced data for any errors the user can do. That's OK.
But when the data is loaded automatically, it is supposed that this data is
correct, so I don't need that these events are called.
I want to do that because with a lot of data, the load is very slowly, and
with the events disabled it takes only few seconds.

Anyone can help me, and tell me how can I disable these events?

I have done that, but it doesn't work.

Thanks in advance!

Marc

private void EventDisabler(bool Enable)

{

if (Enable)

{

//Enable event handler

dgvItems.CellClick += new DataGridViewCellEventHandler(dgvItems_CellClick);

dgvItems.CellValueChanged += new
DataGridViewCellEventHandler(dgvItems_CellValueCha nged);

dgvItems.CellEndEdit += new
DataGridViewCellEventHandler(dgvItems_CellEndEdit) ;

dgvItems.CellValidated += new
DataGridViewCellEventHandler(dgvItems_CellValidate d);

dgvItems.RowEnter += new DataGridViewCellEventHandler(dgvItems_RowEnter);

dgvItems.RowLeave += new DataGridViewCellEventHandler(dgvItems_RowLeave);

dgvItems.RowValidated += new
DataGridViewCellEventHandler(dgvItems_RowValidated );

}

else

{

//Disable event handler

dgvItems.CellClick -= new DataGridViewCellEventHandler(dgvItems_CellClick);

dgvItems.CellValueChanged -= new
DataGridViewCellEventHandler(dgvItems_CellValueCha nged);

dgvItems.CellEndEdit -= new
DataGridViewCellEventHandler(dgvItems_CellEndEdit) ;

dgvItems.CellValidated -= new
DataGridViewCellEventHandler(dgvItems_CellValidate d);

dgvItems.RowEnter -= new DataGridViewCellEventHandler(dgvItems_RowEnter);

dgvItems.RowLeave -= new DataGridViewCellEventHandler(dgvItems_RowLeave);

dgvItems.RowValidated -= new
DataGridViewCellEventHandler(dgvItems_RowValidated );

}

}

Nov 3 '06 #1
4 16776
I take it by "it doesn't work" you mean that they are still firing.
What you have should work, if used correctly. Is it possible that it has
been enabled twice (without an unsubscribe) through some route? As if so, it
will be subscribed twice, and fired twice. This will a: be slower, and b: if
you unsubscribe (-=) it will only remove the first found instance, perhaps
leaving one behind.

If this is the case, I would sugggest either keeping a bool field to track
if you think you are currently subscribed, or, *always* run the -= (which
unsubscribes if possible), then (if enabled) run the +=, so you can only
ever have 1 instance of each. The bool is cleaner.

Marc
Nov 3 '06 #2
Thanks Marc,

I thought before to do that with the bool field, but it will always rise the
event and then compare the bool, isn't it?

Finally I choose the option to run always the -= .

Thanks a lot for your help.

Marc
"Marc Gravell" <ma**********@gmail.comescribió en el mensaje
news:e0**************@TK2MSFTNGP04.phx.gbl...
>I take it by "it doesn't work" you mean that they are still firing.
What you have should work, if used correctly. Is it possible that it has
been enabled twice (without an unsubscribe) through some route? As if so,
it will be subscribed twice, and fired twice. This will a: be slower, and
b: if you unsubscribe (-=) it will only remove the first found instance,
perhaps leaving one behind.

If this is the case, I would sugggest either keeping a bool field to track
if you think you are currently subscribed, or, *always* run the -= (which
unsubscribes if possible), then (if enabled) run the +=, so you can only
ever have 1 instance of each. The bool is cleaner.

Marc

Nov 3 '06 #3
No, I meant in your method... i.e.

bool eventsBound = false;
void BindEvents(bool enable) {
if(enable && eventsBound) return; // nothing to do; already bound...
if(!enable && !eventsBound) return; // nothing to do; not bound...

if(enable) {
someControl.SomeEvent += someHandler;
someControl.SomeOtherEvent += someOtherHandler;
} else {
someControl.SomeEvent -= someHandler;
someControl.SomeOtherEvent -= someOtherHandler;
}
eventsBound = enable;
}

Marc
Nov 3 '06 #4
Thank you very much for your help.

It works fine now.

Marc
"Marc Gravell" <ma**********@gmail.comescribió en el mensaje
news:em**************@TK2MSFTNGP03.phx.gbl...
No, I meant in your method... i.e.

bool eventsBound = false;
void BindEvents(bool enable) {
if(enable && eventsBound) return; // nothing to do; already bound...
if(!enable && !eventsBound) return; // nothing to do; not bound...

if(enable) {
someControl.SomeEvent += someHandler;
someControl.SomeOtherEvent += someOtherHandler;
} else {
someControl.SomeEvent -= someHandler;
someControl.SomeOtherEvent -= someOtherHandler;
}
eventsBound = enable;
}

Marc

Nov 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tuong Do | last post by:
Hi, I am going to insert a large amount (200,000 records) of data into a table Is there a way that I can temporarily disable the log? so that the insertion run faster Thanks in advance
2
by: Dw70 | last post by:
I need write simple program with work in background and have only one function when it is run F1 I disable and when I pres for example ctrl+k it (program is terminate) maybe somebody have...
8
by: ORC | last post by:
Hi, Is it possible to disable an event like e.g. disabling: private void textBox1_TextChanged(object....) ? Thanks, Ole
7
by: Scott Emick | last post by:
How can I disable events for the controls on a form? I tried setting the form's enable property to false, but that doesn't stop events from firing on its controls. I need to temporarily disable...
4
by: nick | last post by:
Is it possible to disable/suppress alert popups in javascript? I need to write a function that will loop through a form's inputs and "manually" fire their onchange events (if found). Some of those...
4
by: Jon Slaughter | last post by:
Is there any method to temporarily disable focus changing?(I assume only method is tab or mouse?) This problem has been tieing me up for a while and nothing seems to work. The only thing that I...
10
by: Gary Jefferson | last post by:
Suppose I have 3 modules that belong to a project, 'A', 'A.a' and 'B', and each module has its own logger, created with: module1logger = logging.getLogger('project.A') and module2logger =...
37
by: Vince C. | last post by:
Hi all. I've installed Bloodshed Dev-C++ on a Windows 2000 SP4 machine. I'm using MinGW 3.4.2. I'd like to temporarily disable standard functions to write to stderr, i.e. for instance...
3
hsriat
by: hsriat | last post by:
How can I temporarily disable vertical scrollbar? I have replaced confirmation boxes and alert boxes with inlay popup DIVs. But while they are on the screen, I need to disable scrollbar of the...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.