简体   繁体   English

C#进度栏最小值未从文本框中更新

[英]C# Progress Bar Min value is not updating from textbox

Im trying to work with progress bar in C# WFA. 我正在尝试使用C#WFA中的进度栏​​。 My code works but i have problem with Min value of progress bar. 我的代码有效,但是进度栏的最小值不正确。 It only works when i enter max value first not min. 仅当我先输入最大值而不是最小值时才有效。 When i enter min value first it starts from 0 every time i dont know why. 当我第一次输入最小值时,每次我都不知道为什么从0开始。 Any ideas? 有任何想法吗?

    private void textBoxWartosc_TextChanged(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(textBoxWartosc.Text))
        {

        }
        else
        if ((Convert.ToInt32(textBoxWartosc.Text)) >= (Convert.ToInt32(textBoxMin.Text)) && (Convert.ToInt32(textBoxWartosc.Text)) <= (Convert.ToInt32(textBoxMax.Text)))
        {
            int i = Convert.ToInt32(textBoxWartosc.Text);
            progressBar1.Value = i;
        }
    }


    private void textBoxMax_TextChanged(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(textBoxMax.Text);
        progressBar1.Maximum = i;
    }

    private void textBoxMin_TextChanged(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(textBoxMin.Text);
        progressBar1.Minimum = i;
    }

First let's take a look at how the ProgressBar behaves. 首先,让我们看一下ProgressBar的行为。 The main rule is that the minimum will always be less than or equal to the maximum. 主要规则是最小值始终小于或等于最大值。 So if you change the value of maximum to something less than the minimum, you are changing the minimum too. 因此,如果您将最大值更改为小于最小值的值,那么您也在更改最小值。 clear? 明确?

So, I guess this is the case here: first you change the minimum to something lets say 120, you'll have: 所以,我想这里就是这种情况:首先,您将最小值更改为可以说120的值,您将拥有:

minumum = 120 , maximum = 120    // maximum also changes from 100 to 120 based on the rule

then you try to input the maximum amount to let's say 180 , but the TextChanged event gets called three times. 那么您尝试输入最大数量,例如180 ,但是TextChanged事件被调用了3次。 First, as soon as you insert the "1" , so you will have: 首先,一旦您插入"1" ,您将拥有:

minimum = 1 , maximum = 1    // minimum also changes because 1 is less than 100

Second, when you enter "8" , now you will have: 第二,当您输入"8" ,您将拥有:

minimum = 1 , maximum = 18   // now minimum stays 1

and then when you enter the final "0" and the final situation will be: 然后,当您输入最终的"0" ,最终情况将是:

minimum = 1 , maximum = 180

There are lot's of ways to solve this, but I suggest 有很多方法可以解决这个问题,但我建议

1: you use the Leave event instead of TextChanged 1:您使用Leave事件而不是TextChanged

or 2: put a Save button there and change alter values only when the button is pressed. 或2:将“ Save按钮放在那里,仅在按下按钮时更改更改值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM