Uber-n00b

Can you assign different functionality to the up and down buttons on a specific nud I'm trying to create a very common game element. Assume a player/user has a certain number of points to spend stored in control nud1. When they spend a point by clicking the up button in nud2, nud1 should decrease to show they have one less point to spend. Simiarly, if the player changes their mind, and they decrease nud2, it should increase nud1. The closest event I found is "valueChanged", but this code triggers regardless of which up or down arrow is clicked. I'm pretty new with programming as a whole. Any code snippets would be appreciated.



Re: Windows Forms General numericUpDown Affecting Another numericUpDown

nobugz

Try something like this:

public partial class Form1 : Form {
private decimal mPrevNud1;
private decimal mPrevNud2;
private bool mUpdating;
public Form1() {
InitializeComponent();
mPrevNud1 = numericUpDown1.Value = 10;
mPrevNud2 = numericUpDown2.Value = 10;
numericUpDown1.ValueChanged += numericUpDown1_ValueChanged;
numericUpDown2.ValueChanged += numericUpDown2_ValueChanged;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e) {
if (!mUpdating) {
mUpdating = true;
decimal value2 = numericUpDown2.Value - numericUpDown1.Value + mPrevNud1;
if (value2 >= numericUpDown2.Minimum && value2 <= numericUpDown2.Maximum) numericUpDown2.Value = value2;
mUpdating = false;
}
mPrevNud1 = numericUpDown1.Value;
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e) {
if (!mUpdating) {
mUpdating = true;
decimal value1 = numericUpDown1.Value - numericUpDown2.Value + mPrevNud2;
if (value1 >= numericUpDown1.Minimum && value1 <= numericUpDown1.Maximum) numericUpDown1.Value = value1;
mUpdating = false;
}
mPrevNud2 = numericUpDown2.Value;
}
}





Re: Windows Forms General numericUpDown Affecting Another numericUpDown

Uber-n00b

Works flawlessly. Thanks so much for the detailed and prompt response. You're on my XMas card list Smile

Brian