C# 如何使 Windows 窗体控件只读?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/255955/
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 do I make a Windows Forms control readonly?
提问by Gishu
Returning to WinForms in VS2008 after a long time.. Tinkering with a OOD problem in VS2008 Express Edition.
久违的VS2008中的WinForms..修补VS2008 Express Edition中的OOD问题。
I need some controls to be "display only" widgets. The user should not be able to change the value of these controls... the widgets are updated by a periodic update tick event. I vaguely remember there being a ReadOnly property that you could set to have this behavior... can't find it now.
我需要一些控件作为“仅显示”小部件。用户不应该能够更改这些控件的值……小部件由定期更新滴答事件更新。我依稀记得有一个 ReadOnly 属性,您可以将其设置为具有此行为......现在找不到它。
The Enabledproperty set to false: grays out the control content. I want the control to look normal. The Lockedproperty set to false: seems to be protecting the user from accidentally distorting the control in the Visual Form Designer.
将启用属性设置为false:变灰的控制内容。我希望控件看起来正常。该锁定属性设置为false:似乎是保护用户意外扭曲在Visual窗体设计器控制。
What am I missing?
我错过了什么?
采纳答案by Gulzar Nazim
For some typical winforms controls:
对于一些典型的 winforms 控件:
http://jquiz.wordpress.com/2007/05/29/c-winforms-readonly-controls/
http://jquiz.wordpress.com/2007/05/29/c-winforms-readonly-controls/
This is also a good tip to preserve the appearance:
这也是保持外观的一个很好的技巧:
Color clr = textBox1.BackColor;
textBox1.ReadOnly = true;
textBox1.BackColor = clr;
回答by Patrick Desjardins
Textbox
文本框
.ReadOnly property to true
.ReadOnly 属性为 true
Controls without ReadOnly
没有只读的控件
Other control do not have all the time the ReadOnly property. You will require to play with the Events to take off the editing process and keeping your value not editable.
其他控件始终没有 ReadOnly 属性。您将需要使用事件来取消编辑过程并保持您的值不可编辑。
回答by Grant
Two relevant properties ReadOnly and Enabled. ReadOnly = true prevents editing grays out the background, but it still allows focus. Enabled = false grays out the background, text and prevents editing or focus.
两个相关的属性 ReadOnly 和 Enabled。ReadOnly = true 防止编辑使背景变灰,但它仍然允许聚焦。启用 = false 使背景、文本变灰并阻止编辑或聚焦。
Windows UI conventions dicate giving the user a visual cue that a control is readonly (that way they won't attempt to edit it and be subsequently frustrated). The grayed out disabled state is the defined system convention, but it's arguable too much of a cue (and not a legibile enough one).
Windows UI 约定向用户提供了一个视觉提示,即控件是只读的(这样他们就不会尝试编辑它并随后感到沮丧)。灰显的禁用状态是已定义的系统约定,但它是一个有争议的提示(而且不够清晰)。
The simplest route is probababy to set your control to ReadOnly, set the background to System.Drawing.SystemColors.Window and then block focus messages. You could do this by catching OnEnter events and immediately moving Focus to another control that's not readonly (say, a Close or Edit button). Or you could derive your own control and eat any WM_SETFOCUS messages. Example below.
最简单的方法可能是将您的控件设置为只读,将背景设置为 System.Drawing.SystemColors.Window,然后阻止焦点消息。您可以通过捕获 OnEnter 事件并立即将焦点移动到另一个非只读控件(例如关闭或编辑按钮)来实现此目的。或者,您可以派生出自己的控制权并吃掉任何 WM_SETFOCUS 消息。下面举例。
I believe various third-party control sets give you additional options and granularity.
我相信各种第三方控件集为您提供了额外的选项和粒度。
public class ReadOnlyTextBox : TextBox
{
const uint WM_SETFOCUS = 0x0007;
public ReadOnlyTextBox()
{
this.ReadOnly = true;
this.BackColor = System.Drawing.SystemColors.Window;
this.ForeColor = System.Drawing.SystemColors.WindowText;
}
protected override void WndProc(ref Message m)
{
// eat all setfocus messages, pass rest to base
if (m.Msg != WM_SETFOCUS)
base.WndProc(ref m);
}
}
回答by Rajan Arora
To make the forms control Readonly instantly on one click do use the following peice of Code :
要使表单一键立即控制只读,请使用以下代码:
public void LockControlValues(System.Windows.Forms.Control Container)
{
try
{
foreach (Control ctrl in Container.Controls)
{
if (ctrl.GetType() == typeof(TextBox))
((TextBox)ctrl).ReadOnly = true;
if (ctrl.GetType() == typeof(ComboBox))
((ComboBox)ctrl).Enabled= false;
if (ctrl.GetType() == typeof(CheckBox))
((CheckBox)ctrl).Enabled = false;
if (ctrl.GetType() == typeof(DateTimePicker))
((DateTimePicker)ctrl).Enabled = false;
if (ctrl.Controls.Count > 0)
LockControlValues(ctrl);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Then call it from your Button Click Event like this :
然后从您的按钮单击事件中调用它,如下所示:
LockControlValues(this)
Hope, this helps to solve your problem :
希望,这有助于解决您的问题:
Happy Programming,
快乐编程,
Rajan Arora www.simplyrajan.co.nr
Rajan Arora www.simplyrajan.co.nr
回答by C Johnson
I was given this same requirement at work yesterday. Except instead of a textbox I had to make an entire form disabled without changing it's color.
昨天我在工作中也得到了同样的要求。除了文本框之外,我必须禁用整个表单而不更改其颜色。
So I replaced a call to
所以我换了一个电话
form->Enabled = false;
with
和
IntPtr hWnd = form->Handle;
HWND window_handle = (HWND)hWnd.ToPointer();
::EnableWindow(window_handle, aEnable ? TRUE:FALSE);
Which worked well. You can see above that I am using managed C++. The entire form is now disabled, but not greyed out.
哪个运作良好。您可以在上面看到我使用的是托管 C++。整个表单现在被禁用,但不会变灰。