且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将用户控件添加到容器

更新时间:2022-10-15 10:59:17

的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 ...