首页 > 解决方案 > 如何在 form1.control 之上添加一个 form2 控件

问题描述

我有两种形式。带有richtextbox 的Form1 和带有列表框的form2。

我想把列表框放在richtextbox 的顶部,对接它。我想做这样的事:

一切正常,直到将 form2.listbox1.location 设置为如果richtextbox1 被隐藏,列表框也会被隐藏。此外,将列表框的位置和/或边界设置为与richtextbox 相同并不能完全覆盖richtextbox。我也尝试对两者使用相同的尺寸。

GIF 示例

标签: vb.netcontrols

解决方案


这对我有用:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim LB As ListBox = Form2.ListBox1
    If IsNothing(LB.Tab)
        LB.Tag = LB.Size ' <-- store it for later use
    End If
    Me.Controls.Add(LB)
    LB.Bounds = RichTextBox1.Bounds
    RichTextBox1.Hide()
End Sub

稍后,当您将 ListBox 移回时,检索存储在 Tag 中的大小:

If Not IsNothing(LB.Tab)
    LB.Size = LB.Tag
End If

输出:

在此处输入图像描述


推荐阅读