【问题标题】:Handle user number input value condition with ReadOnly false of numericUpDown使用 numericUpDown 的 ReadOnly false 处理用户编号输入值条件
【发布时间】:2021-09-22 09:24:17
【问题描述】:

在 C# WinForms 桌面应用程序中,我使用 2 个相互依赖的 numericUpDown1 minnumericUpDown2 maxnumericUpDown 控件:

private void numericUpDown1_ValueChanged(object sender, EventArgs e) 
{
    if (numericUpDown1.Value <= numericUpDown2.Value)
    {
        min = (int)numericUpDown1.Value;
    } 
    else
    {
        numericUpDown1.Value = min - 1;
    }
}

private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
    if (numericUpDown2.Value >= numericUpDown1.Value)
    {
        max = (int)numericUpDown2.Value;
    }
    else
    {
        numericUpDown2.Value = max + 1;
    }
} 

使用ReadOnly = true; 来避免手动输入numericUpDown 使最大数量小于最小值。:

        min = 20;
        max = 1999;

        numericUpDown1.Value = min;
        numericUpDown2.Value = max;

        numericUpDown1.ReadOnly = true;
        numericUpDown2.ReadOnly = true;

        numericUpDown1.Increment = 1;
        numericUpDown2.Increment = 1;

        numericUpDown1.Maximum = 2000;
        numericUpDown1.Minimum = 1;
        numericUpDown2.Maximum = 2000;
        numericUpDown2.Minimum = 1;

但我使用的范围很大,从 1 到 2000,并且希望允许用户使用 ReadOnly = false; 手动更改 numericUpDown 的数量。

我正在想办法,如何用numericUpDownReadOnly = false; 控制用户输入条件,以避免输入的最大数小于最小值或最小值大于最大值。

【问题讨论】:

  • hmmm 但它会在失去焦点时变为最小值/最大值......问题出在哪里?只需使用MinimumMaximum 属性

标签: c# event-handling readonly numericupdown


【解决方案1】:

试试这个解决方案:

首先设置您的 numericUpDown 属性 ReadOnly=false 或在您的 FormLoad_Function 中编写一行代码,

numericUpDown1.ReadOnly = false;
numericUpDown2.ReadOnly = false;

然后

private void numericUpDown1_ValueChanged(object sender, EventArgs e) 
{
    if (numericUpDown1.Value > numericUpDown2.Value)
    {
        numericUpDown1.Value = numericUpDown1.Value - 1;
        MessageBox.Show("Min value always less then Max value");
    } 
}

private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
    if (numericUpDown1.Value > numericUpDown2.Value)
    {
        numericUpDown2.Value = numericUpDown2.Value + 1;
        MessageBox.Show("Max value always greater then Min value");
    } 
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多