vb.net 将表单加载到面板中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30904538/
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-09-17 19:13:24  来源:igfitidea点击:

Load a form into a panel

vb.netwinforms

提问by crimson

I have a panel and a button, when I click the button I want to load a form inside the panel.

我有一个面板和一个按钮,当我单击按钮时,我想在面板内加载一个表单。

This is how i'm loading the form into the panle

这就是我将表单加载到面板中的方式

Dim f As New Form()
f.TopLevel = False
f.WindowState = FormWindowState.Maximized
f.FormBorderStyle = Windows.Forms.FormBorderStyle.None
f.Visible = True
Panel1.Controls.Add(f)

My problem is once the form loads, everything is stretched. Controls do not look like what they are in the form once it loads inside the panel. I tried making the form smaller than the panel, still stretched. I also tried not maximizing the window in the panel and just using SizableBorder

我的问题是一旦表单加载,一切都会被拉伸。一旦加载到面板中,控件看起来就不像它们在表单中的样子。我试着让表格比面板小,但仍然拉伸。我也尝试不最大化面板中的窗口而只使用SizableBorder

回答by BlinkSun

I tried your code and I saw the form inside the panel maximized, So I only put this line:

我试过你的代码,我看到面板里面的表格最大化了,所以我只放了这一行:

f.WindowState = FormWindowState.Normal

before setting the visible to true with the button.

在使用按钮将可见设置为 true 之前。

Public f As New Form()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    f.TopLevel = False
    f.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
    f.Size = New System.Drawing.Size(200, 150)
    f.Location = New System.Drawing.Point(20, 20)
    f.WindowState = FormWindowState.Normal
    f.Visible = False
    Panel1.Controls.Add(f)
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If f.Visible = False Then f.Visible = True Else f.Visible = False
End Sub

Screenshot of what I gotbecause I don't have enough reputation to post image (my first answer with stackoverflow). You can see the panel in green. I set the BorderStyle like that just to show you the result but it's working with property to None and added Size/Location to position the form. Hope this ll help you.

我得到的截图,因为我没有足够的声誉来发布图像(我用 stackoverflow 的第一个答案)。您可以看到绿色面板。我像这样设置 BorderStyle 只是为了向您展示结果,但它使用属性为 None 并添加了大小/位置来定位表单。希望这会帮助你。