Windows窗体-显示组合框的文本 [英] Windows Forms - Display text for a combobox

查看:100
本文介绍了Windows窗体-显示组合框的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在组合框中以浅灰色突出显示或显示某些文本,例如选择项目"或选择名称",因此当我单击组合框上的任意位置时,

解决方案

您可以为组合框设置选定的文本属性,并在Enter事件中将其清除.

好吧,大概有一半的问题可以解决,但是我快速浏览了一下,并在4-5分钟后我觉得我有解决方案.您的文本框是银色的,还是您最初想要的.接下来,将DropDown样式设置为dropdown,这将使您拥有不在列表中的初始文本值.所以现在我们要使用PInvoke ...
我们将使用的功能是:

[DllImport("user32.dll")] 我们还需要添加此常量.
公共静态外部int SendMessage(int hWnd,int msg,int wParam,IntPtr lParam);

public const int CB_SHOWDROPDOWN = 0x14F;

现在将组合框mousedown事件添加到您的代码中.

private void comboBox1_MouseDown(object sender, MouseEventArgs e) <br />{ <br />comboBox1.ForeColor = SystemColors.WindowText;<br />SendMessage(comboBox1.Handle.ToInt32(), CB_SHOWDROPDOWN, 1, IntPtr.Zero);
}

it may be worth throwing this line in the selected index change.

(窗体上的其他控件).focus();


How do I highlight or display some text like "select item" or "select name" in a combobox in a light gray color in such a way that whenever I click anywhere on the combobox, the text should be cleared and the drop down list opens?

解决方案

You can set the selected text property for the combobox and clear it in the Enter event.


well there is probably half a dozon answers for this but i did a quick run through and 4-5 minutes later i think i have a solution for you.
make the forecolor of your textbox silver or what ever you originally wanted. next set the DropDown style to dropdown this will allow you to have an initial text value that is not in your list. soooo now we are going to use PInvoke...
the function we wil use is:

[DllImport("user32.dll")] We also nee to add this constant.
public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);

public const int CB_SHOWDROPDOWN = 0x14F;

Now add a combobox mousedown event to your code.

private void comboBox1_MouseDown(object sender, MouseEventArgs e) <br />{ <br />comboBox1.ForeColor = SystemColors.WindowText;<br />SendMessage(comboBox1.Handle.ToInt32(), CB_SHOWDROPDOWN, 1, IntPtr.Zero);
}

it may be worth throwing this line in the selected index change.

(some other control on the form).focus();


这篇关于Windows窗体-显示组合框的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆