开发者

How to protect yourself from argumentOutOfRangeException

开发者 https://www.devze.com 2023-03-21 13:29 出处:网络
I have numericUpDown control minValue - 0 maxValue - 100. I create binding to this control. If the value changes to 101 will be the exception, which I do not need, and I would like to value is not s

I have numericUpDown control minValue - 0 maxValue - 100.

I create binding to this control.

If the value changes to 101 will be the exception, which I do not need, and I would like to value is not specifically mentioned. how to do it ?

UPDATE:

BindinHelper.BindField(this.nUpDownExecArea, "Value", TempConfigClass, "ExecArea");

BindField:

public static void BindField(Control control, string propertyName,
               object dataSource, string dataMember)
        {
            Binding bd;

            for (int index = control.DataBindings.Count - 1; (index == 0); index--)
            {
                bd = control.DataBindings[index];
                if (bd.PropertyName == propertyName)
                    control.DataBindings.Remove(bd);
            }
            control.DataBindings.Add(prope开发者_如何学PythonrtyName, dataSource, dataMember, false, DataSourceUpdateMode.OnPropertyChanged);
        }

I set TempConfigClass.ExecArea = 99999;

does not result in errors, but when I go to a tab (tabcontrol) where the error appears to be numericUpDown


An argumentOutOfRangeException is telling you that the argument received was exceptional. However if your app is designed in a way to expect certain arguments there are two ways to go about it:

The recommended way - Simply check the value prior to the binding taking place and prevent it going any further if out of range

or - Use a try/catch block to catch the specific exception only and deal with it accordingly

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号