TextChanged Event of NumericUpDown



我使用的是Microsoft Visual C#2010学习版。当我使用箭头更改numericUpDown的值时,我的按钮变为启用。但当我通过直接更改文本来更改numericUpDown的值时,我也想启用我的按钮。

我正在使用以下代码:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    button1.Enabled = true;
}

您可能需要使用TextChanged事件,而不是使用ValueChanged。Value changed事件需要您在更改值后按enter键才能激发ValueChanged。

MSDN对NumericUpDown.ValueChanged事件的评价

若要发生ValueChanged事件,可以更改Value属性在代码中,通过单击向上或向下按钮,或者通过用户输入控件读取的新值当用户点击ENTER键或导航离开控件。如果用户输入一个新值,然后单击向上或向下按钮ValueChanged事件将发生两次,MSDN。

绑定TextChanged事件

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

TextChanged事件的声明

void numericUpDown1_TextChanged(object sender, EventArgs e)
{
    button1.Enabled = true;
}

最新更新