C# 将用户控件添加到表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15471550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:56:47  来源:igfitidea点击:

add user control to a form

c#winformsuser-controls

提问by Viva

I have created a user control with a TextBox and two Buttons, but I haven't created events, just I place them to user control. When I want to add the user control to my form it says:

我已经创建了一个带有 TextBox 和两个按钮的用户控件,但我还没有创建事件,只是将它们放置到用户控件中。当我想将用户控件添加到我的表单时,它说:

Cannot move Task_control.cs. The destination folder is the same as the source folder.

无法移动 Task_control.cs。目标文件夹与源文件夹相同。

I can't understand why.

我不明白为什么。

采纳答案by Mohammad Dehghan

After creating your user control, you should build the project (F6or Ctrl+Shift+B).

创建用户控件后,你应该建立项目(F6Ctrl+ Shift+ B)。

Then Visual Studio automatically adds the control to the Toolbox. You should drag the control from the Toolbox(Ctrl+Alt+X) to the form.

然后 Visual Studio 自动将控件添加到Toolbox。您应该将控件从工具箱( Ctrl+ Alt+ X)拖到窗体中。

If after successfully building the project, the control is not on the Toolbox, make sure that you have Automatically Populate Toolboxset to true: Go to Tools->Options, select Windows Forms Designernode, and set Automatically Populate Toolboxto true.

如果成功构建项目后,控件不在工具箱上,请确保将自动填充工具箱设置为 true:转到工具->选项,选择Windows 窗体设计器节点,并将自动填充工具箱设置为true



You can also manually add your user control to the toolbox (if "Automatically Populate Toolbox" is not set, and you don't want to set it!). It is highly recommended that you create an additional Tabon the Toolbox for your specific user controls: Right click on an empty area of the Toolbox and click Add Tab, then give the new tab a name.

您还可以手动将您的用户控件添加到工具箱(如果“自动填充工具箱”未设置,并且您不想设置它!)。强烈建议您在工具箱上为您的特定用户控件创建一个附加选项卡:右键单击工具箱的空白区域并单击添加选项卡,然后为新选项卡命名。

Now build the project, right click on an empty area on the toolbox (of course, in tab you already created, or in the "General" tab), and select Choose Items:

现在构建项目,右键单击工具箱上的空白区域(当然,在您已经创建的选项卡中,或在“常规”选项卡中),然后选择选择项目

Adding controls to the Toolbox

向工具箱添加控件

Wait a few seconds (or minutes) for the dialog to be ready, then click Browseand select your assembly. All of your user controls are now in the list. Select the ones you want to add to the Toolbox and click OK.

等待几秒钟(或几分钟)让对话框准备好,然后单击浏览并选择您的程序集。您的所有用户控件现在都在列表中。选择要添加到工具箱的那些,然后单击确定。

回答by Saeid

After creating your user control, you should build the project from the Solution Explorer. Click on your project name and click build, If it doesn't work rebuild it again enter image description here

创建用户控件后,您应该从解决方案资源管理器构建项目。单击您的项目名称并单击构建,如果它不起作用重新构建它,请在此处输入图像描述