VB.net动态添加用户控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/374082/
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
Dynamically add a usercontrol in VB.net
提问by eflles
I have made a custom UserControl i Vb.net (windows application).
我制作了一个自定义的 UserControl i Vb.net(Windows 应用程序)。
How can I add this dynamically to a form?
如何将其动态添加到表单中?
回答by Vilx-
A UserControl is essentially just another class. It inherits from Control, so you can do all kinds of things you do with controls, but otherwise it's just a class. Thus, to add the usercontrol dynamically to your form you'd do the following:
UserControl 本质上只是另一个类。它继承自 Control,因此您可以使用控件执行各种操作,否则它只是一个类。因此,要将用户控件动态添加到表单中,您需要执行以下操作:
- Create a new instance of your control. Like
Dim X As New MyControl() - Add the control to your form as a child object to whatever container you want it. Like
Me.MyGreatTabPage.Controls.Add(X). You can also add it directly to your form too, because a form is also a container. - Set the controls position within the container. That would be setting
X.LocationandX.Size.
- 创建控件的新实例。喜欢
Dim X As New MyControl() - 将控件作为子对象添加到您的表单中,添加到您想要的任何容器中。喜欢
Me.MyGreatTabPage.Controls.Add(X)。您也可以将其直接添加到表单中,因为表单也是一个容器。 - 设置容器内的控件位置。那将是设置
X.Location和X.Size。
Remember that each instance you create with New MyControl()will be a separate MyControl. Don't make the mistake of repeatedly creating new controls and placing them over each other somehow. Create and place the control once. Assign it to a member variable to your form, and when you need to work with it, use this variable.
请记住,您创建的每个实例New MyControl()都是一个单独的 MyControl。不要犯重复创建新控件并将它们以某种方式相互重叠的错误。创建并放置控件一次。将其分配给表单的成员变量,当您需要使用它时,请使用此变量。
回答by Commander Keen
I think what you're looking for is written as: this.Controls.Add(myControl) in C#. I'm sure it's very similar in VB too?
我认为您要查找的内容在 C# 中是这样写的:this.Controls.Add(myControl)。我确定它在 VB 中也非常相似?
回答by Thomas Hansen
Form.Controls.Add(Page.LoadControl("SomeUserControl.ascx"))
Then comes the hard part with trapping eventsin it since it needs to be reloaded every request. I normally use a ViewState flag to signify it's already loaded and the check for the existence of that flag to see if I sould reload it again in OnInit
然后是在其中捕获事件的困难部分,因为它需要在每个请求中重新加载。我通常使用 ViewState 标志来表示它已经加载并检查该标志是否存在以查看我是否应该在 OnInit 中再次重新加载它
Dim newControl As UserControl = LoadControl("~/Controls/DRQ/Create/UCNewControl.ascx")
Me.panelHolder1.Controls.Add(newControl)
回答by Christian Payne
For i As Integer = 1 To 10
Dim tb As New TextBox
tb.Top = 26 * i
tb.Left = 12
tb.Text = "text box " & i.ToString()
tb.Parent = Me
Next
回答by Viacheslav
This is a method for adding two or more:
这是一种添加两个或多个的方法:
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

