C# 如何使winforms中的组合框只读
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/392098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make Combobox in winforms readonly
提问by Kishore A
I do not want the user to be able to change the value displayed in the combobox. I have been using Enabled = false
but it grays out the text, so it is not very readable. I want it to behave like a textbox with ReadOnly = true
, where the text is displayed normally, but the user can't edit it.
我不希望用户能够更改组合框中显示的值。我一直在使用,Enabled = false
但它使文本变灰,因此可读性不强。我希望它表现得像一个带有 的文本框ReadOnly = true
,文本正常显示,但用户无法编辑它。
Is there is a way of accomplishing this?
有没有办法做到这一点?
采纳答案by Tom Anderson
The article ComboBox-with-read-only-behaviorsuggests an interesting solution:
文章ComboBox-with-read-only-behavior提出了一个有趣的解决方案:
Create both a readonly textbox and a combobox in the same place. When you want readonly mode, display the textbox, when you want it to be editable, display the combobox.
在同一位置创建只读文本框和组合框。当你想要只读模式时,显示文本框,当你想要它可编辑时,显示组合框。
回答by Marc Gravell
The best thing I can suggest is to replace the combo-box with a read-only textbox (or just perhaps a label) - that way the user can still select/copy the value, etc.
我能建议的最好的事情是用只读文本框(或者只是一个标签)替换组合框 - 这样用户仍然可以选择/复制值等。
Of course, another cheeky tactic would be to set the DropDownStyle
to DropDownList
, and just remove all other options - then the user has nothing else to pick ;-p
当然,另一种厚脸皮的策略是将 设置DropDownStyle
为DropDownList
,然后删除所有其他选项 - 这样用户就没有其他选择了 ;-p
回答by Dan Williams
Not sure if this is what you're looking for but...
不确定这是否是您要找的东西,但是...
Set the DropDownStyle = DropDownList
设置 DropDownStyle = DropDownList
Then on the SelectedIndexChanged event
然后在 SelectedIndexChanged 事件上
If (ComboBox1.SelectedIndex <> 0)
{
ComboBox1.SelectedIndex = 0
}
This ugly part is that they will "feel" like they can change it. They might think this is an error unless you give them an alert telling them why they can't change the value.
这个丑陋的部分是他们会“感觉”他们可以改变它。他们可能会认为这是一个错误,除非您向他们发出警报,告诉他们为什么无法更改该值。
回答by Tom Anderson
You can change the forecolor and backcolor to the system colors for an enabled combo box, although this may confuse the users (why have it if they can't change it), it will look better.
您可以将前景色和背景色更改为已启用组合框的系统颜色,尽管这可能会使用户感到困惑(如果他们不能更改它,为什么要使用它),但它看起来会更好。
回答by Tom Anderson
make DropDownStyle
property to DropDownList
instead of DropDown
then handle the TextChanged
event to prevent user changing text.
makeDropDownStyle
属性DropDownList
而不是DropDown
然后处理TextChanged
事件以防止用户更改文本。
回答by David Bo?jak
Why don't you just use a text box? Text box has a "Read only" property, and since you want your combo box only to display data, I don't see why you would need a combo box.
为什么不直接使用文本框?文本框具有“只读”属性,并且由于您希望组合框仅显示数据,因此我不明白您为什么需要组合框。
An alternative is that you just cancel out the input for the "on value changed" event. That way you will be displaying your information no mater what user does ...
另一种方法是您只需取消“on value changed”事件的输入。这样,无论用户做什么,您都将显示您的信息......
回答by Rob
Actually, its rather simple:
其实很简单:
Private Sub combobox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles combobox1.KeyDown
' the following makes this the combobox read only
e.SuppressKeyPress = True
End Sub
回答by Manish
Here is the Best solution for the ReadOnly Combo.
这是 ReadOnly Combo 的最佳解决方案。
private void combo1_KeyPress(object sender, KeyPressEventArgs e)
{
e.KeyChar = (char)Keys.None;
}
It will discard the keypress for the Combo.
它将丢弃 Combo 的按键。
回答by Behzad
Here is the Best solution for the ReadOnly Combo.
这是 ReadOnly Combo 的最佳解决方案。
private void combo1_KeyPress(object sender, KeyPressEventArgs e) {
e.KeyChar = (char)Keys.None;
}
It will discard the keypress for the Combo. It doesn't have "e.KeyChar" !
它将丢弃 Combo 的按键。它没有“e. KeyChar”!
回答by Neolisk
This is how you would address the fact that a ComboBox
with Enabled = False
is hard to read:
这是你如何解决 a ComboBox
withEnabled = False
难以阅读的事实: