C# 只有前两个控件在tableLayoutPanel中分配了单击事件

C# 只有前两个控件在tableLayoutPanel中分配了单击事件,c#,winforms,C#,Winforms,我有一张桌子,如图左侧所示 我使用以下方法填充它 private void populateQuestionTable() { answerTable.Controls.Add(new Button() { Text = "1", Font = new Font("Arial", 12) }, 0, 0); answerTable.Controls.Add(new Label() { Text = "A", Font = new Font("Arial"

我有一张桌子,如图左侧所示

我使用以下方法填充它

private void populateQuestionTable()
    {
        answerTable.Controls.Add(new Button() { Text = "1", Font = new Font("Arial", 12) }, 0, 0);
        answerTable.Controls.Add(new Label() { Text = "A", Font = new Font("Arial", 12) }, 1, 0);
        answerTable.Controls.Add(new Label() { Text = "B", Font = new Font("Arial", 12) }, 2, 0);
        answerTable.Controls.Add(new Label() { Text = "C", Font = new Font("Arial", 12) }, 3, 0);
        answerTable.Controls.Add(new Label() { Text = "D", Font = new Font("Arial", 12) }, 4, 0);
        answerTable.Controls.Add(new Label() { Text = "E", Font = new Font("Arial", 12) }, 5, 0);
        for( int i = 1; i < 50; i++ )
        {
            answerTable.RowCount = answerTable.RowCount + 1;
            answerTable.RowStyles.Add(new RowStyle(SizeType.Absolute, 40));

            answerTable.Controls.Add(new Button() { Text = Convert.ToString(i + 1), Font = new Font("Arial", 12) }, 0, i);
            answerTable.Controls.Add(new Label() { Text = "A", Font = new Font("Arial", 12) }, 1, i);
            answerTable.Controls.Add(new Label() { Text = "B", Font = new Font("Arial", 12) }, 2, i);
            answerTable.Controls.Add(new Label() { Text = "C", Font = new Font("Arial", 12) }, 3, i);
            answerTable.Controls.Add(new Label() { Text = "D", Font = new Font("Arial", 12) }, 4, i);
            answerTable.Controls.Add(new Label() { Text = "E", Font = new Font("Arial", 12) }, 5, i);
        }

        for (int i = 0; i < 50; i++)
            (answerTable.GetControlFromPosition(0, i) as Button).Click += showQuestion;
    }  
private void populateQuestionTable()
{
Add(new Button(){Text=“1”,Font=new Font(“Arial”,12)},0,0);
Add(new Label(){Text=“A”,Font=new Font(“Arial”,12)},1,0);
Add(new Label(){Text=“B”,Font=new Font(“Arial”,12)},2,0);
Add(new Label(){Text=“C”,Font=new Font(“Arial”,12)},3,0);
Add(new Label(){Text=“D”,Font=new Font(“Arial”,12)},4,0);
Add(new Label(){Text=“E”,Font=new Font(“Arial”,12)},5,0);
对于(int i=1;i<50;i++)
{
answerTable.RowCount=answerTable.RowCount+1;
添加(新的行样式(SizeType.Absolute,40));
Add(new Button(){Text=Convert.ToString(i+1),Font=new Font(“Arial”,12)},0,i);
Add(new Label(){Text=“A”,Font=new Font(“Arial”,12)},1,i);
Add(new Label(){Text=“B”,Font=new Font(“Arial”,12)},2,i);
Add(new Label(){Text=“C”,Font=new Font(“Arial”,12)},3,i);
Add(new Label(){Text=“D”,Font=new Font(“Arial”,12)},4,i);
Add(new Label(){Text=“E”,Font=new Font(“Arial”,12)},5,i);
}
对于(int i=0;i<50;i++)
(answerTable.GetControlFromPosition(0,i)作为按钮)。单击+=showQuestion;
}  
但是,只有第一个和第二个按钮在实际单击时才会触发单击事件。
我对C#有点陌生,所以我对这方面的知识可能有限。有多少个控件可能具有分配给它们的功能有限制吗?

如果没有,那么我编写的代码有什么问题?

您的第二个for循环似乎不正确。看起来您缺少了一组花括号({})。还有,为什么要从0迭代到50,就像在第一个循环中一样,从1迭代到50?@Eminem我不会将第一行添加到表中,只会添加以下内容。如果看起来不对,你能解释一下原因吗?它使用的是正确的COLLMN和row,据我所知,除了第一行和第二行之外,它似乎没有注册任何东西。乍一看,它看起来还行。将光标放在
showQuestion
上,右键单击并选择断点添加。运行F5并检查您点击注册表的频率。没有明显的错误。您是否已通过查看
GetControlForPosition
返回了哪些按钮?你应该能够检查
文本
属性。我已将你的代码粘贴到一个新项目中,每个按钮都工作正常,很遗憾,你的代码不足以重现你的问题。