如果在 textbox.text 中的值為 011,則顯示 label2 和 label3 并隱藏 label1。
如果在 textbox.text 中值為 101,則顯示 label1、隱藏 label2、顯示 label3。
如果在 textbox.text 中值為 000,則隱藏所有標簽。
如果在 textbox.text 中值為 111,則顯示所有標簽(label1、label2、label3)。
如果 textbox.text 中的值為 00011111000001101011,則相同。
//Count how digits are in textbox
int result = textBox1.Text.Count(c => char.IsDigit(c));
label1.Text = result.ToString();
//find the position of '1' or '0' are
uj5u.com熱心網友回復:
您可以先將標簽放入陣列中。您可以在致電后執行此操作InitializeComponent();:
public class Form1
{
private readonly List<Label> _labels;
public Form1()
{
InitializeComponent();
_labels = new List<Label>() { label1, label2, label3, label4, label5 };
}
然后我們可以添加一個方法,該方法接受一串 0 和 1,回圈遍歷它,并設定_labels串列中相應標簽的可見性:
private void SetLabelVisibility(string visString)
{
// get the maximum number of possible labels to update
// this is the smaller of the number of 0s and 1s in the string,
// and the number of labels in _labels
int maxLen = Math.Min(visString.Length, _labels.Count);
for (int i = 0; i < maxLen; i)
{
// check if the character is 0, 1, or other
switch (visString[i])
{
case '0': // set to invisible
_labels[i].Visible = false;
break;
case '1': // set to visible
_labels[i].Visible = true;
break;
default: // invalid character
throw new ArgumentException(nameof(visString));
}
}
}
因此呼叫SetLabelVisibility("01101");將使標簽 2、3 和 5 可見,但隱藏標簽 1 和 4。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/528323.html
標籤:C#表格
