I am using Microsoft Visual C# 2010 Express. When i change the value of numericUpDown using arrows, my button becomes enable. But i also want to enable my button when i change the value of numericUpDown by changing the text directly.

I am using the following code:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    button1.Enabled = true;
}
有帮助吗?

解决方案

You may need to use TextChanged event instead of using ValueChanged. The Value changed event need you to press enter key after changing value to get ValueChanged fired.

What MSDN say about NumericUpDown.ValueChanged Event

For the ValueChanged event to occur, the Value property can be changed in code, by clicking the up or down button, or by the user entering a new value that is read by the control. The new value is read when the user hits the ENTER key or navigates away from the control. If the user enters a new value and then clicks the up or down button, the ValueChanged event will occur twice, MSDN.

Binding TextChanged event.

private void TestForm_Load(object sender, EventArgs e)
{
    numericUpDown1.TextChanged += new EventHandler(numericUpDown1_TextChanged);
}

Declaration of TextChanged event.

void numericUpDown1_TextChanged(object sender, EventArgs e)
{
    button1.Enabled = true;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top