ValueChanged event NOT being fired when setting NumericUpDown.Value property

G

Guest

Hi,

I am setting the NumericUpDown .Value property and the ValueChanged event
is NOT being fired. Does this ONLY get fired when I change it on the UI and
not programatically?

Thanks
 
J

Jon Skeet [C# MVP]

I am setting the NumericUpDown .Value property and the ValueChanged event
is NOT being fired. Does this ONLY get fired when I change it on the UI and
not programatically?

I'm not seeing the behaviour you've described. For instance:

using System;
using System.Windows.Forms;

class Test
{

static void Main()
{
NumericUpDown nud = new NumericUpDown();
nud.ValueChanged += new EventHandler (SayHello);
nud.Value = 20m;
}

static void SayHello (object sender, EventArgs e)
{
Console.WriteLine ("Hello");
}
}

displays "Hello" as expected.

Could you produce a similar short but complete program which
demonstrates the problem?
 
G

Guest

I have the control in a PANEL and its got Panel.Enabled = false, then while
its in the false state I update the NumericUpDown.Value property and its
not firing. Does it only fire when its in an Enabled container?
 
J

Jon Skeet [C# MVP]

I have the control in a PANEL and its got Panel.Enabled = false, then while
its in the false state I update the NumericUpDown.Value property and its
not firing. Does it only fire when its in an Enabled container?

If you look at the example I posted, it's not in a container at all.
 
J

Jon Skeet [C# MVP]


How relevant is that? You asked whether the event only fired when it's
in an Enabled container. I pointed out that my example doesn't use a
container at all, so the answer to your question is clearly "no".

Now, as I said before, the best way of resolving this is for you to
come up with a short but complete program which demonstrates the
problem.

See http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.
 
G

Guest

Is that the desired logical behaviour because I am updating a value , its
logically changed but yet its not notified to all subscribers that its
changed. Doesnt smell right to me.




"Mick Doherty"
If the Container is disabled then the control is disabled. Disabled controls
normally do not fire events.
 
G

Guest

As a workaround for this illogical behaviour, I fire the event myself after
i change the property.

Having them NOT fire events when disabled yet theyre still changing doesnt
seem logical at all. In fact I would say its giving incorrect values as the
subscribers think its one value yet its actually NOT that value because its
changed but no event to notify them.
 
J

Jon Skeet [C# MVP]

Its very relevant because the event is NOT firing with it in a disabled
container.

But it's not relevant to your question of whether the event is only
fired when it's in an Enabled container. My code answered that question
- the answer is "no".

However, here's another piece of code which shows that in fact a
control doesn't have to be enabled to fire an event, either:

using System;
using System.Windows.Forms;

class Test
{

static void Main()
{
NumericUpDown nud = new NumericUpDown();
nud.ValueChanged += new EventHandler (SayHello);
nud.Enabled = false;
nud.Value = 20m;
}

static void SayHello (object sender, EventArgs e)
{
Console.WriteLine ("Hello");
}
}

Until you produce a complete example, it's going to be very hard to
help you further.
 
M

Mick Doherty

In fact I just tried it, and I was wrong, the event does fire. There must be
an error in your code.
 
J

Jon Skeet [C# MVP]

My problem is its not firing when its in a disabled container, thats the
issue here.

One last time: post your code. Just what's required, but in a complete
form so that I can compile and immediately run it.

Until you do so, I can't help you any further.
 
G

Guest

How much of an error can I have when I do a NumericUpDown.Value = someValue;
and the subscriber isnt getting it.

Im quite sure Im setting the .Value property and I am subscribed to the
ValueChanged event.

Could you explain where the error is and no im not giving out this
production code. Could be an issue with my version installed. I always
hated mishmashed upgrading scenarios.

In the InitializeComponent() form designer code its clearly subscribed my
event handler to the event.

When I start the form, its set the panel to enabled = false (from the
designer property) and I load up the value and set the .Value property, its
clearly NOT firing. I set a breakpoint and nope, nada.

I now call the event handler directly after setting the .Value property as a
workaround.


"Mick Doherty"
 
G

Guest

Yeah sure let me just post my production code here. Do I look like a gimp?

If I get time I will try to repro it in a small project. I know what Im
doing and I can ssee clearly its not firing.
 
J

Jon Skeet [C# MVP]

Yeah sure let me just post my production code here. Do I look like a gimp?

Did I ask you to post your production code?
If I get time I will try to repro it in a small project. I know what Im
doing and I can ssee clearly its not firing.

Reproducing it in a small project is exactly the thing to do.
 
S

Saurabh

Yeah sure let me just post my production code here. Do I look like a gimp?

No you don't.
If I get time I will try to repro it in a small project. I know what Im
doing and I can ssee clearly its not firing.

Thats exactly what Jon had advised you to do when he sent you a link to his
website explaining what he means by a "short but complete" code, I agree
with him a 100% that if you write a short but complete code, you will be
able to track your problem for yourself.

People are ready to help here, just in case you wondered.

--Saurabh
 
M

Mick Doherty

How much of an error can I have when I do a NumericUpDown.Value =
someValue;
and the subscriber isnt getting it.

Could you explain where the error is and no im not giving out this
production code.

NumericUpDown.Value = 12;

If the Value was already 12 then the Value did not change, and subsequently,
the ValueChanged event will not fire.

I don't program in C#, I'm a VB.Net programmer,but to see the problem I
created a New windows form C# project.

I added a Panel to the form. I added a NumericUpDown Control to the Panel. I
set the Enabled property of the Panel to False. I added a button to the form
and added the following code.

private void button1_Click(object sender, System.EventArgs e)
{
this.numericUpDown1.Value +=1;
}
private void numericUpDown1_ValueChanged(object sender, System.EventArgs e)
{
MessageBox.Show("Hello!");
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


You must have some error, if you do not post your code it's useless.

Ijust did a fast test, I created a form, put a panel, and inside it a
numericupdown, I set the panel as disabled.
also added a button withi this code:

this.numericupdown1.Value = 12;

IT DOES FIRE the event.

Below you will find my full code listing, I left it just as VS generated
it, for you to see it visually.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


*************************************************** START OF CODE

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace WindowsApplication1

{

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Panel panel1;

private System.Windows.Forms.NumericUpDown numericUpDown1;

private System.Windows.Forms.Button button1;

//private System.ComponentModel.Container components = null;

public Form1()

{

InitializeComponent();

}



#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.panel1 = new System.Windows.Forms.Panel();

this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();

this.button1 = new System.Windows.Forms.Button();

this.panel1.SuspendLayout();

((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit(
);

this.SuspendLayout();

//

// panel1

//

this.panel1.Controls.Add(this.numericUpDown1);

this.panel1.Enabled = false;

this.panel1.Location = new System.Drawing.Point(64, 104);

this.panel1.Name = "panel1";

this.panel1.Size = new System.Drawing.Size(176, 96);

this.panel1.TabIndex = 0;

//

// numericUpDown1

//

this.numericUpDown1.Location = new System.Drawing.Point(48, 32);

this.numericUpDown1.Name = "numericUpDown1";

this.numericUpDown1.TabIndex = 0;

this.numericUpDown1.ValueChanged += new
System.EventHandler(this.numericUpDown1_ValueChanged);

//

// button1

//

this.button1.Location = new System.Drawing.Point(200, 48);

this.button1.Name = "button1";

this.button1.TabIndex = 1;

this.button1.Text = "button1";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 273);

this.Controls.Add(this.button1);

this.Controls.Add(this.panel1);

this.Name = "Form1";

this.Text = "Form1";

this.panel1.ResumeLayout(false);

((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();

this.ResumeLayout(false);

}

#endregion

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void numericUpDown1_ValueChanged(object sender, System.EventArgs e)

{

MessageBox.Show("hello");

}

private void button1_Click(object sender, System.EventArgs e)

{

this.numericUpDown1.Value = 12;

}

}

}







*************************************************** END OF CODE


How much of an error can I have when I do a NumericUpDown.Value = someValue;
and the subscriber isnt getting it.

Im quite sure Im setting the .Value property and I am subscribed to the
ValueChanged event.

Could you explain where the error is and no im not giving out this
production code. Could be an issue with my version installed. I always
hated mishmashed upgrading scenarios.

In the InitializeComponent() form designer code its clearly subscribed my
event handler to the event.

When I start the form, its set the panel to enabled = false (from the
designer property) and I load up the value and set the .Value property, its
clearly NOT firing. I set a breakpoint and nope, nada.

I now call the event handler directly after setting the .Value property as a
workaround.


"Mick Doherty"
message news:[email protected]...
In fact I just tried it, and I was wrong, the event does fire. There
must
be
an error in your code.

Is that the desired logical behaviour because I am updating a value , its
logically changed but yet its not notified to all subscribers that its
changed. Doesnt smell right to me.




"Mick Doherty"
<EXCHANGE#[email protected].[mdaudi100#ntlworld.com]>
wrote
in
message If the Container is disabled then the control is disabled. Disabled
controls
normally do not fire events.

Mine is.

I have the control in a PANEL and its got Panel.Enabled = false,
then
while
its in the false state I update the NumericUpDown.Value property
and
its
not firing. Does it only fire when its in an Enabled container?

If you look at the example I posted, it's not in a container at all.
 

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

Top