背景:

我有一个带有DropDownStyle = DropDown的Forms.ComboBox.

我不使用自动完成功能,但我实现了类似的功能,它不仅过滤文本的开头,而且使用正则表达式并显示与输入的文本匹配的所有项目.这很好用.

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

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

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

问题:

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

链接:

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

尝试这个:
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);
                //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,out info);
        return info;
    }

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

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

}

阻止System.Window.Forms.ComboBox(C#)的AutoSelect行为的更多相关文章

  1. canvas中普通动效与粒子动效的实现代码示例

    canvas用于在网页上绘制图像、动画,可以将其理解为画布,在这个画布上构建想要的效果。本文详细的介绍了粒子特效,和普通动效进行对比,非常具有实用价值,需要的朋友可以参考下

  2. H5混合开发app如何升级的方法

    本篇文章主要介绍了H5混合开发app如何升级的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  3. canvas学习和滤镜实现代码

    这篇文章主要介绍了canvas学习和滤镜实现代码,利用 canvas,前端人员可以很轻松地、进行图像处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  4. localStorage的过期时间设置的方法详解

    这篇文章主要介绍了localStorage的过期时间设置的方法详解的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  5. 详解HTML5 data-* 自定义属性

    这篇文章主要介绍了详解HTML5 data-* 自定义属性的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  6. HTML5的postMessage的使用手册

    HTML5提出了一个新的用来跨域传值的方法,即postMessage,这篇文章主要介绍了HTML5的postMessage的使用手册的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  7. 教你使用Canvas处理图片的方法

    本篇文章主要介绍了教你使用Canvas处理图片的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  8. ios – Swift语言:如何调用SecRandomCopyBytes

    从Objective-C,我可以这样做:在Swift中尝试这个时,我有以下内容:但我得到这个编译器错误:data.mutableBytes参数被拒绝,因为类型不匹配,但我无法弄清楚如何强制参数.解决方法这似乎有效:

  9. 使用Firebase iOS Swift将特定设备的通知推送到特定设备

    我非常感谢PushNotifications的帮助.我的应用聊天,用户可以直接向对方发送短信.但是如果没有PushNotifications,它就没有多大意义.它全部设置在Firebase上.如何将推送通知从特定设备发送到特定设备?

  10. ios – NSData to Data swift 3

    如何将此代码转换为使用Swift3数据?

随机推荐

  1. static – 在页面之间共享数据的最佳实践

    我想知道在UWP的页面之间发送像’selectedItem’等变量的最佳做法是什么?创建一个每个页面都知道的静态全局变量类是一个好主意吗?

  2. .net – 为Windows窗体控件提供百分比宽度/高度

    WindowsForm开发的新手,但在Web开发方面经验丰富.有没有办法为Windows窗体控件指定百分比宽度/高度,以便在用户调整窗口大小时扩展/缩小?当窗口调整大小时,可以编写代码来改变控件的宽度/高度,但我希望有更好的方法,比如在HTML/CSS中.在那儿?

  3. 使用Windows Azure查询表存储数据

    我需要使用特定帐户吗?>将应用程序部署到Azure服务后,如何查询数据?GoogleAppEngine有一个数据查看器/查询工具,Azure有类似的东西吗?>您可以看到的sqlExpressintance仅在开发结构中,并且一旦您表示没有等效,所以请小心使用它.>您可以尝试使用Linqpad查询表格.看看JamieThomson的thispost.

  4. windows – SetupDiGetClassDevs是否与文档中的设备实例ID一起使用?

    有没有更好的方法可以使用DBT_DEVICEARRIVAL事件中的数据获取设备的更多信息?您似乎必须指定DIGCF_ALLCLASSES标志以查找与给定设备实例ID匹配的所有类,或者指定ClassGuid并使用DIGCF_DEFAULT标志.这对我有用:带输出:

  5. Windows Live ID是OpenID提供商吗?

    不,WindowsLiveID不是OpenID提供商.他们使用专有协议.自从他们的“测试版”期结束以来,他们从未宣布计划继续它.

  6. 如果我在代码中进行了更改,是否需要重新安装Windows服务?

    我写了一个Windows服务并安装它.现在我对代码进行了一些更改并重新构建了解决方案.我还应该重新安装服务吗?不,只需停止它,替换文件,然后重新启动它.

  7. 带有双引号的字符串回显使用Windows批处理输出文件

    我正在尝试使用Windows批处理文件重写配置文件.我循环遍历文件的行并查找我想要用指定的新行替换的行.我有一个’函数’将行写入文件问题是%Text%是一个嵌入双引号的字符串.然后失败了.可能还有其他角色也会导致失败.如何才能使用配置文件中的所有文本?尝试将所有“在文本中替换为^”.^是转义字符,因此“将被视为常规字符你可以尝试以下方法:其他可能导致错误的字符是:

  8. .net – 将控制台应用程序转换为服务?

    我正在寻找不同的优势/劣势,将我们长期使用的控制台应用程序转换为Windows服务.我们为ActiveMQ使用了一个叫做java服务包装器的东西,我相信人们告诉我你可以用它包装任何东西.这并不是说你应该用它包装任何东西;我们遇到了这个问题.控制台应用程序是一个.NET控制台应用程序,默认情况下会将大量信息记录到控制台,尽管这是可配置的.任何推荐?我们应该在VisualStudio中将其重建为服务吗?我使用“-install”/“-uninstall”开关执行此操作.例如,seehere.

  9. windows – 捕获外部程序的STDOUT和STDERR *同时*它正在执行(Ruby)

    哦,我在Windows上:-(实际上,它比我想象的要简单,这看起来很完美:…是的,它适用于Windows!

  10. windows – 当我试图批量打印变量时,为什么我得到“Echo is on”

    我想要执行一个简单的批处理文件脚本:当我在XP中运行时,它给了我预期的输出,但是当我在Vista或Windows7中运行它时,我在尝试打印值时得到“EchoisOn”.以下是程序的输出:摆脱集合表达式中的空格.等号(=)的两侧可以并且应该没有空格BTW:我通常在@echo关闭的情况下启动所有批处理文件,并以@echo结束它们,所以我可以避免将代码与批处理文件的输出混合.它只是使您的批处理文件输出更好,更清洁.

返回
顶部