简体   繁体   English

.NET:如何将 windows 表单转换为 windows 控件

[英].NET: How to convert a windows form to a windows control

I have aa .NET 3.5 windows form which I'd like to embed into another form.我有一个 .NET 3.5 windows 表格,我想将其嵌入到另一种表格中。 Is there a quick way to turn that form into a control?有没有一种快速的方法可以将该表单转换为控件?

Thanks谢谢

Change the form to inherit from UserControl instead of Form, then fix any compile errors.将表单更改为从 UserControl 继承而不是 Form,然后修复任何编译错误。

There's also a way to embed a form in a control: Here's the code in VB:还有一种方法可以在控件中嵌入表单:这是 VB 中的代码:

Public Shared Sub ShowFormInControl(ByRef ctl As Control, ByRef frm As Form)
    If ctl IsNot Nothing AndAlso frm IsNot Nothing Then
        frm.TopLevel = False
        frm.FormBorderStyle = FormBorderStyle.None
        frm.Dock = DockStyle.Fill
        frm.Visible = True
        ctl.Controls.Add(frm)
    End If
End Sub

I think I acquired this code from another post on SO, but I can't remember where, so sorry if this is your code snippet!我想我是从 SO 上的另一篇文章中获得此代码的,但我不记得在哪里,如果这是您的代码片段,请见谅!

Not saying that you should do this now but in the future you can take a look at MEF .并不是说您现在应该这样做,但将来您可以看看MEF Its a framework for (among other things) building composite applications which it sounds like might be what you're trying to achieve.它是一个(除其他外)构建复合应用程序的框架,听起来可能是您想要实现的目标。

I used @Neil Barnwell's solution, with an addition.我使用了@Neil Barnwell 的解决方案,并添加了一些内容。 I manually edited the.vbprog file and changed the form's "SubType" to "UserControl":我手动编辑了.vbprog 文件并将表单的“SubType”更改为“UserControl”:

<SubType>UserControl</SubType>

This allows the icon in the project explorer to show as a User Control, rather than a form.这允许项目资源管理器中的图标显示为用户控件,而不是表单。

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

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