C# “checked = false”的CheckBox(事件)方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15200618/
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
CheckBox (event) method for "checked = false"
提问by Enthusiastic
Is there any method or Is it possible to write a method to execute a set of instructions when a (Windows Form)checkbox is unchecked (but not when checked)? I will clarify this providing my sample code.
是否有任何方法或是否可以编写一种方法来在未选中(Windows 窗体)复选框时(但未选中时)执行一组指令?我将提供我的示例代码来澄清这一点。
Existing Sample Code:
现有示例代码:
private void ChkBox_CheckedChanged(object sender, EventArgs e)
{
if (ChkBox.Checked == false)
{
MessageBox.Show("unchecked");
}
}
Q:Could that if condition
be avoided by writing a method for this which gets executed only when checkbox is unchecked.
问:是否if condition
可以通过为此编写一个方法来避免这种情况,该方法仅在未选中复选框时执行。
Illustration:Something like this:
插图:像这样:
private void ChkBox_Unchecked(object sender, EventArgs e)
{
MessageBox.Show("unchecked");
}
additional comments:I don't have anything to execute for event 'checkbox is checked' .. So just thinking if I can avoid checkchanged
event by replacing it with unchecked
type of event.
附加评论:我没有任何要执行的事件 'checkbox is checked' .. 所以只是想我是否可以checkchanged
通过用unchecked
事件类型替换它来避免事件。
采纳答案by Sergey Berezovskiy
If you really need that strange events, then create custom check box by inheriting from CheckBox. Override it's OnCheckedChanged
event, and add your Unchecked
event:
如果你真的需要那些奇怪的事件,那么通过从 CheckBox 继承来创建自定义复选框。覆盖它的OnCheckedChanged
事件,并添加您的Unchecked
事件:
public class StrageCheckBox : CheckBox
{
public event EventHandler Unchecked;
protected override void OnCheckedChanged(EventArgs e)
{
base.OnCheckedChanged(e);
if (!Checked)
OnUnchecked(e);
}
protected virtual void OnUnchecked(EventArgs e)
{
if (Unchecked != null)
Unchecked(this, e);
}
}
Use StrangeCheckBoxes
instead of default check boxes. Subscribe to Unchecked
event. Voila, you have that strange behavior.
使用StrangeCheckBoxes
代替默认复选框。订阅Unchecked
事件。瞧,你有那种奇怪的行为。
RemarkActually I would not suggest usage of custom control or if/else in all scenarios. If you have many checkboxes with this kind of behavior, then custom control is better than many duplicated if/else in each handler. If you have several controls, or single handler for all checkboxes, then I'd go with if/else approach.
备注实际上,我不会建议在所有场景中使用自定义控件或 if/else。如果您有许多具有这种行为的复选框,那么自定义控件比每个处理程序中的许多重复 if/else 更好。如果您有多个控件或所有复选框的单个处理程序,那么我会采用 if/else 方法。
回答by Grant Thomas
No, the only event exposed is one to detect when a change to the state occurred, then you must check the state to determine how it changed.
不,唯一公开的事件是检测状态何时发生更改,然后您必须检查状态以确定它是如何更改的。
You could, if so inclined, create your own control which essentially wraps this one and exposes such state-distinguishing events, but this seems entirely pointless.
如果您愿意,您可以创建自己的控件,该控件本质上包含此控件并公开此类区分状态的事件,但这似乎毫无意义。
回答by Paulie Waulie
As far as I know the event is fired on all changes, you could write your own control which differentiates the value selected and raises separate events but not sure it is really worthwhile.
据我所知,所有更改都会触发该事件,您可以编写自己的控件来区分所选的值并引发单独的事件,但不确定它是否真的值得。
回答by JRoughan
No, sorry. That's the only event you can trigger for a state change. Your code looks fine. Any reason you don't like it?
不,对不起。这是您可以为状态更改触发的唯一事件。你的代码看起来不错。你有什么不喜欢的理由吗?
回答by niculare
I don't think you can do something like this, but if you have many instructions to execute if the checkbox was unchecked and you don't want to encapsulate them in an if
statement you could check for checked
at the beginning and return:
我不认为你可以做这样的事情,但是如果你有很多指令要执行,如果复选框没有被选中并且你不想将它们封装在一个if
语句中,你可以checked
在开始时检查并返回:
private void ChkBox_CheckedChanged(object sender, EventArgs e)
{
if (ChkBox.Checked) return;
MessageBox.Show("unchecked");
}
回答by Parimal Raj
You can inherit the CheckBox
and override OnCheckedChanged
您可以继承CheckBox
和override OnCheckedChanged
class ExtendedCheckBox : CheckBox
{
public event EventHandler Unchecked;
protected override void OnCheckedChanged(EventArgs e)
{
base.OnCheckedChanged(e);
if (Checked == false)
OnUnchecked(e);
}
}
回答by ihayet
This will be a simple straight forward solution although the best suggested way will be to inherit from the CheckBox and modify the events. But, a relatively simple way is as follows:
这将是一个简单直接的解决方案,尽管最好的建议方法是从 CheckBox 继承并修改事件。但是,一个相对简单的方法如下:
Reduce the CheckBox
width so that the CheckBox Label
is no longer visible. Only the box should be visible. Then, include another label beside the box which should be mutually exclusive from the box.
减小CheckBox
宽度,使CheckBox Label
不再可见。只有框应该是可见的。然后,在盒子旁边添加另一个标签,该标签应该与盒子相互排斥。
Now, edit the CheckBox Clicked
event. Use some sort of a flag to identify the state of the checkbox. Voila!
现在,编辑CheckBox Clicked
事件。使用某种标志来标识复选框的状态。瞧!