将用户控件添加到容器 [英] Add user control to container

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

问题描述

我正在使用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





**关于性能和方法的问题**



添加和处理用户控件的哪些方法在性能方面最有效?



我尝试过:



我已尝试过上述两种选择,但仍然对如何做到这一点感到困惑。

解决方案
的re bis no generell方式回答你的问题。

基本上:

作为你的表格的一部分的控制或你的表格上的控制的一部分(如Panel)最终属于到表格(面板属于表格,控制权属于面板)。

所以效率没有区别..除了:Panel是一个ContainerControl,它可以为你做一些共同的功能(如果将Panel切换为Visible = False,则所有包含的控件也将变为不可见。同一行为有你的UserControl(因为它也是一个ContainerControl)。



如果你需要一个控件在你的表格上,你应该把它放在那里。如果你需要控制取决于一个状态你使它可见或不可见(使用它的属性)或可用于启用。

动态创建控件是一个不错的选择,如果你想拥有(例如)根据不同的设置进行控制(输入到文本框中)。这些控件将被实例化并在需要时添加到Container的集合集合中(通常不会放置)。



给你一个建议(对我而言)它是有必要获得有关您的问题的更多信息......


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.

解决方案

There bis no generell way to answer your question.
Basicly :
A Control which is part of your Form OR part of a Control on your Form (like Panel) belongs finally to the Form (the Panel belongs to the Form and the Control belongs to the Panel).
So there is no difference in efficiency .. except : a Panel is a ContainerControl which could do common functionality for you (if you switch the Panel to Visible=False then all included Controls become also invisible. The same Behaviour has your UserControl (because it's also a ContainerControl).

If you need a Control on your Form you should place it there. If you need the Control depending on a state you make it visible or invisible (with it's Property) or usable with Enabled.
A dynamicly created Control is a good choice if you want to have (for example) Controls depending on different settings (an input into a Textbox). Those Controls will be instanced and added to the Controls-Collection of the Container when needed (and not generally placed).

To give you an advice (for me) it's necessary to have more information about your issue ...


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

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