将元素添加到表单编程

问题描述 投票:1回答:1

我在Ubuntu写C#代码,并使用CSC编译器,我需要用我不使用Visual Studio IDE Windows.Forms的,只有在一个普通的编辑器编写代码进行编译。在课堂上的基本的ShowDialog()函数的扩展形式运行良好。然而,当我尝试添加使用Controls.Add被()元素,它的编译器显示以下错误:SpanHelpers.Add<T>(IntPtr, int)' is inaccessible due to its protection level如何访问这个特殊的功能?而我在做什么错?

我的代码:

using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

namespace helloWorld
{
    class first
    {
        public static void Main(string[] args)
        {
            FormsSample forms = new FormsSample();
        }
    }


    public partial class FormsSample : Form
    {
        private FolderBrowserDialog fbd;

        private void InitializeComponents()
        {
            this.Controls.Add<FolderBrowserDialog>(fbd);
        }
        public FormsSample()
        {
            fbd = new FolderBrowserDialog();
            InitializeComponents();
            this.Name = "Folder Browser";
            this.AutoScaleDimensions = new SizeF(6F, 13F);
            this.ClientSize = new Size(1000, 500);
            this.CenterToScreen();
            ShowDialog();
        }
    }
}
c# winforms
1个回答
0
投票

Control.ControllCollection没有定义一个通用的方法Add<T>。看来你不知何故尝试引用非公共扩展方法。

你应该改变你的代码,只需使用Add

this.Controls.Add(fbd);
© www.soinside.com 2019 - 2024. All rights reserved.