【问题标题】:WinForm: control won't add to a panelWinForm:控件不会添加到面板
【发布时间】:2015-04-12 18:24:14
【问题描述】:

在 WinForms 上,每次单击按钮时,我都会尝试将控件(此处为简单标签)添加到面板中。 用户界面如下所示:

当我第一次点击按钮时,我得到了这个(我所期望的!):

但是,在第二次、第三次等点击之后,什么也没有发生;不再添加任何标签:(

但是,当我处于调试模式时,我可以在控件列表中看到它们:

这是我的代码(只有有趣的东西):

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());
    }
}

有什么想法吗? 是的,我是 WinForms 的初学者...... 谢谢!

【问题讨论】:

  • 增加面板高度并检查
  • 由于您将它们全部添加到同一点,它们是否有可能相互重叠?
  • 笨蛋,就是这样:都在同一点上!关于高度,面板几乎占据了窗口的所有空间(默认情况下是屏幕截图的大小),所以我认为不是这样。

标签: c# winforms controls add


【解决方案1】:

您的面板高度是固定的。您的控件正在添加到面板中。然而,由于面板高度,它们被隐藏在面板中。您可以增加面板高度或在面板/表单中垂直滚动以使标签可见。

还根据编号定义 Y 位置。标签数量

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

【讨论】:

  • sami 相同:问题确实出在位置上。我也会考虑垂直滚动,谢谢!
猜你喜欢
  • 2016-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-21
  • 1970-01-01
相关资源
最近更新 更多