在VB.net中动态添加用户控件 [英] Dynamically add a usercontrol in VB.net

查看:531
本文介绍了在VB.net中动态添加用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个自定义的UserControl i Vb.net(Windows应用程序)。

I have made a custom UserControl i Vb.net (windows application).

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

How can I add this dynamically to a form?

推荐答案

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

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:


  1. 创建控件的新实例。像 Dim X As New MyControl()

  2. 将控件作为子对象添加到窗体中,并将​​其添加到所需的任何容器中。就像 Me.MyGreatTabPage.Controls.Add(X)。您也可以直接将其添加到表单中,因为表单也是容器。

  3. 设置控件在容器中的位置。那将设置 X.Location X.Size

  1. Create a new instance of your control. Like Dim X As New MyControl()
  2. 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.
  3. Set the controls position within the container. That would be setting X.Location and X.Size.

请记住,使用 New MyControl()创建的每个实例将是一个单独的MyControl。不要犯重复创建新控件并将它们以某种方式彼此放置的错误。创建并放置一次控件。将其分配给表单的成员变量,并在需要使用它时使用此变量。

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.

这篇关于在VB.net中动态添加用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆