How to disable the numericupdown event firing?

  • Thread starter Thread starter tommaso.gastaldi
  • Start date Start date
T

tommaso.gastaldi

Hello,

A probably dumb question...

does anyone know hot to avoid that if one keep the mouse pressed on an
arrow of the numericUpDown it continues to fire events (it uses
evidently a timer) ?

I want 1 event for each click, and no timer activation. I have tried
using disable within the handler but does not work.

Any idea?

tommaso
 
I assume you do not want the SelectedIndexChanged event to only fire
once? Why not create a Boolean property in the Form's class, and
manipulate it with the MouseDown and MouseUp events to indicate the
state of the mousebutton. Then the SelectedIndexChanged event can check
this property and if set, exit.
 
hi!
thanks for the reply.
Checking mouse event can't do it, IMHO, as the mouse can ble clicked
out of the arrows, unless one knows how to detect that (?).

(I am not sure what you mean by "SelectedIndexChanged ": I am using VB
2005 and I do not have this event for the "NumericUpDown". Perhaps you
meant another control...).

Any other ideas?

-tommaso
 
I was talking aout the MouseUp and MouseDown events for the numeric
updown control, which fire ONLY when you click on the numeric updown
controls buttons.
 
I have used the NUD control and it most certainly has a
SelectedIndexChanged event, which I use to change the value displayed
in a related textbox. If you are not using any of the NUD's events, I
don't see what your problem is.
 
mmm, well there must be some misundertunding here.

I have just tried and the MouseDown and MouseUp events are raised even
if you do not click on the arrows of the NumericUpDown.
Further I am not able to find the "SelectedIndexChanged" for the
NumericUpDown. Could you provide a pointer to the documentation where
it is dealt with?

At this point I am a little bit confused. I still think we are talking
about different things (perhaps you talk about a ComboBox ??)...

tommaso
 
Sorry, my bad. Not SelectedIndexChanged, ValueChanged.

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref17/html/E_System_Windows_Forms_NumericUpDown_ValueChanged.htm

And you are right, to a degree. The MouseDown and MoustUp event do fire
if the mouse is simply somewhere over the control and the mouse button
is clicked, not when you click on one of the control's up or down
buttons. There may be some way to use them with the ValueChanged event
or some other event to accomplish your desired result.
 
Yes. Let me explain further my problem, which actually I believe is a
BUG of the numericUpDown control which was already present in version
2003, and the VS team does not *seem* to have done anything about it.

ANYTIME you place any code under the ValueChanged event which take a
little while to execute (I do not mean a very long process [which could
justify for instance a new thread], but something within tens of a
second), if you keep the mouse clicked on one arrow you systematically,
that is ALWAYS get the following error:

----------------------------
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="System.Windows.Forms"
StackTrace:
at
System.Windows.Forms.UpDownBase.UpDownButtons.TimerHandler(Object
source, EventArgs args)
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext
context)
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[]
commandLine)
at Trial.My.MyApplication.Main(String[] Args) in
17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
------------------

and there seems anything one can do about it.

That's why I wished to disable the associated timer. Because it causes
an exception I cannot do anything about.

If anyone has suggestion to solve this annoying problem (and I do hope
someone of the VS team is listening) I would like to know.

tommaso
 
Hmm, you know, the app I wrote that uses the NUD control does some
calculations in the ValueChanged event, and I can hold down either the
up or the down button, and I have never (so far) gotten the exception
you get. The calculations are not intense, but are not trivial either.
Maybe I will test it and see if I can re-create your problem.
 
It doesn't take much to reproduce the bug. It is crucial (to get the
exception) that
the called routine allows events (DOEVENTS). I get systematically this
exception
when drawing something within the ValueChanged event.

Here is the BUG demo code. Just create a form with a NumericUpDown and
paste the code.
Run the program. Click on the UP arrow and keep pressed. You will get
the exception very soon.
I don't go beyond 11.

'--------------------------------begin code

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.NumericUpDown1.Maximum = Decimal.MaxValue
End Sub

Private Sub NumericUpDown1_ValueChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
NumericUpDown1.ValueChanged
Me.SomeTaskWithEvents()
End Sub

Sub SomeTaskWithEvents()
System.Threading.Thread.Sleep(10)
Application.DoEvents()
End Sub

End Class

'--------------------------------end code




- Details

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="System.Windows.Forms"
StackTrace:
at
System.Windows.Forms.UpDownBase.UpDownButtons.TimerHandler(Object
source, EventArgs args)
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.DoEvents()
at BUGNUD.Form1.SomeTaskWithEvents() in C:\Documents and
Settings\User\Documenti\Visual Studio
2005\Projects\BUGNUD\BUGNUD\Form1.vb:line 13
at BUGNUD.Form1.NumericUpDown1_ValueChanged(Object sender,
EventArgs e) in C:\Documents and Settings\User\Documenti\Visual Studio
2005\Projects\BUGNUD\BUGNUD\Form1.vb:line 8
at System.Windows.Forms.NumericUpDown.OnValueChanged(EventArgs
e)
.....
 
In the mouse down event, use the "Remove Handler" to remove the ValueChanged
Event handler then in the MouseUp event, use the Add Handler to restore event
handling in the Value Changed Event. This should work.
 
hi Dennis,

MouseUp/Down cannot be used to place semaphores or detach handlers as
they are fired even when the mouse is clicked on an area of the NUD
different from arrows.

Further, the ValueChanged is executed under a handles clause and cannot
be removed that way,...

Could you actually show your suggestion applied to the Bug code?

tommaso
 
MouseUp/Down events CAN be used to remove/add handlers. The following works
except that the ValueChanged event is fired ONCE before the mousedown event
is fired when the mouse button is held down.

Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles NumericUpDown1.ValueChanged

'Will fire one time when the mouse is pressed and held then not fire
again until the function is called from the MouseUp event.

End Sub

Private Sub NumericUpDown1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles NumericUpDown1.MouseDown
RemoveHandler NumericUpDown1.ValueChanged, AddressOf
NumericUpDown1_ValueChanged

End Sub

Private Sub NumericUpDown1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles NumericUpDown1.MouseUp
AddHandler NumericUpDown1.ValueChanged, AddressOf
NumericUpDown1_ValueChanged
Dim e1 As New System.EventArgs
'NumericUpDown1_ValueChanged(sender, e1)
End Sub
 
Good! You are right.

You are almost there. There seems to be a last problem. If one clicks
on UpArrow and release the mouse and repeat this process starting, for
instance, from 1, the ValueChanged events is fired only for the odd
numbers. That is in a sequence of clicks only half of them rise the
ValueChanged event. But one would like to get the event for each
click...

Any idea?

tommaso
 
mmm, I am afraid that this isn't the right approach. There seems to be
no way to make it work right.
Perhaps another approach could be working by reflection and disabling
the internal timer.
But, It would probably be easier to create a custom NUD!

Such a small control is so defective! I still do not understand how
come takes so long (years) to fix it !

-t
 
Here is a stupid workaround which seems to work.

I include a complete demo. Requires a NUD and a CheckBox. Whem the
CheckBox is not checked
the program show the NUD "out of sync" (is that a good name?) BUG, if
checked implements the dumb
workaround to stop continuous event firing (which causes bug).

If you have more suggestions, please let me know. And let's hope that
they fix soon the NUD
this is the kind of things that make Infragistic (and similar business)
happy, I guess !


Public Class Form1

Private DisableFiring As Boolean

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Text = "NUD BUG Demo"
Me.CheckBox1.Text = "Disable continuous firing"
Me.NumericUpDown1.Maximum = Decimal.MaxValue
End Sub

Private Sub NumericUpDown1_ValueChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
NumericUpDown1.ValueChanged
If DisableFiring Then Me.NumericUpDown1.Enabled = False
Me.SomeTaskWithEvents()
If DisableFiring Then NumericUpDown1.Enabled = True
End Sub

Sub SomeTaskWithEvents()
System.Threading.Thread.Sleep(10)
Me.Text = "Value " & Me.NumericUpDown1.Value
Application.DoEvents()
End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
Me.DisableFiring = Me.CheckBox1.Checked
End Sub

End Class
 
Yes, it's too bad they don't fix the ValueChanged event on the NumericUpDown
control. It should fire the ValueChanged event AFTER the Click event which
is fired after the MouseUp event OR have a BeforeValueChange event with a
cancel option. Many of Ms controls are like this. I don't think they put
much thought into their controls...third party controls are much better
normally.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top