C# 禁止ComboBox滚动事件细谈

时间:2024-04-05 12:38:02

如果是Dev 控件 ComboBoxEdit 禁止滚动事件则只需设置一个属性即可:
ComboBoxEdit.Properties.AllowMouseWheel = false;

设置器里设置:

C# 禁止ComboBox滚动事件细谈


以下是微软自带的combobox用法:
 /// <summary>
/// 判断是否是ComboBox在进行鼠标滚轮动作
/// </summary>
public bool isWheel = false;    
/// <summary>
/// 用来存放在执行鼠标滚轮前,ComboBox中的最后值
/// </summary>
public string strComB = null;
在Form_Load中写入以下代码:
//鼠标滚动事件注册
cmbStep.MouseWheel += new MouseEventHandler(ComboBox_MouseWheel);
       //滚动事件(控制多个下拉框滚动事件)
        void ComboBox_MouseWheel(object sender, MouseEventArgs e)
        {
            try
            {
                switch ((sender as ComboBoxEdit).Name)
                {
                    case "cmbSpecialArea":
                        // 把当前ComboBox的值存放到变量中
                        strComB = cmbSpecialArea.Text;
                        isWheel = true;
                        break;
                    case "cmbDiffAjustId":
                        strComB = cmbDiffAjustId.Text;
                        isWheel = true;
                        break;
                    case "cmbStep":
                        strComB = cmbStep.Text;
                        isWheel = true;
                        break;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.ToString());
                MsgBox.Warning(ex.ToString());
            }
        }
        //鼠标按下事件(找对应下拉框的MouseDown事件绑定)
        void ComboBox__MouseDown(object sender, MouseEventArgs e)
        {
            isWheel = false;
        }
        //下拉框text改变事件(找对应下拉框的TextChanged事件绑定)
        public void combobox_change(object sender, EventArgs e)
        {
            try
            {
                switch ((sender as ComboBoxEdit).Name)
                {
                    case "cmbSpecialArea":
                        if (isWheel)
                        {
                            cmbSpecialArea.Text = strComB;
                        }
                        break;
                    case "cmbDiffAjustId":
                        if (isWheel)
                        {
                            cmbDiffAjustId.Text = strComB;
                        }
                        break;
                    case "cmbStep":
                        if (isWheel)
                        {
                            cmbStep.Text = strComB;
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.ToString());
                MsgBox.Warning(ex.ToString());
            }
        }


第二种方法时属于全局控制滚动效果,即是,页面所有滚动,滚动条失去效果。

public partial class Form1 : Form, IMessageFilter
{
    public Form1()
    {
        InitializeComponent();
    }
    #region IMessageFilter 成员
    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == 522)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    #endregion
    private void Form1_Load(object sender, EventArgs e)
    {
        Application.AddMessageFilter(this);
    }
}