代码之家  ›  专栏  ›  技术社区  ›  Grzenio

以编程方式单击复选框

  •  4
  • Grzenio  · 技术社区  · 16 年前

    6 回复  |  直到 16 年前
        1
  •  10
  •   Wolf    10 年前

    为什么您需要模拟单击,这行代码不符合您的需要吗?

    myCheckBox.Checked = !myCheckBox.Checked;
    

    CheckedChanged 事件而不是 Click .

    private void CheckBox1_CheckedChanged(Object sender, EventArgs e)
    {
       MessageBox.Show("You are in the CheckBox.CheckedChanged event.");
    }
    
        2
  •  3
  •   Fredrik Mörk    16 年前

    如果要切换其值:

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

    同样的方法可以(也许应该)用于大多数控制事件处理程序;将尽可能多的代码从事件处理程序中移出,并移入可重用性更强的方法中。

        3
  •  3
  •   cacoroto    13 年前

    上述解决方案调用Checkbox.CheckedChanged事件。

    如果要显式调用Click事件,可以执行以下操作:

    checkBox1_Click(checkBox1, null);
    
        4
  •  1
  •   scwagner    16 年前

    我仍在设置一个新的工作站,所以目前无法对此进行适当的研究,但是 UI Automation 可能复选框支持 IInvokeProvider 你可以使用 Invoke 方法?

        5
  •  0
  •   Mark Byers    16 年前

    我认为,如果不直接调用复选框\单击事件处理程序,就无法以这种方式生成单击事件。但你可以这样做:

    checkBox.Checked = !checkBox.Checked;
    

        6
  •  0
  •   Loathing    5 年前

    这个 Button PerformClick() CheckBox . 方法#1是使用反射来调用 internal Control 类别:

    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;
        }
    }
    
        7
  •  -1
  •   Fandango68    9 年前

    您没有指定复选框控件的类型。ASP.net还是Windows?

    从代码隐藏调用JavaScript函数

    步骤1添加Javascript代码

    <script type="text/javascript" language="javascript">
        function Func() {
            alert("hello!")
        }
    </script>
    

    步骤3:在按钮单击事件中添加此代码

    ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "Func()", true);