Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,
I've created a class in VB.Net, it inherits panel class, so I'll use it as a control in my application. How can I catch "when this control is created" as event?
I tried resized event defining a boolean that is false at the first time, and becomes true at first resize event, so will occur once, so I tought it would call resize event when it is created by designer, but it didn't. Here is my code:
VB
Public Class ScrollPanel
    Inherits Panel
    Dim pnl As New Panel With {.Location = New Point(0, 0), .Size = New Size(Me.Width - 10, Me.Height), .BackColor = Color.White}
    Dim kgOk As Boolean = False

    Private Sub SC() Handles MyBase.SizeChanged
        If Not kgOk Then
            Controls.Add(pnl)
            kgOk = True
        End If
    End Sub
End Class
Posted
Comments
Sergey Alexandrovich Kryukov 10-Jul-14 12:19pm    
To start with, tag your UI library and/or application type. For example, what is "Panel"? There are different unrelated classes under this name. Full type name, please.
—SA
MuhsinFatih 10-Jul-14 14:19pm    
I mean the panel in the toolbox of VB.Net (Windows desktop application). Maciej Los' solution is exactly what I wanted. Thanks

1 solution

Not sure what you want, but it seems like you want to use constructor.

Class definition:
VB
Public Class ScrollPanel
    Inherits Panel

    Private sText As String = "New Panel"

    Public Sub New()
        MsgBox(String.Concat(sText, " has been created!"), MsgBoxStyle.Information, "Message")
    End Sub



    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
End Class


Usage:
VB
Dim sp As ScrollPanel = New ScrollPanel With {.Parent = Me, .Size = New Size(100, 100), .Location = New Point(80, 80), .Visible = True}


More:
Using Constructors and Destructors[^]
 
Share this answer
 
Comments
MuhsinFatih 10-Jul-14 14:20pm    
This is exactly what I wanted. Thanks!
Maciej Los 10-Jul-14 14:54pm    
You're very welcome ;)
Maciej Los 10-Jul-14 15:00pm    
BTW: To add new control to your ScrollPanel, use: MyBase.Controls.Add(control). You don't need to create private object pnl inside your class.
MuhsinFatih 10-Jul-14 15:18pm    
The pnl panel is a part of my control, so I use it in main events of scrollpanel. So I think it would be easier to define it in class. But I add the other controls which can change in time like you've showed to me. Thank you
Maciej Los 10-Jul-14 15:27pm    
Whenever you want to manage ScollPanel's properties, events and methods, inherited from base class, use MyBase. If you define custom property, use its name.
It might be interesting too: Add Custom Event to a Class in VB.NET[^]
Happy codng!
You're welcome ;)

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



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