如何禁用自定义组合框而不使其灰显?


您好,我有一个自定义组合框类。
在某些情况下,我需要将其禁用而不变灰,而在其他一些情况下,我需要通过 SQL 查询加载 SQL Server 数据库数据源的数据来启用它。
所以我想到了这个,进入我的自定义课程
并创建一个自定义 MouseMove 事件,以便在我想禁用它时使其失去焦点。 但我不知道如何覆盖它,因为我有一个布尔类型的附加参数禁用。

C#
private void CustomCbo_MouseMove(object sender, MouseEventArgs e, bool Disable)
{
     if (Disable)
     {
         this.Parent.Focus();
         this.SelectionLength = 0;
     }
}

但是我在另一个网站上发现了以下内容,没关系,但我不太理解它,我也需要相反的内容,以便启用
CustomCombo 以便拥有我的数据库的数据源的适当值。

C#
public void DisableComboWithoutGrayedOut()
    {
          //Appearence Enable, behavior Disabled
          this.DropDownHeight = 1;
          this.KeyDown += (s, e) => e.Handled = true;
          this.KeyPress += (s, e) => e.Handled = true;
          this.KeyUp += (s, e) => e.Handled = true;

     }

如果我使用 Enabled 属性,那没问题,但我不喜欢变灰。 而且 ComboBox 没有像文本框那样的 ReadOnly 属性。
对于这两种方式的任何建议和解释,我们将不胜感激。 非常感谢您。

我尝试过的:

private void CustomCbo_MouseMove(object sender, MouseEventArgs e, bool Disable)
 {
      if (Disable)
      {
          this.Parent.Focus();
          this.SelectionLength = 0;
      }
 }

  public void DisableComboWithoutGrayedOut()
      {
            //Appearence Enable, behavior Disabled
            this.DropDownHeight = 1;
            this.KeyDown += (s, e) => e.Handled = true;
            this.KeyPress += (s, e) => e.Handled = true;
            this.KeyUp += (s, e) => e.Handled = true;
           
       }

解决方案4

创建一个启用方法。 通过注释掉 KeyDown、KeyPress 和 KeyUp 处理程序,它们将正常运行!

C#
public void EnableComboWithoutGrayedOut()
      {
            //Appearence Enable, behavior Enabled
            this.DropDownHeight = ??;  // Whatever it was before or what you want it to be
            //this.KeyDown += (s, e) => e.Handled = true;
            //this.KeyPress += (s, e) => e.Handled = true;
            //this.KeyUp += (s, e) => e.Handled = true;
           
       }

解决方案1

目标是 ComboBox 实际上并未禁用,但下拉菜单设置为 1,以便它下拉,但由于只有 1 个像素,您将看不到它。

其他 KeyDown、KeyPress 和 KeyUp 处理程序 e.Handled 属性设置为 true,表示该事件已被处理,不再执行任何操作。

解决方案2

这是我的 customCombo 类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
namespace AngelTechShop
{
    public class CustomCombo : ComboBox
    {
        public CustomCombo()
        {
            BorderColor = Color.DimGray;
        }

        [Browsable(true)]
        [Category("Appearance")]
        [DefaultValue(typeof(Color), "DimGray")]
        public Color BorderColor { get; set; }

        private const int WM_PAINT = 0xF;
        private int buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth;

        private bool mDisable;

        public bool Disable
        {
            get { return mDisable; }
            set { mDisable = value; }
        }
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == WM_PAINT)
            {
                using (var g = Graphics.FromHwnd(Handle))
                {
                    // Uncomment this if you don't want the "highlight border".

                    using (var p = new Pen(this.BorderColor, 1))
                    {
                        g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
                    }
                    using (var p = new Pen(this.BorderColor, 2))
                    {
                        g.DrawRectangle(p, 0, 0, Width, Height);
                    }
                }
            }
        }
        
    }
}

解决方案3

当我们通过将 DropDownHeight 设置为 1 来禁用组合框并且需要重新启用它时,如何将值重置为其数据集内容? 因为它保持 1 值。 我们如何改变它,以及如何根据元素的数量和字体来计算 DropDownHeight?

コメント

タイトルとURLをコピーしました