Question : How to Display a User Control on Form in CODE

I have created a user control and it is in my TOOLBOX.

I want to display the control at a certain location based on a tree view selection.

I will eventually have MANY user controls that will be displayed to the right of the tree view based on various selections...

How in code can I invoke a user control and display it at a particular location on a form?

This is what I have so far...there are no errors, but the control does not show.

Public Class frmMain
    Inherits System.Windows.Forms.Form

    'Global User Control
    Dim ctrlProjectInfo As ctrlProjectInformation = New ctrlProjectInformation

#Region " Windows Form Designer generated code "

    '*********************************
    'User want to exit the application
    '*********************************
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Application.Exit()
    End Sub

    '*******************************
    'User selects the tree view node
    '*******************************
    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        If TreeView1.SelectedNode.Text = "Project Information" Then
            ctrlProjectInfo.Show() <------------- WANT TO DISPLAY THE USER CONTROL
        Else
            < ----- WANT TO REMOVE THE USER CONTROL
        End If
    End Sub

    '*************************
    'To do when the form loads
    '*************************
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ctrlProjectInfo.Location = New Point(248, 48) <------------ SET LOCATION
        ComboBox1.SelectedIndex = 0
    End Sub
End Class

Answer : How to Display a User Control on Form in CODE

Here's code that works for me:

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim MyControl As New UserControl1
    Me.Controls.Add(MyControl)
    MyControl.Location = New Point(100, 100)
    MyControl.Visible = True
  End Sub
Random Solutions  
 
programming4us programming4us