簡體   English   中英

將NumericUpDown控件的當前值限制為另一個NumericUpDown

[英]Limiting the current value of NumericUpDown control to another NumericUpDown

我在winforms應用程序中有2個NumericUpDown控件,用於Min / Max值。 我想做一些事情,如果MAX是30,那么MIN值不應該超過29,如果我們說MIN值當前是20,那么MAX值不應該低於21。

所以我想要的是MIN和MAX值之間應該總是1。

我試着像下面的代碼那樣邏輯,但它不起作用! 怎么了?

private void numericUpDownChartMin_ValueChanged(object sender, EventArgs e)
{
    var value = numericUpDownChartMin.Value; //Current value

    if (value < numericUpDownChartMax.Value) //if value < MAX
        tempChart.ChartStyle.MinimumValue = value; //Use the value
    else
        numericUpDownChartMin.Value = value; //Keep the value the same
}

private void numericUpDownChartMax_ValueChanged(object sender, EventArgs e)
{
    var value = numericUpDownChartMax.Value; //Current value

    if (value > numericUpDownChartMin.Value) //if value > MAX
        tempChart.ChartStyle.MaximumValue = value; //Use the value
    else
        numericUpDownChartMax.Value = value; //Keep the value the same
}

例!!!!

upDownMÍN當前值為20,upDownMax當前值為30.因此用戶可以將upDownMin值更改為29。

如果將upDownMAX增加到40,則用戶可以將upDownMIN設置為39。

upDownMAX也是如此......用戶不應該將最大值設置為低於upDownMIN值。

    private void numericUpDownChartMin_ValueChanged(object sender, EventArgs e)
    {
         numericUpDownChartMax.Minimum = numericUpDownChartMin.Value + 1;
    }

    private void numericUpDownChartMax_ValueChanged(object sender, EventArgs e)
    {
         numericUpDownChartMin.Maximum = numericUpDownChartMax.Value - 1;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM