简体   繁体   English

停止使用radioButton CheckedChanged事件再次调用函数吗?

[英]Stop double call to function using radioButton CheckedChanged events?

I have 3 radio buttons all held within a groupbox (so only 1 can be marked at a time). 我有3个单选按钮,它们都保存在一个分组框中(因此一次只能标记1个)。 I've got my code working, but to be quicker on updating my label, I want to stop the function from being called twice when a new radioButton is selected. 我的代码可以正常工作,但是为了更快地更新标签,我希望在选择新的radioButton时停止两次调用该函数。

For instance, in my code below, my default is rb1 and then I click rb2, the rb1 CheckedChanged Event fires and updates the label, then rb2's CheckedChanged Event fires and updates the label again (same value). 例如,在下面的代码中,默认设置为rb1,然后单击rb2,则rb1 CheckedChanged事件将触发并更新标签,然后rb2的CheckedChanged Event将触发并再次更新标签(相同值)。

What would be the best way to add some extra criteria to where if the label has been updated once, then stop the function from being called again? 如果标签已被更新一次,然后停止再次调用该函数,那么向其中添加一些额外条件的最佳方法是什么?

CODE: 码:

private void rb1_CheckedChanged(object sender, EventArgs e)
    {
        if (cmbLetterType.Text.Length != 0)
        {
            updatePrintedCntLabel();
        }
    }

private void rb2_CheckedChanged(object sender, EventArgs e)
{
    if (cmbLetterType.Text.Length != 0)
    {
        updatePrintedCntLabel();
    }
}

private void rb3_CheckedChanged(object sender, EventArgs e)
{
    if (cmbLetterType.Text.Length != 0)
    {
        updatePrintedCntLabel();
    }
}

EDIT: To clarify, this is a C# Winforms application I'm doing this in. 编辑:澄清一下,这是我正在使用的C#Winforms应用程序。

Consider this 考虑一下

public class Form1 : Form 
{


    public Form1()
    {
        rb1.CheckedChanged += rb_CheckedChanged;
        rb2.CheckedChanged += rb_CheckedChanged;
        rb3.CheckedChanged += rb_CheckedChanged;
    } 

    private void rb_CheckedChanged(object sender, EventArgs e)
    {

        if (!((Radiobutton)sender).Checked) return;

        if (cmbLetterType.Text.Length != 0)
        {
            updatePrintedCntLabel();
        }

    }
}

Change your condition to: 将您的条件更改为:

if (cmbLetterType.Text.Length != 0 && rb1.Checked)

Do this for the other 2 handlers as well (using rb2.Checked and rb3.Checked) and each one will only fire when it's the one that's become checked , not unchecked . 还要对其他2个处理程序执行此操作(使用rb2.Checked和rb3.Checked),每个处理程序仅在被检查的而不是 检查的情况下才会触发。

The events are doing what are telling them to do 事件正在告诉他们要做的事情

If you don't want to update the Label twice then don't 如果您不想两次更新标签,请不要
Simple, if the value did not change then don't update 很简单,如果值没有变化,就不要更新
Save the value and test for change 保存值并测试更改

Why do you have three identical event handlers? 为什么会有三个相同的事件处理程序?
You can wire multiple events to the same handler. 您可以将多个事件关联到同一处理程序。

Even if you use the approach from Baldrick you can get the button from sender. 即使您使用Baldrick的方法,也可以从发件人处获取按钮。

Late tag on WinForm but still can use the same approach ( if (labelVal = value) return; ) WinForm上的后期标记,但仍可以使用相同的方法(如果(labelVal = value)return;)
In general use public properties 一般使用的公共财产

private string labelVal = string.empty;

public string LabelVal
{
    get { return labelVal; } 
    set
    { 
        if (labelVal = value) return;
        labelVal = value;
        NotifyPropertyChanged("LableVal");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用单选按钮CheckedChanged的匿名绑定委托方法 - Call Anonymous binded delegate method of radiobutton CheckedChanged 关于RadioButton CheckedChanged事件 - About RadioButton CheckedChanged event 在TextChanged或CheckedChanged事件上使用JavaScript清除两个TextBox和checkBox - Clear two TextBoxes and checkBox using JavaScript on TextChanged or CheckedChanged events 刷新后,RadioButton的CheckedChanged消失 - RadioButton's CheckedChanged disappears after refresh 无法在链接按钮单击事件内触发运行时单选按钮checkedchanged事件 - Cannot fire runtime radiobutton checkedchanged event inside linkbutton click event 动态(以编程方式)添加复选框和checkedchanged事件 - Dynamically (programatically) adding check boxes and checkedchanged events 请参阅另一种形式的RadioButton_Checkedchanged值 - Refer to RadioButton_Checkedchanged value from another form 有没有办法在 c# 中设置 Xamarin 单选按钮 CheckedChanged 方法? - Is there a way to set the Xamarin radiobutton CheckedChanged method in c#? 如何在 asp:Radiobutton 中调用 Javascript 函数? - How to Call Javascript function in asp:Radiobutton? TextBox TextChanged事件和CheckBoc CheckedChanged事件未在gridview中触发 - TextBox TextChanged Events and CheckBoc CheckedChanged Events not firing within a gridview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM