使用“if”的替代方法复选框的声明 [英] Use an alternative of "if" statement for checkBoxes

查看:177
本文介绍了使用“if”的替代方法复选框的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用for或while替换每个if:

I try to replace each "if" by a "for" or "while":

if (CheckObject1.Checked)
    XRPC.SetMemory(8184, value);
if (CheckObject2.Checked)
    XRPC.SetMemory(7848, value);
[...]
if (CheckObject20.Checked)
    XRPC.SetMemory(1800, value);

我已经尝试了这个但问题是我不知道如何增加checkBox结束:

I already tried this but the problem is that I don't know how to increment the number at the end of checkBox:

int current = 8184;
int X = 1;
while (current > 1800)
{
    if (CheckObjectX.Checked) // regex?
        XRPC.SetMemory(current, value);
    X += 1;
    current -= 336;
}

我不知道正则表达式是否可用于checkBox ...任何想法?

I don't know if a regular expression can be used for "checkBox"... Any idea?

推荐答案

如果这是winforms你可以使用 Controls.Find 和LINQ:

If this is winforms you can use Controls.Find and LINQ:

int current = 8184;
int X = 1;
while (current > 1800)
{
    CheckBox CheckObjectX = this.Controls.Find("CheckObject"+X, true).OfType<CheckBox>().SingleOrDefault();
    if (CheckObjectX != null && CheckObjectX.Checked)
        XRPC.SetMemory(current, value);
    X += 1;
    current -= 336;
}

如果它们都在同一个父控件中而不是嵌套,则可以通过 false 作为第二个参数并用容器替换(如 myCheckBoxPanel )。这可能更有效率。

If they're all in the same parent control and not nested you can pass false as second parameter and replace this with the container(like myCheckBoxPanel). That could be more efficient.

这篇关于使用“if”的替代方法复选框的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆