Telerik控件C#的真/假逻辑错误

时间:2013-11-26 18:37:37

标签: c# checkbox telerik

当一个非常不寻常的事件发生时,我正在我的一个字段上执行验证。我有一个非常简单的if语句基于消息框对话框按钮。所有要做的就是将我的复选框从true更改为false。我花了很多时间试图调试这个以查看发生了什么。每次断点显示复选框为false,但它从未在表单上显示。它也不会超过退货声明。我在return语句中使用了断点,它说它是真的。 (见下文)

private void rChkBoxB_Click(object sender, EventArgs e)
{
    if (rChkBoxB.Checked == false) return;//showed .Checked = true

    if (Convert.ToInt32(rTxtBoxFormatID.Text) > 256)
    {
        DialogResult dialogresult = MessageBox.Show("B does not support numbering over 256!", "Error",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
        if (dialogresult == DialogResult.OK)
        {
            rChkBoxB.Checked = false; //showed .Checked = false
        }
    }
}

我终于尝试将值设置为true而不是false来尝试它,并且它有效。 (见下文)

private void rChkBoxBizerba_Click(object sender, EventArgs e)
{
    if (rChkBoxBizerba.Checked == true) return;

    if (Convert.ToInt32(rTxtBoxFormatID.Text) > 256)
    {
        DialogResult dialogresult = MessageBox.Show("Bizerba does not support numbering over 256!", "Error",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
        if (dialogresult == DialogResult.OK)
        {
            rChkBoxBizerba.Checked = true;
        }
    }
}

什么会导致这种奇怪的逻辑错误发生?

1 个答案:

答案 0 :(得分:1)

我建议使用as per docs from Telerik site ToggleStateChanging事件。

private void rChkBoxBizerba_ToggleStateChanging(object sender, StateChangingEventArgs args)
{
    if (args.NewValue == ToggleState.On) 
    {
        if (Convert.ToInt32(rTxtBoxFormatID.Text) > 256)
        {
            DialogResult dialogresult = MessageBox.Show(.......);
            args.Cancel = true;
        }
    }
}

嗯,不确定这是你需要的逻辑,但重要的是处理事件ToggleStateChanging并将属性args.Cancel设置为True,如果你的逻辑指示不应该选中复选框(On根据Telerik偏压)