Skip to main content
 首页 » 编程设计

C# 按钮可见性问题

2024年02月13日13Terrylee

我已经以编程方式向 C# 窗体添加了几个按钮,但只有我通过设计器添加的按钮才真正显示出来。尽管如此,其余的仍然不可见。在此之前我正在执行很多操作;这可能是原因吗?

public FormView 
{ 
    List<Button> listOfButtons = new List<Button>(); 
    int frame = 0; 
    for (int i = 0; i < 36; i++) 
    { 
        listOfButtons.Add(new Button()); 
    } 
    listOfButtons[4 * frame].Width = 92; 
    listOfButtons[4 * frame].Height = 92; 
    listOfButtons[4 * frame].BackColor = Color.Red; 
    listOfButtons[4 * frame].BackgroundImage = System.Drawing.Image.FromFile("image.png"); 
    listOfButtons[4 * frame].BackgroundImageLayout = ImageLayout.Stretch; 
    listOfButtons[4 * frame].ImageAlign = ContentAlignment.MiddleCenter; 
    listOfButtons[4 * frame].TextAlign = ContentAlignment.TopCenter; 
    listOfButtons[4 * frame].Location = new Point(1, 0); 
    listOfButtons[4 * frame].Click += new EventHandler(this.button1_Click); 
 
    listOfButtons[4 * frame + 1].Width = 92; 
    listOfButtons[4 * frame + 1].Height = 92; 
    listOfButtons[4 * frame + 1].BackColor = Color.Red; 
    listOfButtons[4 * frame + 1].BackgroundImage = System.Drawing.Image.FromFile("image.png"); 
    listOfButtons[4 * frame + 1].BackgroundImageLayout = ImageLayout.Stretch; 
    listOfButtons[4 * frame + 1].ImageAlign = ContentAlignment.MiddleCenter; 
    listOfButtons[4 * frame + 1].TextAlign = ContentAlignment.TopCenter; 
    listOfButtons[4 * frame + 1].Location = new Point(1, 99); 
    listOfButtons[4 * frame + 1].Click += new EventHandler(this.button2_Click); 
 
    listOfButtons[4 * frame + 2].Width = 92; 
    listOfButtons[4 * frame + 2].Height = 92; 
    listOfButtons[4 * frame + 2].BackColor = Color.Red; 
    listOfButtons[4 * frame + 2].BackgroundImage = System.Drawing.Image.FromFile("image.png"); 
    listOfButtons[4 * frame + 2].BackgroundImageLayout = ImageLayout.Stretch; 
    listOfButtons[4 * frame + 2].ImageAlign = ContentAlignment.MiddleCenter; 
    listOfButtons[4 * frame + 2].TextAlign = ContentAlignment.TopCenter; 
    listOfButtons[4 * frame + 2].Location = new Point(99, 0); 
    listOfButtons[4 * frame + 2].Click += new EventHandler(this.button3_Click); 
 
    listOfButtons[4 * frame + 3].Width = 92; 
    listOfButtons[4 * frame + 3].Height = 92; 
    listOfButtons[4 * frame + 3].BackColor = Color.Red; 
    listOfButtons[4 * frame + 3].BackgroundImage = System.Drawing.Image.FromFile("image.png"); 
    listOfButtons[4 * frame + 3].BackgroundImageLayout = ImageLayout.Stretch; 
    listOfButtons[4 * frame + 3].ImageAlign = ContentAlignment.MiddleCenter; 
    listOfButtons[4 * frame + 3].TextAlign = ContentAlignment.TopCenter; 
    listOfButtons[4 * frame + 3].Location = new Point(99, 99); 
    listOfButtons[4 * frame + 3].Click += new EventHandler(this.button4_Click); 
 
 
    listOfButtons[4 * frame].Visible = true; 
    listOfButtons[4 * frame + 1].Visible = true; 
    listOfButtons[4 * frame + 2].Visible = true; 
    listOfButtons[4 * frame + 3].Visible = true; 
    InitializeComponent(); 
} 

有什么想法吗?

请您参考如下方法:

我没有看到您将按钮添加到任何父控件。 你应该做这样的事情......

Form1.Controls.Add(listOfButtons[4 * frame]); // and so on...