Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
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:
VB
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
Posted
Comments
Sergey Alexandrovich Kryukov 10-Jul-14 17:37pm    
What, are they sliding down off the panel? :-)
What is "MyBase"?
—SA
MuhsinFatih 10-Jul-14 19:31pm    
A friend told me to use mybase, i may have misunderstood him :) I also used "me" too. Pnl is the panel to be sliding. Another control is sliding it

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.

VB
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


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.
 
Share this answer
 
Comments
MuhsinFatih 12-Jul-14 16:29pm    
Actually, I'm trying to make a panel that has a different vertical scroll control. Becouse '.Net' s scroll is too big for some applications, also not pretty. I know I can make it by coding for each panel I use, or I can also make a function that automatically adds a panel. I already use it. But I wanted to be able to add my panel by a drag-drop from toolbox :). It's almost all working correctly. Basically, there's a panel inside of a parent panel. Panel holds all the controls that user may add. Parent panel holds the panel I've told, and a control which scrolls the panel inside(this control is 'kg' in my questions code block). Reason of adding all controls in a panel inside is, when I tried a for loop that moves all the controls by scrolls move, it worked too slowly and lagged the computer becouse of too much process(when panel has hundreds of controls). Anyway the problem is, when I add a control in panel via designer, it's correctly added. But when I build application, weirdly the control goes to behind of the panel, then control's parent is changed to the panel that holds the other panel.
Here you can see, before build:
https://www.dropbox.com/s/9fl4nnt3kbd2cq0/before%20build.PNG
After build:
https://www.dropbox.com/s/5dj9uvqdnlnccd1/after%20build.PNG
How can I prevent this happen?
Thanks for helping.
Tino Fourie 12-Jul-14 16:54pm    
I have been faced with exactly the same problem while designing Forms and I have found that the IDE screws it up somehow. The bad news is, there is no way to fix this and you will need to redesign / recreate the form from scratch. I have lost hours of coding time because of this anomaly in VS and it has driven me to the brink of tears.

It is not worth your time to try and fix it, redesign / recreate the form using a new form. Do not reuse the exiting form.
To test this, create a new form and see if you can replicate the same problem you are facing now. You might be pleasantly surprised.

Edit:
For improved performance, use a BackGroundWorker to place controls on the Panel control. This will prevent your application from "locking up" while it adds the controls or cause your application to lag.

Hope that helps you.
Tino Fourie 12-Jul-14 17:15pm    
I just remembered something else I tried but did not work for me. Look at this MSDN link where you can send a control to the "back" or bring it to the "front". This really means that you are either going to "move" a control behind others or place it over existing controls.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.bringtofront(v=vs.110).aspx[^]
MuhsinFatih 14-Jul-14 21:35pm    
I'm sorry I'm late in responding, I was really too busy novadays :). Another problem had occured about Visual Studio designer before to me. As you told, I could be able to fix it by recreating project. But this time, it didn't work :(. Even if "bringtofront" works, their parent is still the base panel. It must be the inside panel. Anyway, thanks for helping. It looks like I'll have to continue adding my controls via code.
I would be pleased if you can check this out, maybe you can see a mistake of me. This is the library project:
https://www.dropbox.com/sh/q3njlrmro3ekter/AAAT5JUoiHAzc3GpPGXyEvNla
Note that you can find something ridiculous becouse of I tried lots of changes in code :)
Thanks for helping. Have a nice day :)
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
 
Share this answer
 
v2
Comments
MuhsinFatih 12-Jul-14 16:39pm    
Thanks for telling me how to use a forum :P Do you get all points like this? This is not a helpful comment. You only underestimate me. If you know something, try help me, in a forum, what you're supposed to do is this. You could tell me what you didn't understand, don't tell me question is incomplete. I'm not a beginner, I've been programming for 5 years so I know basics.. Try understand question before writing a comment.
Sergey Alexandrovich Kryukov 12-Jul-14 22:58pm    
Even if I was confused about your experience (mistakes happen, and everyone here knows that I'm always ready to admit mine), this is because your writing creates strong impression of a very inexperienced one, such as "a friend told me to use mybase". Even if this is only an impression, I had no signs which would stimulate me to talk to you as with an experienced one. "I'm not a beginner" is told not by claiming, and not even by years of experience (I met some developers of 25 years of programming experience who still have no clue how to do programming, believe or not), but by real understanding.

Again, I apologize if I made a mistake about your experience.

Do you really think that an advice on proper use of "me" is not useful? Then I cannot be sure that you really expect help, sorry...
And sorry, if I cannot understand question, it certainly means that the question is incomplete, otherwise I would clearly understand the question on such a simple topic. Do you want to argue over incomplete the question, or want to make it more complete to actually get help? — that is the question. :-)

—SA
MuhsinFatih 12-Jul-14 16:42pm    
If you still want to understand my question, read the comment below Tino Fourie's solution, I'm thankfully to him becouse he tries to help, unlike you.
Sergey Alexandrovich Kryukov 12-Jul-14 23:02pm    
Oh, I see what's going on...
Sorry, I'm not ready to continue this kind of conversation.
—SA
MuhsinFatih 13-Jul-14 6:43am    
Actually, I've written the comment which starts with "If you still want to understand my question" without seeing your answer, so please don't misunderstand me. I've tried using "me" instead of "mybase"(I already use "me" normally, but it did not make any sense. You can see what I mean by "a friend told me" here: http://www.codeproject.com/Questions/795429/Control-Created-event?arn=0).
I did not say I'm a professional, I just said I'm not a beginner, and this is a simple question about a problem of Visual Studio's designer. Doesn't have to have complex code or professionality. I've just written a simple class and I don't think I have to read books to learn everything about classes just to make a simple scroll panel. I've asked another question just a few days ago, same thing happened, you asked me what I exactly mean by panel, but another man told me a possible solution, it helped. Just like what here happened. I think you should try to think, "what he would be talking about by saying this?.." instead of "This is incomplete, what panel, what class etc.". Seriously, this is a friendly advice, not dissing.
Being met to some developers of 25 years of experience and they still can't program doesn't mean you can underestimate and think everyone as the same, and people doesn't have to be experienced to be able to get help of you. This is ridiculous. Why do I ask such a simple question if I'm a professional?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900