【问题标题】:How do I Open Labels/Buttons with C# code?如何使用 C# 代码打开标签/按钮?
【发布时间】:2015-01-04 18:23:19
【问题描述】:

看看代码,我希望你能明白我在做什么:

private void btnOpen_Click(object sender, EventArgs e)
{
        string[] Folders = Directory.GetDirectories(txtFolder.Text);
        string foldername;
        int count=0;
        foreach (string f in Folders)
        {
            foldername = Path.GetDirectoryName(f);
            Label newlabe = new Label();
            newlabe.Location = new Point(12, 58);
            newlabe.Text = foldername;
            count++;
        }
}

如您所见,我在文本框中插入了一个目录路径,然后我打开了一个包含子目录的数组,下一步是打开包含从我插入到文本框的目录中的子目录名称的标签,这不工作,我该怎么办?

【问题讨论】:

  • 您没有将标签添加到表单中。而且您对每个标签位置使用相同的点,因此一旦您将它们添加到表单中,它们就会堆叠在一起
  • 考虑将文件夹名称发布到列表框。您不必担心位置

标签: c# arrays path location labels


【解决方案1】:

使用某种容器并将标签堆叠/添加到其中。您无需为标签指定位置,因为容器(取决于容器布局算法)会为您布置它们。

我不知道你是用WinForms还是WPF还是别的什么,所以我不会写任何示例代码。 但这里有一些伪代码:

create a container and add it to the form
for each folder
    create a label for the folder
    add the label to the container

顺便问一下,你试过TreeView控件吗?

【讨论】:

  • 我在使用 WinForms ,我对 TreeView 控件不熟悉......如果你能写一个代码那就太好了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-24
  • 1970-01-01
  • 2012-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-20
相关资源
最近更新 更多