自定义控件中的面板不保持控件 [英] Panel in custom control not holding controls

查看:90
本文介绍了自定义控件中的面板不保持控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在VB.Net中创建了一个类,它继承了面板类,所以我将它用作我的应用程序中的控件。这个面板类中有另一个面板,它在自定义面板的new()事件中生成,我们称之为pnl。我可以在pnl中添加控件(例如,一个按钮),但是当我构建应用程序时,按钮的父级会更改为自定义类,其中包含pnl。如何通过visual studio designer在pnl中添加项目?

这是我使用的代码:

Hi,
I've created a class in VB.Net, it inherits panel class, so I'll use it as a control in my application. This panel class has another panel in it which is generated in custom panel's new() event, lets call it "pnl". I can add controls in the pnl(For example, a button), but when I build the application, button's parent is changed to custom class which holds pnl in it. How can I add items in pnl via visual studio designer?
Here is the code I use:

Public Class ScrollPanel
    Inherits Panel
    Public WithEvents pnl As New Panel With {.Name = "pnl", .Location = New Point(0, 0), .Size = New Size(Me.Width - 10, Me.Height), .BackColor = Color.White}

    Public Sub New()
        MyBase.Controls.Add(pnl)
        MyBase.Controls.Add(kg)
        kg.KayanPanel = pnl
        pnl.Enabled = True
    End Sub
End Class

推荐答案

我不完全确定你在尝试什么实现和使用类来动态显示具有set属性的控件意味着您将自己限制为该控件的单个实例。如果还有另一个面板,那么对该课程的任何额外调用都会将下一个面板放在退出的面板之上。



如果我没有弄错,你可以做与以下代码相同,您可以为每个新动态添加的面板控件单独设置属性,添加父面板。



以下是创建Picturebox的示例动态地为文件夹中的每个图像文件。该代码还将AddHandler用于可以在Picturebox上发布的事件,如Click事件。该代码还控制每个新添加的Picturebox的位置,并设置每个Picturebox的属性。对于添加到表单或现有父控件的任何控件,机制/概念保持不变。



I am not entirely sure what you are trying to achieve and using a class to display a control dynamically with set properties means you limiting yourself to a single instance of that control. Any additional calls to that class will place the next panel on top of the exiting one, if there is already another panel.

If I am not mistaken you can do the same with the following code while you can set the properties separately for each new dynamically added panel control you add the the parent panel.

Here is an example of creating a Picturebox dynamically for each image file in a folder. The code also uses AddHandler for event that can be issued on the Picturebox, like a Click event. The code also controls the placement of each newly added Picturebox and sets the properties for each Picturebox. The mechanics / concept remains the same for any control you add to a form or existing parent control.

Private Sub btnLoadImage_Click(sender As System.Object, e As System.EventArgs) Handles btnLoadImage.Click
        Dim Left As Integer = 12
        Dim Top As Integer = 0
        Dim picBox As PictureBox

        pgbImagesLoaded.Value = 0

        Me.Cursor = Cursors.WaitCursor
        Try
            'Check if there are any images in the shared folder - if not then return
            If System.IO.Directory.GetFiles(fbdGetImagesOnDisk.SelectedPath).Length - 1 < 1 Then
                MsgBox("No images were found in the selected folder.", MsgBoxStyle.Information)
                Me.Cursor = Cursors.Arrow
                Return
            End If

            pgbImagesLoaded.Maximum = System.IO.Directory.GetFiles(fbdGetImagesOnDisk.SelectedPath).Length - 1
            lblFilesProcessed.Text = "0/" & pgbImagesLoaded.Maximum

            For Each imgFile As String In Directory.GetFiles(fbdGetImagesOnDisk.SelectedPath)

                Dim fs As System.IO.FileStream
                imgInfo = New FileInfo(imgFile)
                'Limit image files to JPG only
                If myext.Contains(LCase(imgInfo.Extension)) Then
                    picBox = New PictureBox()
                    'Specify a valid picture file path
                    fs = New System.IO.FileStream(imgInfo.FullName, IO.FileMode.Open, IO.FileAccess.Read)
                    picBox.Image = System.Drawing.Image.FromStream(fs).GetThumbnailImage(128, 128, Nothing, Nothing)
                    fs.Close()
                    'Postion control
                    If Left > pnlImagesFromDisk.Width - 138 Then
                        Left = 12
                        Top = Top + 134 '138
                    End If

                    With picBox
                        .Size = New Size(128, 128)
                        .SizeMode = PictureBoxSizeMode.Zoom
                        .Top = Top
                        .Left = Left
                        .Visible = True
                        .Tag = imgInfo.Name
                    End With
                    ttLoadedImages.SetToolTip(picBox, "Single Click to View" & vbCrLf & "Double Click to add to selection")
                    pnlImagesFromDisk.Controls.Add(picBox)

                    pgbImagesLoaded.Value += 1

                    lblFilesProcessed.Text = pgbImagesLoaded.Value & "/" & pgbImagesLoaded.Maximum
                    Application.DoEvents()

                    AddHandler picBox.Click, New System.EventHandler(AddressOf PreviewImage)
                    AddHandler picBox.DoubleClick, New System.EventHandler(AddressOf AddToSelection)
                    AddHandler picBox.MouseEnter, New System.EventHandler(AddressOf pnlImages_MouseEnter)
                    AddHandler picBox.MouseLeave, New System.EventHandler(AddressOf pnlImages_MouseLeave)
                    Left = Left + 135   '74
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Me.Cursor = Cursors.Arrow
    End Sub





在代码块中你会注意到在添加控件之前首先设置要添加的控件的属性。



或者,您可以创建UserControl,根据需要设置属性然后添加它当你需要它时。通过创建UserControl,您可以更好地控制控件的使用方式,属性和事件。如果需要更改布局,属性和事件也更容易。



In the code block you will notice that the properties of the control to be added is set first before the control gets added.

Alternatively, you can create a UserControl, set the properties as needed and then add it as and when you need it. Creating UserControls gives you more control over how the controls are used, their properties and their events. It is also much easier to make changes to the layout, properties and events should it be required.


您正在使用 Controls.Add 基类的,没有意义,因为控件不是虚拟的,不能被覆盖。使用me,它是对实例成员可访问的实例的引用。你知道那些是什么吗?除非你对基础知识非常有信心(而不是朋友告诉我),否则我不建议做UI。通过不花时间学习基础知识,你只是浪费时间......



您没有提供全面的信息。如果 ScrollPanel 是你的控件(从 Control 派生,直接与否;顺便说一下:哪一个?总是表示完整类型名称),你正确添加 pnk kg ,但它们也有尺寸,正确码头(对于 System.Windows.Forms.Control 和后代),依此类推。控件可能不可见......



-SA
You are using Controls.Add of the base class, which makes no sense, because the Controls is not virtual and could not be overridden. Use "me", which is a reference to the instance accessible by instance members. Do you know what are those? I would not advise to do UI unless you are quite confident in basics (instead of "a friend told me"). By not taking decent time for learning basics, you just waste time...

You did not provide comprehensive information. If ScrollPanel is some your control (derived from Control, directly or not; by the way: which one? always indicate full type names), you correctly add pnk and kg, but they also have such properties as Size, Left, Right, Dock (for System.Windows.Forms.Control and the descendants), and so on. The controls might be invisible…

—SA


这篇关于自定义控件中的面板不保持控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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