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

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

问题描述

我尝试将每个"if"替换为"for"或"while":

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,则可以使用

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 作为第二个参数,并将 this 替换为容器(例如 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.

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

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