我的 WinForms 复选框代码有问题
我有一个要求,如果我单击复选框,则应更新相应的端口,并且这些更新的端口将在另一个文件中用于将输出发送到这些特定端口。因此,当我单击 clickbox 1 4 和 7 时,应启用那些相应的端口。在必须发生输出的地方(即 class2),我运行一个循环,它将在 while 循环中从端口读取(比如说),因此只有启用的 1 4 和 7 端口应该运行。问题是,如果我单击复选框 1 2 3,即连续数字,它工作正常,如果我单击 7,则循环递减,最后从 1 到 7 的端口被启用,而此时只有 7 应该闪烁。即,从 7 开始,然后是 6,然后是 5,最后是 1,当只有 7 应该闪烁时,所有灯都会闪烁。
这是复选框条件:
private void checkBox4_CheckedChanged_1(object sender, EventArgs e)
{
if (sender is CheckBox)
{
CheckBox checkbox = sender as CheckBox;
if (checkbox.Checked)
{
Enableports[4] = true; or Enableport(4); // im setting that port 4 to true( Enableports[4] = true ) and directly entering the value in another API( Enableport(4) ).
}
else
{
Disableport(4);
}
}
}
这是循环:(这是在另一个类中) bool[] Enabledports { 获取;放; } 是启用端口的声明。 无效Enableport(int输出);对于一个人来说。
for (int i = 0; i <= 12; i++)
{
if (Enabledports[i] == true) // API to check those enabled ports only
{
Enableport(i);
}
}
这里Enabledports是bool[]
返回类型。
我不确定我的做法是否正确。我只是一个初学者,任何帮助将不胜感激。
I have a requirement where if i click on Checkboxes, then that corresponding port should be updated and these updated ports will be used in another file for sending outputs to these particular ports. So when i click on clickbox 1 4 and 7, then those corresponding ports should be enabled. In the place where the output has to happen(ie., class2), i run a loop, which will read from the port in a while loop(say), so only 1 4 and 7 ports which are enabled should run. The prob is, if i click on checkboxes 1 2 3, ie., consecutive numbers, it works fine, if i click on say 7, then the loop decrements and finally ports from 1 to 7 are enabled when only 7 is supposed to blink. ie., starts from 7, then 6, then 5 then finally 1 and all lights blink when only 7 is supposed to blink.
this is the checkbox condition:
private void checkBox4_CheckedChanged_1(object sender, EventArgs e)
{
if (sender is CheckBox)
{
CheckBox checkbox = sender as CheckBox;
if (checkbox.Checked)
{
Enableports[4] = true; or Enableport(4); // im setting that port 4 to true( Enableports[4] = true ) and directly entering the value in another API( Enableport(4) ).
}
else
{
Disableport(4);
}
}
}
this is the loop :(this is in another class)
bool[] Enabledports { get; set; } is the declaration for Enabled ports.
void Enableport(int output); for a single one.
for (int i = 0; i <= 12; i++)
{
if (Enabledports[i] == true) // API to check those enabled ports only
{
Enableport(i);
}
}
here Enabledports is bool[]
return type.
I'm not sure if I'm doing it the right way. I'm just a beginner and any help will be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不完全确定我是否正确理解了您的问题,但无论如何我都会提供最好的建议:
我认为在一个地方执行“EnablePort()”是一个坏主意(“循环”)另一个类”)和另一个类中的“DisablePort()”(在 checkBox4_CheckedChanged_1() 事件处理方法的“else”子句内)。
我不明白为什么你必须在循环中执行“EnablePort()”,而不是直接从 checkBox4_CheckedChanged_1() 方法调用它,如果没有特别需要,我建议直接调用“EnablePort()” .
只是我的2分钱...
I'm not completely sure that I've understood your question correctly, but I'll offer my best advice anyhow:
I think it is a bad idea to do "EnablePort()" in one place (the "loop in another class") and the "DisablePort()" in another (inside the "else" clause of the checkBox4_CheckedChanged_1() event handling method).
I don't understand why you have to do the "EnablePort()" inside a loop instead of calling it directly from the checkBox4_CheckedChanged_1() method, If not specifically required, I would suggest just calling "EnablePort()" directly.
Just my 2 cents...