【问题标题】:C# set UserControl.Value without invoking the ValueChanged eventC# 设置 UserControl.Value 而不调用 ValueChanged 事件
【发布时间】:2018-05-08 00:01:49
【问题描述】:

我遇到了一个无限循环问题。

我有两个数字上/下控件(高度和宽度输入参数)。当用户更改其中一个控件的值时,我需要缩放另一个以保持高宽比不变。

有没有一种方法可以在不调用 ValueChanged 事件的情况下设置控件的值。我只希望在用户更改值时执行 ValueChanged 事件。

private void FloorLength_ValueChanged(object sender, EventArgs e)
{
    if (this.mCurrentDocument.System.SuperTrakSystem.FloorBitmap != null)
    {
        FloorWidth.Value = FloorLength.Value * 
            ((decimal)this.mCurrentDocument.System.SuperTrakSystem.FloorBitmap.Height / 
            (decimal)this.mCurrentDocument.System.SuperTrakSystem.FloorBitmap.Width);
    }
}

private void FloorWidth_ValueChanged(object sender, EventArgs e)
{
    if (this.mCurrentDocument.System.SuperTrakSystem.FloorBitmap != null)
    {
        FloorLength.Value = FloorWidth.Value * 
            ((decimal)this.mCurrentDocument.System.SuperTrakSystem.FloorBitmap.Width / 
            (decimal)this.mCurrentDocument.System.SuperTrakSystem.FloorBitmap.Height);
    }
}

【问题讨论】:

    标签: c# user-controls event-handling


    【解决方案1】:

    感谢您的回答。

    我想出了一个可行的替代解决方案。用户从 UI 更改值会触发事件,而编程的 Value 参数更改不会触发事件。

    using System;
    using System.Windows.Forms;
    
    namespace myNameSpace.Forms.UserControls
    {
        public class NumericUpDownSafe : NumericUpDown
        {
            EventHandler eventHandler = null;
    
            public event EventHandler ValueChanged
            {
                add
                {
                    eventHandler += value;
                    base.ValueChanged += value;
                }
    
                remove
                {
                    eventHandler -= value;
                    base.ValueChanged -= value;
                }
            }
    
            public decimal Value
            {
                get
                {
                    return base.Value;
                }
                set
                {
                    base.ValueChanged -= eventHandler;
                    base.Value = value;
                    base.ValueChanged += eventHandler;
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      我对 NumericUpDown 控件不太熟悉,但可能无法在不触发 ValueChanged 事件的情况下设置值。相反,在设置值之前,您可以设置一个指示该事件应被忽略的标志,并在设置值后清除该标志。在您的事件处理程序中,如果设置了标志,则不执行任何操作。

      private bool ignoreEvent = false;
      private void setValue(int value)
      {
          ignoreEvent = true;
          FloorLength.Value = value;
          ignoreEvent = false;
      }
      
      private void FloorLength_ValueChanged(object sender, EventArgs e)
      {
          if(ignoreEvent) { return; }
      
          // your code here
      }
      

      【讨论】:

        【解决方案3】:

        理论上,这些值应该稳定......这意味着如果用户更改 1,系统会更改另一个,然后第一个保持不变。因此,我只需在两个事件处理程序(伪代码)中添加一个检查:

        newValue = equation;
        if(controlValue != newValue)  
        {
            controlValue = newValue; //raises the event only when necessary.
        }
        

        【讨论】:

        • 它们不稳定,我认为这是因为值被四舍五入到小数点后两位
        • 那么,我们能否获得有关值(一两个示例)和 NumericUpDown 属性(例如 DecimalPlaces)的更多信息?
        猜你喜欢
        • 2011-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多