防止 System.Windows.Forms.ComboBox 的自动选择行为(C#)

Posted

技术标签:

【中文标题】防止 System.Windows.Forms.ComboBox 的自动选择行为(C#)【英文标题】:Prevent AutoSelect behavior of a System.Window.Forms.ComboBox (C#) 【发布时间】:2014-10-30 04:07:06 【问题描述】:

背景:

我有一个Forms.ComboBox 和一个DropDownStyle = DropDown

我不使用AutoComplete,但我实现了类似的东西,它不仅过滤文本的开头,而且使用正则表达式并显示与输入的文本匹配的所有项目。这很好用。

但是,当我键入匹配项的第一个字母时,ComboBox 回退到其原始行为并设置DroppedDown = true 并自动选择第一个条目并完成文本以匹配所选项目(类似于@ 987654328@ 附加)。我想要的是没有自动选择和自动完成。

到目前为止,我发现,我必须以某种方式阻止 SendMessage()CB_FINDSTRING 被调用,并将 CB_FINDSTRING 替换为 CB_FINDSTRINGEXACT (MSDN Link)。

我想我必须扩展 ComboBox 类,但我不确定我必须重写哪些方法。我正在使用 C# .NET Framework v3.5。

问题:

如何扩展 Windows.Forms.ComboBox 以防止自动选择行为?

链接:

How can I prevent auto-select in ComboBox on drop-down except for exact matches?(没有帮助我)

【问题讨论】:

【参考方案1】:

试试这个:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;

using Opulos.Core.Win32;

namespace Opulos.Core.UI 

// Extension class to disable the auto-select behavior when a combobox is in DropDown mode.
public static class ComboBoxAutoSelectEx 

    public static void AutoSelectOff(this ComboBox combo) 
        Data.Register(combo);
    

    public static void AutoSelectOn(this ComboBox combo) 
        Data data = null;
        if (Data.dict.TryGetValue(combo, out data)) 
            data.Dispose();
            Data.dict.Remove(combo);
        
    

    private class Data 
        // keep a reference to the native windows so they don't get disposed
        internal static Dictionary<ComboBox, Data> dict = new Dictionary<ComboBox, Data>();

        // a ComboBox consists of 3 windows (combobox handle, text edit handle and dropdown list handle)
        ComboBox combo;
        NW nwList = null; // handle to the combobox's dropdown list
        NW2 nwEdit = null; // handle to the edit window

        internal void Dispose() 
            dict.Remove(this.combo);
            this.nwList.ReleaseHandle();
            this.nwEdit.ReleaseHandle();
        

        public static void Register(ComboBox combo) 
            if (dict.ContainsKey(combo))
                return; // already registered

            Data data = new Data()  combo = combo ;
            Action assign = () => 
                if (dict.ContainsKey(combo))
                    return; // already assigned

                COMBOBOXINFO info = COMBOBOXINFO.GetInfo(combo); // new COMBOBOXINFO();
                //info.cbSize = Marshal.SizeOf(info);
                //COMBOBOXINFO2.SendMessageCb(combo.Handle, 0x164, IntPtr.Zero, out info);

                dict[combo] = data;
                data.nwList = new NW(combo, info.hwndList);
                data.nwEdit = new NW2(info.hwndEdit);
            ;

            if (!combo.IsHandleCreated)
                combo.HandleCreated += delegate  assign(); ;
            else
                assign();

            combo.HandleDestroyed += delegate 
                data.Dispose();
            ;
        
    

    private class NW : NativeWindow 
        ComboBox combo;
        public NW(ComboBox combo, IntPtr handle) 
            this.combo = combo;
            AssignHandle(handle);
        

        private const int LB_FINDSTRING = 0x018F;
        private const int LB_FINDSTRINGEXACT = 0x01A2;

        protected override void WndProc(ref Message m) 
            if (m.Msg == LB_FINDSTRING) 
                m.Msg = LB_FINDSTRINGEXACT;
            

            base.WndProc(ref m);

            if (m.Msg == LB_FINDSTRINGEXACT) 
                String find = Marshal.PtrToStringAuto(m.LParam);
                for (int i = 0; i < combo.Items.Count; i++) 
                    Object item = combo.Items[i];
                    if (item.Equals(find)) 
                        m.Result = new IntPtr(i);
                        break;
                    
                
            
        
    

    private class NW2 : NativeWindow 
        public NW2(IntPtr handle) 
            AssignHandle(handle);
        

        private const int EM_SETSEL = 0x00B1;
        private const int EM_GETSEL = 0x00B0;

        protected override void WndProc(ref Message m) 
            if (m.Msg == EM_SETSEL) 
                // if this code is not here, then the entire combobox text is selected
                // which looks ugly, especially when there are multiple combo boxes.
                //
                // if this method returns immediately, then the caret position is set
                // to (0, 0). However, it seems that calling EM_GETSEL has a side effect
                // that the caret position is mostly maintained. Sometimes it slips back
                // to (0, 0).
                SendMessage(Handle, EM_GETSEL, IntPtr.Zero, IntPtr.Zero);
                //int selStart = (sel & 0x00ff);
                //int selEnd = (sel >> 16) & 0x00ff;
                //Debug.WriteLine("EM_GETSEL: " + selStart + " nEnd: " + selEnd);
                return;
            
            base.WndProc(ref m);
        

        [DllImportAttribute("user32.dll", SetLastError=true)]
        private static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
    



[StructLayout(LayoutKind.Sequential)]
public struct COMBOBOXINFO 
    public Int32 cbSize;
    public RECT rcItem;
    public RECT rcButton;
    public int buttonState;
    public IntPtr hwndCombo;
    public IntPtr hwndEdit;
    public IntPtr hwndList;

    public static COMBOBOXINFO GetInfo(ComboBox combo) 
        COMBOBOXINFO info = new COMBOBOXINFO();
        info.cbSize = Marshal.SizeOf(info);
        SendMessageCb(combo.Handle, 0x164, IntPtr.Zero, out info);
        return info;
    

    [DllImport("user32.dll", EntryPoint = "SendMessageW", CharSet = CharSet.Unicode)]
    private static extern IntPtr SendMessageCb(IntPtr hWnd, int msg, IntPtr wp, out COMBOBOXINFO lp);


//[StructLayout(LayoutKind.Sequential)]
//public struct RECT 
//  public int Left;
//  public int Top;
//  public int Right;
//  public int Bottom;
//


【讨论】:

这正是我想要的。完美运行。非常感谢! 这对我也很有效。谢谢讨厌! 你如何使用这个?这对我来说并不明显。当我尝试时,我得到“尝试读/写受保护的内存”。也许我做错了 @PandaWood 您可能需要将构建选项从x86 更改为Any CPU。见***.com/questions/4074585/… RECT 从何而来? VS解决不了...

以上是关于防止 System.Windows.Forms.ComboBox 的自动选择行为(C#)的主要内容,如果未能解决你的问题,请参考以下文章

如何防止用户重复提交数据

PHP 怎么防止GET方式提交重复数据?

如何防止网站被*** 防止网站数据被***篡改

MyBatis怎么防止SQL注入

如何彻底防止SQL注入?

怎么防止网站被***和防止服务器被黑