Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

.net - Can I host a Windows Form inside a control

I have a customer which as a Visual Basic Project in single instance mode with a wired presentation logic.

The main form contains a TabControl with mutliple TabPages. If I click on TabPageA another form is shown in front of the Form and resized to have the same size as the TabPage.

If I click on TabPageB the first form is hidden and another form is displayed. So basically for the user it looks like you have a TabControl with different TabPages which is not the case.

I tried converting the Forms to UserControls and put them inside the TabPage, but, thanks to the SingleInstance app, this would take a whole lot of refactoring. I tried it but eventually gave up because of many many runtime errors and I don't want to put any more effort in this.

My Ideam was that, at runtime, I could add the forms to the TabPages and let them act like UserControls, is this even possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can turn a Form class back to a child control by setting its TopLevel property to False. It becomes essentially a UserControl with some unused overhead. Make it look similar to this:

Public Class Form1
    Public Sub New()
        InitializeComponent()
        Dim frm As New Form2
        frm.TopLevel = False
        frm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        frm.Visible = True
        frm.Dock = DockStyle.Fill
        TabPage1.Controls.Add(frm)
    End Sub
End Class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...