TriState Checkbox - how to change the order of the states

后端 未结 5 652
走了就别回头了
走了就别回头了 2020-12-06 17:03

I have a CheckBox in my application that is using the TriState mode. The normal behavior for this mode seems to be cycling between null, false, true.

I\'d like to c

5条回答
  •  暖寄归人
    2020-12-06 17:47

    Only for completeness here additionally the solution for Windows-Forms. We have to override the OnClick method, overriding OnStateChanged results in display bug (and stackoverflow if done incorrectly).

    /// 
    /// A  with the ability to reverse the checkstate order.
    /// 
    /// 
    public class CheckBoxReversible : CheckBox
    {
        private bool FInvertCheckStateOrder;
    
        /// 
        /// Gets or sets a value indicating whether to invert the check state order from [Indeterminate->Unchecked->Checked] to [Indeterminate->Checked->Unchecked].
        /// 
        /// 
        ///   true to invert the check state order; otherwise, false.
        /// 
        public bool InvertCheckStateOrder
        {
            get { return FInvertCheckStateOrder; }
            set { FInvertCheckStateOrder = value; }
        }
    
        /// 
        /// Löst das -Ereignis aus.
        /// 
        /// Eine Instanz der -Klasse, die die Ereignisdaten enthält.
        protected override void OnClick(EventArgs e)
        {
            if (this.InvertCheckStateOrder)
            {
                if (this.CheckState == CheckState.Indeterminate)
                    this.CheckState = CheckState.Checked;
                else
                    if (this.CheckState == CheckState.Checked)
                        this.CheckState = CheckState.Unchecked;
                    else
                        if (this.CheckState == CheckState.Unchecked)
                            this.CheckState = this.ThreeState ? CheckState.Indeterminate : CheckState.Checked;
            }
            else
                base.OnClick(e);
        }
    }
    

提交回复
热议问题