有什么辦法可以使幾個控制元件的某個屬性可以使用假設的 FOR 回圈來更改?我提到的所有這些控制元件都具有相同的父級,即另一個面板。我試過尋找答案,但找不到答案,或者他們中的一些人談論其他事情。
private void InitiateLargeDialogue(string title, string text1, string text2, Size size)
{
lblLargeTitle.Text = title;
lblLargeText1.Text = text1;
lblLargeText2.Text = text2;
lblLargeText3.Visible = false;
lblLargeText4.Visible = false;
lblLargeText5.Visible = false;
pnlLargeExample.Visible = false;
btnYes.Visible = false;
btnNo.Visible = false;
}
謝謝!
uj5u.com熱心網友回復:
老實說,您現在擁有的是我對 WinForms 專案的期望——它立即可讀。
但是,如果您愿意,可以改為執行以下操作:
Control[] controlarray = new Control[]
{
lblLargeText3,
lblLargeText4,
lblLargeText5,
pnlLargeExample,
btnYes,
btnNo
}
private void InitiateLargeDialogue(string title, string text1, string text2, Size size)
{
lblLargeTitle.Text = title;
lblLargeText1.Text = text1;
lblLargeText2.Text = text2;
foreach(Control c in controlarray)
c.Visible = false;
}
uj5u.com熱心網友回復:
List<Label> listLabel = new List<Label>();
public void test()
{
listLabel.Add(new Label() { Text = "aaa" });
listLabel.Add(new Label() { Text = "bbb" });
listLabel.Add(new Label() { Text = "ccc" });
int count = 1;
foreach(Label l in listLabel)
{
l.Text = $" {count }";
Console.WriteLine(l.Text);
}
}
// 結果 aaa 1 bbb 2 ccc 3
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489379.html
