简体   繁体   English

表单可以用作用户控件吗?

[英]Can a Form be used as a user control?

I need to make a UserControl that can be used for multiple projects. 我需要制作一个可以用于多个项目的UserControl。 But it needs to be a Form so the user can just add a reference to the library and call the form. 但是它必须是一个Form,以便用户可以仅添加对库的引用并调用该Form。

I've seen third party companies like Telerik and DevExpress use custom forms that can be added to a project. 我已经看到第三方公司(例如Telerik和DevExpress)使用可以添加到项目中的自定义表单。

How would I accomplish this? 我将如何完成? I've been looking through SO and various posts from Google, but have not been successful in my searches. 我一直在浏览SO和Google的各种帖子,但是搜索没有成功。

EDIT I was assuming it had to be a UserControl for some reason. 编辑我以为由于某种原因它必须是一个UserControl。 But it doesn't. 但事实并非如此。 I took the suggestion of just adding a form and calling it from that namespace. 我建议只添加一个表单并从该名称空间调用它。 Works exactly as needed. 完全根据需要工作。 Thanks everyone. 感谢大家。

Just create the form in your library, make it public, and you can call it from anywhere. 只需在您的库中创建表单,将其公开即可,您可以从任何地方调用它。

Methods to create and call form are: 创建和调用表单的方法是:

YourFormClassName FormForUser = new YourFormClassName();
FormForUser.Show();
FormForUser.ShowDialog();

Maybe I don't understand. 也许我听不懂。 If I do, then it's straight forward. 如果我这样做,那就很简单。

  1. Add a project (ProjectWithReusedForm) to your solution that contains the form to be reused. 将一个项目(ProjectWithReusedForm)添加到您的解决方案中,该项目包含要重用的表单。
  2. Add a reference to ProjectWithReusedForm in the second project where you want to use the form 在要使用表单的第二个项目中添加对ProjectWithReusedForm的引用
  3. Add a 'using ProjectWithReusedFormNamespace' to the code where you want to use the form 在要使用表单的代码中添加“ using ProjectWithReusedFormNamespace”

You then can add the statement ReusedForm myForm = new ReusedForm(); 然后,您可以添加语句ReusedForm myForm = new ReusedForm();。

Just Create form with all controls. 只需创建带有所有控件的表单即可。 and create empty user control 并创建空的用户控件

Ex: 例如:

do this code inside usercontrol constructor after initialize function 初始化函数后在usercontrol构造函数中执行此代码

dim obja as new RegForm()
obja.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
obja.Visible = true
obja.Dock = System.Windows.Forms.DockStyle.Full
me.Controls.Add(obja)

You can create BaseForm (either add it into a project directly by adding .cs file or reference something compiled - class library to example). 您可以创建BaseForm (可以通过添加.cs文件将其直接添加到项目中,也可以将已编译的类库引用到示例中)。 Then just add a new form to a project 然后只需向项目添加新表单

namespace MySolution
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

and chang Form to BaseForm 并将Form更改为BaseForm

    public partial class Form1 : BaseForm

You have to be careful here. 您在这里一定要小心。 Your tag lists winforms, so I am assuming you are using .net and UserControls. 您的标签列出了winforms,所以我假设您正在使用.net和UserControls。 Winforms only allows a single form per page. Winforms每页只允许一个表单。 You can add multiple UserControls to a page. 您可以将多个UserControls添加到一个页面。 If you go with the base form route, the programmer will have to add everything else to your base page. 如果您使用基本表单路由,那么程序员将不得不将其他所有内容添加到您的基本页面中。 UserControls will offer a little more flexibility in that they can be added to an existing page. UserControls可以提供更多的灵活性,因为它们可以添加到现有页面中。

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

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