有没有一种编程方式可以在复选框上生成点击事件?我正在寻找与Button.PerformClick()相当的等效方法。
myCheckBox.Checked = !myCheckBox.Checked;
CheckedChanged
事件而不是Click
事件。private void CheckBox1_CheckedChanged(Object sender, EventArgs e)
{
MessageBox.Show("You are in the CheckBox.CheckedChanged event.");
}
以上解决方案调用了复选框的 CheckedChanged 事件。
如果您想显式调用 Click 事件,可以使用以下代码:
checkBox1_Click(checkBox1, null);
您为什么想在复选框上生成点击事件?
如果您想切换它的值:
theCheckBox.Checked = !theCheckBox.Checked;
如果您想触发与单击事件相关联的某些功能,最好将代码从Click
事件处理程序移出到一个可以从任何地方调用的单独方法中:
private void theCheckBox_Click(object sender, EventArgs e)
{
HandleCheckBoxClick((CheckBox)sender);
}
private void HandleCheckBoxClick(CheckBox sender)
{
// do what is needed here
}
HandleCheckBoxClick(theCheckBox);
对于大多数控件事件处理程序,可以(并且可能应该)采用相同的方法;尽可能将代码从事件处理程序中移出,并放入更可重用的方法中。
Click
事件中启用/禁用控件不是一个好主意;如果Checked
属性由代码分配,那怎么办?这不会触发Click
事件,UI将处于不一致状态。你应该让代码作为CheckedChanged
事件的结果被执行。 - Fredrik MörkButton
的 PerformClick()
方法会验证活动控件,测试活动控件是否可以失去当前焦点。对于 CheckBox
,有两种可能实现相同功能的方法。第一种方法是使用反射调用 Control
类中的 internal
方法:
public class CheckBoxPerformClick : CheckBox {
private readonly static MethodInfo callValidateActiveControl;
private readonly static PropertyInfo propValidationCancelled;
static CheckBoxPerformClick() {
try {
Type ty = typeof(Control);
Type outBool = Type.GetType("System.Boolean&");
callValidateActiveControl = ty.GetMethod("ValidateActiveControl", BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { outBool }, null);
propValidationCancelled = ty.GetProperty("ValidationCancelled", BindingFlags.Instance | BindingFlags.NonPublic);
} catch {}
}
public CheckBoxPerformClick() : base() {
this.Text = "Checkbox";
this.Appearance = Appearance.Button;
}
public void PerformClick() {
if (callValidateActiveControl != null && propValidationCancelled != null) {
try {
Object[] args = new Object[1];
bool validate = (bool) callValidateActiveControl.Invoke(this, args);
bool validatedControlAllowsFocusChange = (bool) args[0];
if (validate || validatedControlAllowsFocusChange) {
bool cancelled = (bool) propValidationCancelled.GetValue(this);
if (!cancelled) {
ResetFlagsandPaint();
OnClick(EventArgs.Empty);
}
}
} catch {
}
}
}
}
方法二试图做同样的事情,但不使用反射:
public class CheckBoxPerformClick2 : CheckBox {
public CheckBoxPerformClick2() : base() {
this.Text = "Checkbox";
this.Appearance = Appearance.Button;
}
public void PerformClick() {
bool validate = CanPerformClick();
if (validate) {
ResetFlagsandPaint();
OnClick(EventArgs.Empty);
}
}
// before allowing a click, make sure this control can receive the focus, and that other controls don't require validation
public bool CanPerformClick() {
if (!CanSelect)
return false;
Control c = this.Parent;
while (c != null) {
if (c is ContainerControl)
break;
c = c.Parent;
}
bool valid = true;
if (c is ContainerControl) {
var cc = (ContainerControl) c;
valid = cc.Validate(true);
}
return valid;
}
}
我认为你不能以那种方式生成点击事件,而不直接调用checkBox_Click事件处理程序。但是你可以这样做:
checkBox.Checked = !checkBox.Checked;
即使您这样做,CheckedChanged 处理程序仍将被调用。