问题描述
我正在使用Microsoft Visual Studio创建Windows窗体应用程序。 **我不知道如何将我的用户控件添加到我的表格中。**我已经看到很多关于如何做到的例子,但我还没有找到一个考虑到性能的答案,至少我是这样的相信。
以下是我使用的两种方法:
1. ** UserControl。可见=真/假**
我见过的一种比较常见的方法是将用户控件放在表单中并将可见性设置为False。只要需要显示用户控件,您只需将Visible设置为True即可。我个人认为这是处理用户控件的一种非常糟糕的方式。当您的表单中有很多控件时,这可能会变得非常迷惑。 *这是我被教导使用的方法。*
代码看起来像这样:
' 用户控件已放置在表单中,可见性设置为False
公开 类 Form_Main
使用户控件可见的按钮
私有 Sub Button_Visible_Click(发件人作为 对象,e As EventArgs)句柄 Button_Visible.Click
UserControl_1.Visible = True
结束 Sub
结束 类
2. **加载用户控制动态**
动态加载用户控件意味着我将用户控件添加到容器(面板等)
代码看起来像这样:
公共 类 Form_Main
' 实例用户控制
公共 共享 UC_Main 作为 新 UserControl_Main
' 表单加载
私有 Sub Form_Main_Load(sen der 作为 对象,e As EventArgs) 句柄 MyBase .Load
' 添加主菜单控件
Panel_Container.Controls.Add(UC_Main)
结束 Sub
结束 Class
**关于性能和方法的问题**
添加和处理用户控件的哪些方法在性能方面最有效?
我尝试过:
我已尝试过上述两种选择,但仍然对如何做到这一点感到困惑。
I am using Microsoft Visual Studio creating a Windows Form Application. **I don't know how I should add my User Controls to my Form.** I have seen many examples on how you can do it, but I am yet to find an answer that takes performance into account, atleast that's what I believe.
Here are the two methods that I have used:
1. **UserControl.Visible = True / False**
One of the more common methods that I have seen is to place your User Control in your Form and setting the visibility to False. Whenever the User Control needs to be visible you simply set Visible to True. I personally feel that this is a very bad way to handling User Controls. This can become very disorienting when you have a lot of controls in your Form. *This is the method I was taught to use.*
The code can look something like this:
' User Control is already placed in the Form and Visibility is set to False Public Class Form_Main ' Button to make User Control Visible Private Sub Button_Visible_Click(sender As Object, e As EventArgs) Handles Button_Visible.Click UserControl_1.Visible = True End Sub End Class
2. **Load User Control "Dynamically"**
Loading a User Control dynamically implies that I add the User Control to a container (Panel, etc.)
The code can look something like this:
Public Class Form_Main ' Instance User Control Public Shared UC_Main As New UserControl_Main ' On Form Load Private Sub Form_Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Add Main Menu Control Panel_Container.Controls.Add(UC_Main) End Sub End Class
**The question about Performance and Method**
What methods of adding and disposing User Controls are most efficient in terms of performance?
What I have tried:
I have tried both of the alternatives above, but are still confused on how I should do this.
这篇关于将用户控件添加到容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!