Sign in to follow this  

C# Windows form application controls question

This topic is 3629 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've recently started learning C# using the book "microsoft visual studio 2005 step by step", as well as reading the C# documentation, I recently encountered a problem whilst using the "NumericUpDown" control: The form design view will generate the control, and a method such as the following:
private void numericValue_ValueChanged(object sender, EventArgs e)

the trouble is, this method doesn't really care if you increase or decrease the value. the value itself is important, but i need to update other variables depending on weather or not this value increases or decreases, as I am using this to allocate a number of points to various attributes. I can't find any mention in any documentation i have seen of a method such as
private void numericValue_ValueIncreased(object sender, EventArgs e)

does such a method increase? is there a way to define one? Thannks in advance for your help.

Share this post


Link to post
Share on other sites
I remember having this issue last year, with .NET 2.0 obviously. At the time, there was no such feature, however I don't know if it exists in 3.5.

You can solve it by storing a temporary variable and subtract from it with the new value whenever it's changed.

difference = oldValue - newValue

Share this post


Link to post
Share on other sites
Do you really need different events for an increase and a decrease? Don't you just want to inform all fields that depend on this value that it has changed so they can read it back in and figure out their new value?

Heptagonal's approach would work fine and you can also define your own increase and decrease events if you want to seperate out the code.

Share this post


Link to post
Share on other sites
Quote:
Original post by Metorical
Do you really need different events for an increase and a decrease? Don't you just want to inform all fields that depend on this value that it has changed so they can read it back in and figure out their new value?

Heptagonal's approach would work fine and you can also define your own increase and decrease events if you want to seperate out the code.


yes. if you increase the value, a point is assigned to an attribute, and your points remaining are reduced. if you decrease the value, a point is freed up to be spent on a different attribute.

Share this post


Link to post
Share on other sites
Yes, but the correct way to deal with this is to recompute the other fields explicitly based on the new value. That way, you're not limited to changing things one at a time, if you decide to let users type in numbers, or use a control where they can change the number faster.

Essentially, whenever a value changes on the form, just inform everyone else, and have the derived numbers be fully recalculated. You can centralize this code in a single function, so that if you have three different stats to spend points on, your value-changed event for each of those controls just calls the central function, which then does:

PointsRemainingControl.Value = TotalPoints - (Stat1.Value + Stat2.Value + Stat3.Value);

Share this post


Link to post
Share on other sites
Quote:
Original post by osmanb
Yes, but the correct way to deal with this is to recompute the other fields explicitly based on the new value. That way, you're not limited to changing things one at a time, if you decide to let users type in numbers, or use a control where they can change the number faster.

Essentially, whenever a value changes on the form, just inform everyone else, and have the derived numbers be fully recalculated. You can centralize this code in a single function, so that if you have three different stats to spend points on, your value-changed event for each of those controls just calls the central function, which then does:

PointsRemainingControl.Value = TotalPoints - (Stat1.Value + Stat2.Value + Stat3.Value);


quite right. I guess sometimes it takes a fresh set of eyes to see the best way forward. i've created an "update" method to handle something like the above, and it works fine - a few more additions need to be made, but it's all running smoothly.

cheers, guys.

EDIT:
with further testing, i've noticed that, despite running the update function on load, and whenever any stat is changed, it's still one update behind. for example, if I add one point to a value, the "remaining points" variable is not updated, however if i then remove that point, then the remaining points variable updates to show that one point has been spent.

any ideas why this might be the case?

[Edited by - Malazar on January 10, 2008 6:36:25 AM]

Share this post


Link to post
Share on other sites

This topic is 3629 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this