C# 以编程方式单击复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1764198/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:36:49  来源:igfitidea点击:

Programmatically click on a CheckBox

c#.netwinformscheckbox

提问by Grzenio

Is there a way to programmatically generate a click event on a CheckBox? I am looking for an equivalent to Button.PerformClick();

有没有办法以编程方式在 CheckBox 上生成点击事件?我正在寻找等效于 Button.PerformClick();

采纳答案by Jeff Cyr

Why do you need to simulate a click, doesn't this line of code fits your need?

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

myCheckBox.Checked = !myCheckBox.Checked;

If you need to execute logic when the state of the CheckBox changes, you should use CheckedChangedevent instead of Click.

如果你需要在 CheckBox 的状态改变时执行逻辑,你应该使用CheckedChangedevent 而不是Click.

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

回答by scwagner

I'm still setting up a new workstation so I can't research this properly at the moment, but with UI Automationmaybe it's possible that the checkbox supports the IInvokeProviderand you can use the Invokemethod?

我仍在设置一个新工作站,所以我目前无法正确研究它,但是使用UI 自动化,也许复选框支持IInvokeProvider并且您可以使用Invoke方法?

回答by Mark Byers

I don't think you can generate a click event in that way without calling the checkBox_Click event handler directly. But you can do this:

我不认为您可以在不直接调用 checkBox_Click 事件处理程序的情况下以这种方式生成点击事件。但是你可以这样做:

checkBox.Checked = !checkBox.Checked;

The CheckedChanged handler will still be called even if you do this.

即使您这样做,CheckedChanged 处理程序仍将被调用。

回答by Fredrik M?rk

Why do you want to generate a click event on the CheckBox?

为什么要在 CheckBox 上生成点击事件?

If you want to toggle it's value:

如果你想切换它的值:

theCheckBox.Checked = !theCheckBox.Checked;

If you want to trigger some functionality that is connected to the Click event, it's a better idea to move the code out from the Clickevent handler into a separate method that can be called from anywhere:

如果要触发某些与 Click 事件相关的功能,最好将代码从Click事件处理程序移出到一个可以从任何地方调用的单独方法中:

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

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

When you design your code like that, you can easily invoke the functionality from anywhere:

当您像这样设计代码时,您可以轻松地从任何地方调用该功能:

HandleCheckBoxClick(theCheckBox);

The same approach can (and perhaps should) be used for most control event handlers; move as much code as possible out from event handlers and into methods that are more reusable.

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

回答by cacoroto

Those solutions above calls Checkbox.CheckedChanged event.

上面的那些解决方案调用 Checkbox.CheckedChanged 事件。

If you want to explicitly call Click event you can this:

如果你想显式调用 Click 事件,你可以这样:

checkBox1_Click(checkBox1, null);

回答by Fandango68

You're not specifying what sort of CheckBox control. ASP.net or Windows?

您没有指定哪种 CheckBox 控件。ASP.net 还是 Windows?

Assuming a web checkbox control, this is the way to fire it off using JavaScript call from within code-behind...

假设有一个 web 复选框控件,这是在代码隐藏中使用 JavaScript 调用来触发它的方法......

Calling a JavaScript function from code behind

从后面的代码调用 JavaScript 函数

Step 1 Add your Javascript code

第 1 步添加您的 Javascript 代码

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

Step 2 Add 1 Script Manager in your webForm and Add 1 button too

步骤 2 在您的 webForm 中添加 1 个脚本管理器并添加 1 个按钮

Step 3 Add this code in your button click event

第 3 步将此代码添加到您的按钮单击事件中

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