您的位置:首页 > 其它

如何:使 comboBox 输入状态变成 readonly 方式;TextBox 只读时的效果;

2008-07-10 23:13 639 查看
桌面应用程序中的

comboBox 下拉框,输入方式;

分为3种状态

Simple 文本部分可编辑。列表部分总可见。

DropDown 文本部分可编辑。用户必须单击箭头按钮来显示列表部分。这是默认样式。

DropDownList 用户不能直接编辑文本部分。用户必须单击箭头按钮来显示列表部分。

却没有 readonly 状态;有些时候客户要copy 下拉框里的数据确实挺郁闷的,为啥没有?这个得问ms 了呵呵

不过 comboBox 其实是一个嵌套控件(复合控件)在 DropDownList 状态时;他由 下拉列表,和 comboBox 本身组成

DropDown 状态时 comboBox 中多了一个 edit 就是 .net 下的 TextBox 那个输入状态是由 edit 控制的;

不过这个 edit 是无法在 .net 下取得的 this.comboBox1.Controls.Count 返回 0;

既然知道原理了解决问题也就相对简单的(不用 Win API 看来是不行了)

现在我们需要知识

1) 如果取得子控件;这个有多种方法可以实现我们选择 GetWindow 这个 API 取xx窗口或控件下的第一个子控件比较方便也不用回调什么的;

比 EnumWindows API 容易用多了

2) 如何给 edit (TextBox) 设置只读状态;

这个就是发个消息基本可以搞定(不过忘记是那个消息了),看看 msdn 找 em_ 开头的消息,找到 EM_SETREADONLY 看名字就是他了;

根据 SDK 规则,em_ 开头的消息都是对应 edit 的;

打开代码编辑器,几行搞定;

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

{

//using System.Runtime.InteropServices;

[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);

}

}

}

好了结合我以前的一个blog
http://blog.csdn.net/FlashElf/archive/2004/10/31/161024.aspx
不但可以readonly 还可以限制输入;

打完收工;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: