简体   繁体   English

如何使用 xor 使我的代码更短

[英]How can I using xor to make my code shorter

I have three checkbox in windows form application bold,italic and underline我在 windows 表单应用程序粗体、斜体和下划线中有三个复选框

when I change check I want the text in label to change to one of these and if I check two of the I want them to active together I do long code for this using if...else but I want shorter code using xor ^ I heard it is shorter but I do not know how当我更改检查时,我希望 label 中的文本更改为其中之一,如果我检查其中两个,我希望它们一起活动,我使用 if...else 执行长代码,但我想要使用 xor 的更短代码 ^ I听说比较短但是不知道怎么弄

I'd use 3 ifs:我会使用 3 个 if:

aLabel.Text = "";
if(xCheckBox.Checked)
  aLabel.Text += "x, ";
if(yCheckBox.Checked)
  aLabel.Text += "y, ":
if(zCheckBox.Checked)
  aLabel.Text += "z, ";
aLabel.Text = aLabel.Text.TrimEnd(' ', ',');

Because it (or some variation on it) is easy to understand..因为它(或它的一些变体)很容易理解..

You could put the text you want in the checkbox tag and then do;您可以将所需的文本放在复选框标签中,然后执行;

aLabel.Text = string.Join(", ", this.Controls.OfType<CheckBox>()
.Where(cb => cb.Checked)
.Select(cb => cb.Tag));

If you have many checkboxes on the form and want this to apply to only a few either name them some pattern you can check for in the Where, or put them in a panel and call panel.Controls instead of this如果您在表单上有许多复选框,并且希望仅将其应用于少数几个,则可以将它们命名为您可以在 Where 中检查的某种模式,或者将它们放在面板中并调用panel.Controls而不是this

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM