can anyone help me to figure out how to nest forms on visual basic??
example:
i would like to link form1 to form2?

Dani AI

Generated

— "nesting" can mean different things: showing a second form, embedding one UI inside another, or deriving a form class. The replies from and touch on these, but they need a bit of clarification. Directly setting controls on another form (what showed) creates tight coupling and can lead to nulls or circular-access problems if both forms try to touch each other during Load. Inheriting a form (what suggested) produces a subclass for reuse, but it does not “embed” one form inside another at runtime.

A safer, common pattern for passing data is to expose properties or use the constructor, then show the form modally and read results back:

' In Form1 (caller)
Dim dlg As New Form2()
dlg.SomeText = "value to pass"
If dlg.ShowDialog() = DialogResult.OK Then
    Dim returned = dlg.ResultValue
End If

If the goal is multiple-document UI, use MDI: set the parent form's IsMdiContainer = True, set child.MdiParent = parent, then child.Show(). For embedding a form into a panel (less common but works), set TopLevel = False, remove borders and add it to a container:

Dim child As New Form2()
child.TopLevel = False
child.FormBorderStyle = FormBorderStyle.None
child.Dock = DockStyle.Fill
Panel1.Controls.Add(child)
child.Show()

See Form.TopLevel and Form.MdiParent for details.

Prefer creating a UserControl for reusable embedded UI instead of nesting full Form instances. Use properties/events for communication between forms (or callbacks) rather than directly manipulating another form's controls, and avoid mutual access during Load to prevent runtime race/null issues.

Recommended Answers

All 3 Replies

i don't really understand the question. can u give an example?

can anyone help me to figure out how to nest forms on visual basic??
example:
i would like to link form1 to form2?

Something like this?
Pre-requisites: 2 Forms, each containing a Label.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Form2.Label1.Text = "text from Form1"
        Form2.Show()
    End Sub
End Class
Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Form1.Label1.Text = Form1.Text
    End Sub
End Class

can anyone help me to figure out how to nest forms on visual basic??
example:
i would like to link form1 to form2?

You could use the inherits Method

Try this
Public Class Form2
Inherits Namespace1.Form1

Good Luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.