简体   繁体   English

WinForm:控件不会添加到面板中

[英]WinForm: control won't add to a panel

On a WinForms, I'm trying to add a control (here a simple label) to a panel each time I click a button. 在WinForms上,每次单击按钮时,我都尝试向面板添加控件(此处为简单标签)。 The UI looks like this: 用户界面如下所示: 一开始

when I first click the button, I got this (what I expect!): 当我第一次单击该按钮时,我得到了(我期望的是!): 第一次点击后

but, after a second, a third, etc. click, nothing more happens; 但是,在第二,第三等点击之后,什么也没有发生; no labels are added anymore :( 不再添加标签了:(

However, I can see them in the list of controls when I'm in debug mode: 但是,在调试模式下,我可以在控件列表中看到它们: 我能看见你!

Here's my code (only the interesting stuff): 这是我的代码(只有有趣的东西):

public partial class GestionPateFeuilletee : Form
{
    private List<Label> listeTours = new List<Label>();

    public GestionPateFeuilletee()
    {
        InitializeComponent();
    }

    private void boutonAjouterTour_Click(object sender, EventArgs e)
    {
        Point coordDepart = new Point(10, 160);
        int tabIndexDepart = 5;

        listeTours.Add(new Label());
        listeTours.Last().Name = "labelTour" + (listeTours.Count());
        listeTours.Last().Location = new System.Drawing.Point(coordDepart.X, coordDepart.Y + 30);
        listeTours.Last().TabIndex = tabIndexDepart + 1;
        listeTours.Last().Text = "labelTour" + (listeTours.Count());

        this.panelDescription.Controls.Add(listeTours.Last());
    }
}

Any idea? 任何想法? And yes, I'm a beginner with WinForms... Thanks! 是的,我是WinForms的初学者...谢谢!

Your panel height is fixed. 您的面板高度是固定的。 Your controls are getting added to the panel. 您的控件将添加到面板中。 However they are getting hidden in the panel because of the panel height. 但是,由于面板的高度,它们被隐藏在面板中。 You can either increase the panel height or put a vertical scroll in the panel / form to have the labels visible. 您可以增加面板高度,也可以在面板/表单中放置垂直滚动条以使标签可见。

Also define the Y location based on the no. 还要根据编号定义Y位置。 of labels 标签的

listeTours.Last().Location = new System.Drawing.Point(coordDepart.X, coordDepart.Y + ((listeTours.Count() + 1) * 30));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM