473,386 Members | 1,821 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,386 software developers and data experts.

any way to suspend events for controls while I edit them?

If I have a control like say a drop down list and I have some kind of
onSelectItem change event, is there a way to temporarily suspend the event
handling (without removing the event and then re-attaching it?) while I edit
it's contents which would otherwise cause the event to fire?

Or is there maybe a way in my event handler to tell if the user triggered it
as opposed to the program itself?
Oct 5 '06 #1
8 1987
Or is there maybe a way in my event handler to tell if the user triggered it
as opposed to the program itself?
I faced the same problem some time ago.
I used, I'd say, a not so elegant way to solve this, but it worked for
me.
I used a bool value to indicate that the value is changed by me,
programmatically.
Like this:

class myClass {
private bool m_flag = false;
//...other members
private void someMethod() {
m_flag = true;
try{
//change the index of your combobox here; this will
autommatically call MyHandler() - see below
}
finally{
m_flag = false;
}
}
}

inside the event handler that handles the event:

private void MyHandler(EventHandler e) {
if(m_flag)
return;
//...code
}

As I said, it doesn't look very pretty to me, but it worked for me.
If anyone has a better solution, I'm also interested.

Thanks.

Oct 5 '06 #2

"MrNobody" <Mr******@discussions.microsoft.comwrote in message
news:67**********************************@microsof t.com...
If I have a control like say a drop down list and I have some kind of
onSelectItem change event, is there a way to temporarily suspend the event
handling (without removing the event and then re-attaching it?) while I
edit
it's contents which would otherwise cause the event to fire?

Or is there maybe a way in my event handler to tell if the user triggered
it
as opposed to the program itself?
When do you "edit" the contents of the control? At design time? Run-time?
If at design time, do you want to suspend the events always at design-time?

I'm a little confused on this :)

Mythran
Oct 5 '06 #3

nano2k wrote:
Or is there maybe a way in my event handler to tell if the user triggered it
as opposed to the program itself?

I faced the same problem some time ago.
I used, I'd say, a not so elegant way to solve this, but it worked for
me.
I used a bool value to indicate that the value is changed by me,
programmatically.
Like this:

class myClass {
private bool m_flag = false;
//...other members
private void someMethod() {
m_flag = true;
try{
//change the index of your combobox here; this will
autommatically call MyHandler() - see below
}
finally{
m_flag = false;
}
}
}

inside the event handler that handles the event:

private void MyHandler(EventHandler e) {
if(m_flag)
return;
//...code
}

As I said, it doesn't look very pretty to me, but it worked for me.
If anyone has a better solution, I'm also interested.

Thanks.
I use essentially the same solution. It may not be pretty, but it's
prettier than unsubscribing and re-subscribing to events, which gets
uglier and uglier as the number of controls grows....

Oct 5 '06 #4
by using a drop down like that have you thought of making a new type of drop
down that reacts differently?

So like

class EditDropDown : DropDownList

and then change that drop down to react differently in the circumstances. So
you could override the event handler to pass the boolean value that it was
changed by you or not by you. Allowing your code using it to be much neater
but using the same methodology. Then the control itself could do all the
working out for you, you just check the boolean. Make sense?
"MrNobody" <Mr******@discussions.microsoft.comwrote in message
news:67**********************************@microsof t.com...
If I have a control like say a drop down list and I have some kind of
onSelectItem change event, is there a way to temporarily suspend the event
handling (without removing the event and then re-attaching it?) while I
edit
it's contents which would otherwise cause the event to fire?

Or is there maybe a way in my event handler to tell if the user triggered
it
as opposed to the program itself?

Oct 5 '06 #5

I have to add, i am with Mythran, i am confused by this also but 'boing with
the flow' my other answer is stil how i would do it. Tho as i was writing it
i was thinking i have no idea how you will set that bool that you changed
the data. But anyway.....
"Daniel" <Da*****@vestryonline.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
by using a drop down like that have you thought of making a new type of
drop down that reacts differently?

So like

class EditDropDown : DropDownList

and then change that drop down to react differently in the circumstances.
So you could override the event handler to pass the boolean value that it
was changed by you or not by you. Allowing your code using it to be much
neater but using the same methodology. Then the control itself could do
all the working out for you, you just check the boolean. Make sense?
"MrNobody" <Mr******@discussions.microsoft.comwrote in message
news:67**********************************@microsof t.com...
>If I have a control like say a drop down list and I have some kind of
onSelectItem change event, is there a way to temporarily suspend the
event
handling (without removing the event and then re-attaching it?) while I
edit
it's contents which would otherwise cause the event to fire?

Or is there maybe a way in my event handler to tell if the user triggered
it
as opposed to the program itself?


Oct 5 '06 #6
Setting the bool is simple. To understand how it works, you have to go
back to the original problem, which restated, looks like this:

"Have event handlers that I want to invoke whenever the user
manipulates a control, but I don't want to invoke them when I
manipulate the control from within my program."

Unfortunately, the .NET framework doesn't provide a mechanism for
distinguishing between events that result from user actions from events
that result from you messing with the controls in your program. Cue the
boolean.

All you do is create a boolean in your class:

private bool _programIsChangingControls = false;

Then, whenever you write some code that populates a ComboBox, or
changes check boxes in a ListView, or something like that, you do this:

private void SomeFunction()
{
this._programIsChangingControls = true;
this.comboBox1.SelectedItem = ... ;
this._programIsChangingControls = false;
}

and then, in your event handler:

private void comboBox1_SelectedIndexChanged(void sender,
System.EventArgs e)
{
if (!this._programIsChangingControls)
{
... handle the event ...
}
}

I do the same sort of thing with validation routines: unfortunately the
Framework doesn't provide a way to cancel pending validation events, so
I just create a flag that tells the event handlers to not bother
validating, then when I need to flush validation events I set the flag,
force validation, and unset the flag.

Daniel wrote:
I have to add, i am with Mythran, i am confused by this also but 'boing with
the flow' my other answer is stil how i would do it. Tho as i was writing it
i was thinking i have no idea how you will set that bool that you changed
the data. But anyway.....
"Daniel" <Da*****@vestryonline.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
by using a drop down like that have you thought of making a new type of
drop down that reacts differently?

So like

class EditDropDown : DropDownList

and then change that drop down to react differently in the circumstances.
So you could override the event handler to pass the boolean value that it
was changed by you or not by you. Allowing your code using it to be much
neater but using the same methodology. Then the control itself could do
all the working out for you, you just check the boolean. Make sense?
"MrNobody" <Mr******@discussions.microsoft.comwrote in message
news:67**********************************@microsof t.com...
If I have a control like say a drop down list and I have some kind of
onSelectItem change event, is there a way to temporarily suspend the
event
handling (without removing the event and then re-attaching it?) while I
edit
it's contents which would otherwise cause the event to fire?

Or is there maybe a way in my event handler to tell if the user triggered
it
as opposed to the program itself?
Oct 5 '06 #7
Oh i see!

So he wants the event to not fire when his CODE changes the value not HIM as
a person clicking as it implied before. I get it now.

Umm, for a list box for the user to change the value they would have to
first click the mouse button or some other input key right? Could you not
make a new control that inherits from the listbox control, and have the
boolean set by that mouse click? Or something similar to distinguish a user
change over your own? That would be one way of a generic way of reading the
user, which would allow a control that only fires that even on a user change
as oppose to a code change of the value.

It all depends on the timing of the event si guess. Does the event fire
once a user has highlightes a new option and clicks the mouse on it?
Similarly does it fire if they press the up or down key when the control has
focus?

You could use focus as a check, override the selection changed event in your
inherited control to say if has focus and mouse clicked or key pressed then
user change so fire event for change made and pass vars in. Otherwise dont
fire event. Would that work?

"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Setting the bool is simple. To understand how it works, you have to go
back to the original problem, which restated, looks like this:

"Have event handlers that I want to invoke whenever the user
manipulates a control, but I don't want to invoke them when I
manipulate the control from within my program."

Unfortunately, the .NET framework doesn't provide a mechanism for
distinguishing between events that result from user actions from events
that result from you messing with the controls in your program. Cue the
boolean.

All you do is create a boolean in your class:

private bool _programIsChangingControls = false;

Then, whenever you write some code that populates a ComboBox, or
changes check boxes in a ListView, or something like that, you do this:

private void SomeFunction()
{
this._programIsChangingControls = true;
this.comboBox1.SelectedItem = ... ;
this._programIsChangingControls = false;
}

and then, in your event handler:

private void comboBox1_SelectedIndexChanged(void sender,
System.EventArgs e)
{
if (!this._programIsChangingControls)
{
... handle the event ...
}
}

I do the same sort of thing with validation routines: unfortunately the
Framework doesn't provide a way to cancel pending validation events, so
I just create a flag that tells the event handlers to not bother
validating, then when I need to flush validation events I set the flag,
force validation, and unset the flag.

Daniel wrote:
>I have to add, i am with Mythran, i am confused by this also but 'boing
with
the flow' my other answer is stil how i would do it. Tho as i was writing
it
i was thinking i have no idea how you will set that bool that you changed
the data. But anyway.....
"Daniel" <Da*****@vestryonline.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
by using a drop down like that have you thought of making a new type of
drop down that reacts differently?

So like

class EditDropDown : DropDownList

and then change that drop down to react differently in the
circumstances.
So you could override the event handler to pass the boolean value that
it
was changed by you or not by you. Allowing your code using it to be
much
neater but using the same methodology. Then the control itself could do
all the working out for you, you just check the boolean. Make sense?
"MrNobody" <Mr******@discussions.microsoft.comwrote in message
news:67**********************************@microsof t.com...
If I have a control like say a drop down list and I have some kind of
onSelectItem change event, is there a way to temporarily suspend the
event
handling (without removing the event and then re-attaching it?) while
I
edit
it's contents which would otherwise cause the event to fire?

Or is there maybe a way in my event handler to tell if the user
triggered
it
as opposed to the program itself?


Oct 5 '06 #8

"Daniel" <Da*****@vestryonline.comwrote in message
news:ur**************@TK2MSFTNGP06.phx.gbl...
Oh i see!

So he wants the event to not fire when his CODE changes the value not HIM
as a person clicking as it implied before. I get it now.

Umm, for a list box for the user to change the value they would have to
first click the mouse button or some other input key right? Could you not
make a new control that inherits from the listbox control, and have the
boolean set by that mouse click? Or something similar to distinguish a
user change over your own? That would be one way of a generic way of
reading the user, which would allow a control that only fires that even on
a user change as oppose to a code change of the value.

It all depends on the timing of the event si guess. Does the event fire
once a user has highlightes a new option and clicks the mouse on it?
Similarly does it fire if they press the up or down key when the control
has focus?

You could use focus as a check, override the selection changed event in
your inherited control to say if has focus and mouse clicked or key
pressed then user change so fire event for change made and pass vars in.
Otherwise dont fire event. Would that work?

"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>Setting the bool is simple. To understand how it works, you have to go
back to the original problem, which restated, looks like this:

"Have event handlers that I want to invoke whenever the user
manipulates a control, but I don't want to invoke them when I
manipulate the control from within my program."

Unfortunately, the .NET framework doesn't provide a mechanism for
distinguishing between events that result from user actions from events
that result from you messing with the controls in your program. Cue the
boolean.

All you do is create a boolean in your class:

private bool _programIsChangingControls = false;

Then, whenever you write some code that populates a ComboBox, or
changes check boxes in a ListView, or something like that, you do this:

private void SomeFunction()
{
this._programIsChangingControls = true;
this.comboBox1.SelectedItem = ... ;
this._programIsChangingControls = false;
}

and then, in your event handler:

private void comboBox1_SelectedIndexChanged(void sender,
System.EventArgs e)
{
if (!this._programIsChangingControls)
{
... handle the event ...
}
}

I do the same sort of thing with validation routines: unfortunately the
Framework doesn't provide a way to cancel pending validation events, so
I just create a flag that tells the event handlers to not bother
validating, then when I need to flush validation events I set the flag,
force validation, and unset the flag.

Daniel wrote:
>>I have to add, i am with Mythran, i am confused by this also but 'boing
with
the flow' my other answer is stil how i would do it. Tho as i was
writing it
i was thinking i have no idea how you will set that bool that you
changed
the data. But anyway.....
"Daniel" <Da*****@vestryonline.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl.. .
by using a drop down like that have you thought of making a new type
of
drop down that reacts differently?

So like

class EditDropDown : DropDownList

and then change that drop down to react differently in the
circumstances.
So you could override the event handler to pass the boolean value that
it
was changed by you or not by you. Allowing your code using it to be
much
neater but using the same methodology. Then the control itself could
do
all the working out for you, you just check the boolean. Make sense?
"MrNobody" <Mr******@discussions.microsoft.comwrote in message
news:67**********************************@microso ft.com...
If I have a control like say a drop down list and I have some kind of
onSelectItem change event, is there a way to temporarily suspend the
event
handling (without removing the event and then re-attaching it?) while
I
edit
it's contents which would otherwise cause the event to fire?

Or is there maybe a way in my event handler to tell if the user
triggered
it
as opposed to the program itself?


Another way would be to have a method that changes the value and doesn't
raise the event. This method would just change the internal members of the
control w/o raising the event while the property still calls the OnXXXChange
method which raises the event.

HTH,
Mythran
Oct 6 '06 #9

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

Similar topics

0
by: Andrés Giraldo | last post by:
Hi! I'm adding an asp button to a datagrid on the ItemDataBound event, when the user clicks on this button, I basically remove the button and create other 2 buttons... my problem is.. the 2 last...
2
by: Stephan Steiner | last post by:
Hi I'm using several DataTables in my program which are updated periodically. At the same I have those tables bound to DataGrids in my GUI. So far I've been doing all the processing in the same...
4
by: am | last post by:
Hi to all. I have a little problem. I'm working with threads, and I need to abort or suspend them, but many experts dissuade from use Thread.Abort and Thread.Suspend. As I didn't find other way,...
6
by: Mark | last post by:
I have been working for quite some time on this issue which in theory should be quite simple. The problem is that the Cancel and Save events are not fired when their respective buttons are...
5
by: Ben Fidge | last post by:
I've got a problem where some buttons placed on a user control are only firing their OnClick events when the user clicks on them for the second time. I've got the situation where some common...
2
by: JezB | last post by:
I'm adding WebControl objects to a Page dynamically on Page_Load, but I'm having trouble attaching events to these. For example, adding an image button :- ImageButton nb = new ImageButton();...
3
by: Mike | last post by:
Hi, I am adding controls dynamically in a WebForm, but none of these controls' events fire. Here is the class code I am using. I have tried so many things, but nothing works :-( namespace...
2
by: Mark Broadbent | last post by:
I initially posted a problem in the aspnet.webcontrols and have now found why the problem existed. Just thought I'd post this to see if this was perhaps a known issue. Scenario: I have a simple...
1
by: Dave A | last post by:
I have a problem that I have boiled down to a very simple example. I have a user control that displays a some data from a business object. On one screen I have a collection of these business...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.