English 中文(简体)
方案上点击
原标题:Programmatically click on a CheckBox

是否有办法在检查Box时进行点击? 我正在寻找与布顿相当的东西。 Perform;

最佳回答

为什么你需要模拟点击,这种编号线是否适合你们的需要?

myCheckBox.Checked = !myCheckBox.Checked;

如果在支票正文变更状态时需要执行逻辑,请使用<编码>Changed,而不是<代码>Click。

private void CheckBox1_CheckedChanged(Object sender, EventArgs e)
{
   MessageBox.Show("You are in the CheckBox.CheckedChanged event.");
}
问题回答

以上这些解决办法称为检查箱。 检查 改变活动。

如果你想明确叫Click活动,你可以这样做:

checkBox1_Click(checkBox1, null);

为什么你们想要在查比奥引发一个点击事件?

如果你想把它的价值引向来:

theCheckBox.Checked = !theCheckBox.Checked;

如果你想启动与浮标事件相关的一些功能,则从<代码>中删除该代码的想法就更好。 Click eventhandler into a separate means that can be calls from any place:

private void theCheckBox_Click(object sender, EventArgs e)
{
    HandleCheckBoxClick((CheckBox)sender);
}

private void HandleCheckBoxClick(CheckBox sender)
{
    // do what is needed here
}

在你设计像这样的法典时,你可以很容易地从任何地方援引功能:

HandleCheckBoxClick(theCheckBox);

多数控制活动操作者也可(或许应当)采用同样的方法;尽可能从事件处理者转向更可使用的方法。

我不认为你能够以这种方式产生一个点击事件,而不必直接打电话给Box_Click事件手稿。 但你可以这样做:

checkBox.Checked = !checkBox.Checked;

检查 即便是你这样做,也会叫换手。

<代码>ButtonPerformClick()>方法验证了活性控制,测试主动控制是否会失去目前的重点。 两种方式可能与<代码>CheckBox相同。 办法1是用思考方式将internalControl:

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 {
            }
        }
    }
}

第2号办法试图做同样的事情,但并不反映:

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;
    }
}




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签