C#开发——WinForm应用程序中的ComboBox实现ReadOnly功能
来源:互联网 发布:什么是网络通讯员 编辑:程序博客网 时间:2024/05/16 06:57
在ComboBox控件的DropDownStyle有三种属性:
1.Simple 类似TextBox的外观,文本部分可以编辑,控件的Text值可显示,Items中的项要靠键盘“↑”、“↓”来选择。
2.DropDown 文本部分可以编辑,控件的Text值可显示,Items中的项通过点击控件的“▼”出现的下拉选择框来选择。
3.DropDownList 文本部分不可编辑,不能设置控件的Text值,Items中的项通过点击控件的“▼”出现的下拉选择框来选择。
本来是要用类似DropDown 的样式,开始时设置控件的Text值作为提示,但在用户选择选项时,不能让他编辑选项的内容,本来以为会有个ReadOnly属性
然后设置一下就可以了,可没想到把属性列表挨个看了好几遍就是没找到,真不知道MS当时设计控件时怎么想的,只好查资料看能不能实现那样的功能。
找了半天发现用windows API来实现比较简单(又是windows API看来windows API真的很好很强大)
引用命名空间:
using System.Runtime.InteropServices;
用到的API声明:
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd); int GW_CHILD = 5; [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); public const int EM_SETREADONLY = 0xcf;
1.用GetWindow API取得要设置的ComboBox控件的句柄。
2.用SendMessage API给取得的句柄设置只读属性。
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace WindowsFormsApplication1{ public partial class Form1 : Form { [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd); int GW_CHILD = 5; [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); public const int EM_SETREADONLY = 0xcf; public Form1() { InitializeComponent(); IntPtr editHandle = GetWindow(comboBox1.Handle , GW_CHILD); SendMessage(editHandle,EM_SETREADONLY,1,0); } }}
别人解释的用到的原理:
1.comboBox其实是一个嵌套控件(复合控件)在DropDownList状态时;他由下拉列表,和ComboBox本身组成
DropDown状态时ComboBox中多了一个edit就是.net下的TextBox那个输入状态是由edit控制的;
不过这个edit是无法在.net下取得的this.comboBox1.Controls.Count 返回 0.
2.用AIP取得控件句柄。
3.给控件设置只读状态。
这个就是发个消息基本可以搞定(不过忘记是那个消息了),看看msdn找em_ 开头的消息,找到EM_SETREADONLY看名字就是他了;
根据SDK规则,em_ 开头的消息都是对应edit的.
0 0
- C#开发——WinForm应用程序中的ComboBox实现ReadOnly功能
- WinForm应用程序中的ComboBox实现ReadOnly功能
- WinForm应用程序中的ComboBox实现ReadOnly功能
- C# WinForm开发系列 - ComboBox
- C# WinForm开发系列 - ComboBox
- WinForm+C#代码实现控制应用程序自启动功能
- WinForm+C#代码实现控制应用程序自启动功能
- C# - ComboBox 重绘 WinForm开发系列
- C#中的只读字段——readonly
- c# Winform应用程序开发规范
- c# Winform应用程序开发规范
- C# Combobox控件实现模糊查询功能
- 利用C#中ToolStirp中的ComboBox控件实现下拉菜单改变文档字体的功能
- c# Winform 开发分屏显示应用程序
- c# Winform 开发分屏显示应用程序
- c# winform combobox追加数据
- C#中的readonly
- 给winform combox加readonly功能(用户可能复制combox中的内容)
- 切记!这样洗头最伤身
- lisa故障表
- Linux0.11内核进程数据结构
- javascript (二) 支持javascript的几个主流浏览器
- 深入理解js闭包
- C#开发——WinForm应用程序中的ComboBox实现ReadOnly功能
- Chrome插件开发之隐藏页面图片
- MySql查询大量数据时报错Error Code:1153 Got a packet bigger than 'max_allowed_packet' bytes
- MTK Android 菜鸟上手之旅
- Spinner android:entries属性
- ARM板上android系统开机Logo的修改
- hadoop shell 操作指南
- [ACM] poj 1182 食物链(并查集)
- C++ 大数阶乘求取--数组实现代码