Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在VB.net中动态添加usercontrol_.net_Vb.net_Dynamic_User Controls_Windows - Fatal编程技术网

在VB.net中动态添加usercontrol

在VB.net中动态添加usercontrol,.net,vb.net,dynamic,user-controls,windows,.net,Vb.net,Dynamic,User Controls,Windows,我在Vb.net(windows应用程序)中创建了一个自定义用户控件 如何将其动态添加到表单中?我认为您需要的是:C#中的this.Controls.add(myControl)。我敢肯定它在VB中也很相似?用户控件本质上只是另一个类。它继承自控件,因此您可以使用控件执行各种操作,但除此之外,它只是一个类。因此,要将usercontrol动态添加到表单中,需要执行以下操作: 创建控件的新实例。像Dim X一样作为新的MyControl() 将控件作为子对象添加到窗体中,并添加到所需的任何容器中。

我在Vb.net(windows应用程序)中创建了一个自定义用户控件


如何将其动态添加到表单中?

我认为您需要的是:C#中的this.Controls.add(myControl)。我敢肯定它在VB中也很相似?

用户控件本质上只是另一个类。它继承自控件,因此您可以使用控件执行各种操作,但除此之外,它只是一个类。因此,要将usercontrol动态添加到表单中,需要执行以下操作:

  • 创建控件的新实例。像
    Dim X一样作为新的MyControl()
  • 将控件作为子对象添加到窗体中,并添加到所需的任何容器中。比如
    Me.MyGreatTabPage.Controls.Add(X)
    。您也可以将其直接添加到表单中,因为表单也是一个容器
  • 在容器内设置控件的位置。这将是设置
    X.Location
    X.Size
  • 请记住,使用
    New MyControl()
    创建的每个实例都将是一个单独的MyControl。不要犯反复创建新控件并以某种方式将它们相互覆盖的错误。创建并放置控件一次。将其分配给窗体的成员变量,当需要使用它时,使用此变量

    Form.Controls.Add(Page.LoadControl("SomeUserControl.ascx"))
    
    然后是最难的部分,因为每个请求都需要重新加载它,所以在其中捕获事件。我通常使用ViewState标志来表示它已经加载,并检查该标志是否存在,以查看是否应该在OnInit中再次重新加载它

    Dim newControl As UserControl = LoadControl("~/Controls/DRQ/Create/UCNewControl.ascx")
    Me.panelHolder1.Controls.Add(newControl)
    

    这是一种用于添加两个或多个:

    Private _userControlList As New List(Of YourControl)
    
    Private Sub AddingControlOnPanel()
        Dim index As Integer = _userControlList.Count + 1
        Dim userControl As New YourControl
        userControl.Location = New System.Drawing.Point(SomeLocation)
        userControl.Size = New System.Drawing.Size(SomeSize)
        userControl.Name = "userControl" + index.ToString
        userControl.Visible = False
        _userControlList.Add(userControl)
        UserControlsPanel.Controls.Add(userControl)
        userControl.Visible = True
    End Sub
    

    我想指定每次程序运行时将添加多少UserControls。+1用于提醒我不要在每次选项卡页更改时创建控件。有没有办法添加使用string指定类型的控件,而不是使用其构造函数创建新实例?@arejqadomi-如果可能性有限,然后可以使用
    selectcase
    语句。否则,您需要所谓的“反射”。这是一个较长的故事。
    Private _userControlList As New List(Of YourControl)
    
    Private Sub AddingControlOnPanel()
        Dim index As Integer = _userControlList.Count + 1
        Dim userControl As New YourControl
        userControl.Location = New System.Drawing.Point(SomeLocation)
        userControl.Size = New System.Drawing.Size(SomeSize)
        userControl.Name = "userControl" + index.ToString
        userControl.Visible = False
        _userControlList.Add(userControl)
        UserControlsPanel.Controls.Add(userControl)
        userControl.Visible = True
    End Sub