簡體   English   中英

如何通過從另一個 Label 獲取顏色來更改 Label 的顏色?

[英]How do I change Color of Label by getting the Color from another Label?

我正在嘗試做與示例中的類似的事情,但要更改文本/標簽的顏色。

例子:

Label1.Text = Label2.Text;
Label2.Text = Label3.Text;
Label3.Text = Label4.Text;
//so and and so forth

我正在尋找一個非常簡單的解決方案,如示例中所示,但使用 colors。 我正在使用Windows Form App.Net Framework 4.7.2 ,我正在使用C# 9.0

我曾嘗試使用.ForceColor作為更改顏色的一種方式,但如您所見,我正在嘗試完成與示例中類似的事情。

一開始,我建議把標簽放在容器里,除了標簽不要放任何東西。 然后:

  1. for循環, length = ContainerPanel.Controls.Count - 1 ,條件i = ContainerPanel.Controls.Count
  2. Control[i]轉換為labelFirst
  3. Control[i + 1]轉換為labelSecond
  4. labelFirst.ForeColor = labelSecond.ForeColor

在這個例子中,Container 是: ContainerPanel

for (int i = 0; i < ContainerPanel.Controls.Count - 1; i++)
{
    // This is a simple way without variables
    (Label(ContainerPanel.Controls[i])).ForeColor = (Label(ContainerPanel.Controls[i + 1]));
}

注意:最后 Label 你應該改變它的ForeColor ,否則它不會改變。

問答

問:為什么length = ContainerPanel.Controls.Count - 1

A:因為我們應該讓 Cast Control[i + 1]labelSecond ,但我們不能,除非長度小於控制計數一個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM