Creating and changing a Panel object at run time

paulthepaddy

Well-known member
Joined
Apr 9, 2011
Messages
222
Location
UK
Programming Experience
Beginner
hello guys.

unfortuatly i havn't got any test code for this, as i dont know what way i am going to approach this yet.

at the moment i have a groupbox on a form with 3 radio buttons.

what i would like to happen is when a radio button is selected for it to display a panel with a few controls on it.

i am looking to know what is the best way to code this, i have read a few guides on creating objects at runtime, but none of which i have looked at have any information on adding objects to the likes of a panel.

i am thinking that their are probbably 2 way to attempt this.

my logical thinking tells me i should either

1) Create a class will contain the controls and subs that i need and then try to load the class when it is needed.
2)create a control array then try and work with it that way.

i personally think number 1 is the best option, but i cant figure out how to do this.

althought i know i am asking for code more so that help, if someone could give me the code for creating the panel and creating a label IN the panel, im sure i could then have a go at doing the rest myself.

i know im asking a lot, but any help will be much appreciated.
 
VB.NET:
Public Class Form1
    Dim _Panel As New Panel

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim _TextBox As New TextBox
        _Panel.Controls.Add(_TextBox)
    End Sub

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.Controls.Add(_Panel)
    End Sub
End Class
 
thank you, exactly what i wanted, the part that was confusing me was the

Dim _TextBox As New TextBox
_Panel.Controls.Add(_TextBox)
i new how to create the panel but it was creating the item into the panel i got confused on... it is quite simple lol, i should really have figured that out.

thanks and plus REP
 
sorry another pain in the ass question, again sory for not googling this and reading up, but u seem to know what im on about,a nd iv alot of coding to do, so i can wait for the answer while im busy :D

after i have created a button to the form, how do i create a sub routin and add handles to the botton lol, could u add in all the code please so i dont get confused lol

VB.NET:
[COLOR=#00ff00]'The code that creates the panel and the extra controls[/COLOR]
Public Class CustomPanels
    Inherits Windows.Forms.Panel
    Dim BTN_Action As New System.Windows.Forms.Button

    Public Function NewDamageArea() As System.Windows.Forms.Panel
        For Each item In Me.Controls
            Me.Controls.Remove(CType(item, Control))
        Next
       
        'Button1
        Me.BTN_Action.Location = New System.Drawing.Point(250, 170)
        Me.BTN_Action.Name = "BTN_Action"
        Me.BTN_Action.Size = New System.Drawing.Size(150, 23)
        Me.BTN_Action.TabIndex = 0
        Me.BTN_Action.Text = "Add Damage Area"
        Me.BTN_Action.UseVisualStyleBackColor = True

        'Panel_DamageArea
        Me.Controls.Add(Me.BTN_Action)
        Me.Location = New System.Drawing.Point(12, 51)
        Me.Name = "Panel_DamageArea"
        Me.Size = New System.Drawing.Size(487, 308)
        Me.TabIndex = 0
        Return Me
    End Function
End Class

[COLOR=#00ff00]'how i am using the code[/COLOR]
Private Sub RB_DamageArea_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RB_DamageArea.CheckedChanged
        If Me.RB_DamageArea.Checked Then
            Me.Controls.Add(CP.NewDamageArea)
        Else
            Exit Sub
        End If
    End Sub

for me this works, but if you think this is bad practice then please advise better way to use.

many many thanks
 
VB.NET:
Public Class Form1
    Dim _Panel As New Panel

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.Controls.Add(_Panel)
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
        Dim _Button As New NewButton
        _Button.Text = "Hello"

        If CheckBox1.Checked Then
            _Panel.Controls.Add(_Button)
        End If
    End Sub
End Class


'This is the class that handles the button
Friend Class NewButton
    Inherits Button
   
    Private Sub NewButton_Click(sender As Object, e As System.EventArgs) Handles Me.Click
        MsgBox("Hello World!")
    End Sub
End Class

Or you could keep what you have and make this change...

VB.NET:
Public Class CustomPanels
    Inherits Windows.Forms.Panel
    Friend WithEvents BTN_Action As New System.Windows.Forms.Button      '<--- Changed

    'ADDED
    Private Sub BTN_Action_Click(sender As Object, e As System.EventArgs) Handles BTN_Action.Click
        MsgBox("Hello World!")
    End Sub
  
    Public Function NewDamageArea() As System.Windows.Forms.Panel
        For Each item In Me.Controls
            Me.Controls.Remove(CType(item, Control))
        Next
       
        'Button1
        Me.BTN_Action.Location = New System.Drawing.Point(250, 170)
        Me.BTN_Action.Name = "BTN_Action"
        Me.BTN_Action.Size = New System.Drawing.Size(150, 23)
        Me.BTN_Action.TabIndex = 0
        Me.BTN_Action.Text = "Add Damage Area"
        Me.BTN_Action.UseVisualStyleBackColor = True

        'Panel_DamageArea
        Me.Controls.Add(Me.BTN_Action)
        Me.Location = New System.Drawing.Point(12, 51)
        Me.Name = "Panel_DamageArea"
        Me.Size = New System.Drawing.Size(487, 308)
        Me.TabIndex = 0
        Return Me
    End Function
End Class

Keeping the events inside the panel class
 
Last edited:
hi, thanks for the reply, i have decided to go with keeping the event inside the class, as depending on the radio button will depend on what the buttons do, so this would make i easier for me to code, read,edit and dedug.

i was also wondering is it possble to add events and handlers to a class, for example
i hvae a class called car and it will be listed in quite a few diffrent lists, listbox, checked list box.

would it be possble for me to add to code the car class that would be executed every time the item is double clicked while it is in the diffrent listing methods or ill i have to code them to the listing methods on each form?
 
Hi there,

Unfortunately I'm in the learning stages of custom events, handlers, and arguments myself. You've reach the extent of my knowledge on the subject. One of the mods, jmcilhinney, has a great blog about creating custom events that is worth a look...

John McIlhinney's .NET Developer Blog: Defining and Raising Custom Events

Give that a read and hopefully it helps.
 
ah thank you for the link, as i didn't know the proper name of this it was a bit hard for em to find. can i ask how you are learning, i doubt their is classes that you can go to that will be able to teach u most of the things as i would say a programming language is VERY large. can i ask what resources you use, i am still stuck for finding a GOOD site that has lots of diffrent tutorials for VB rather than the lots of sites that do very simple stuff oin a long and drawn out way lol, agian thanks for replying, and will rep :D
 
Actually this forum has been a huge learning tool for me. I've never taken any formal classes but I plan to in the future. I pretty much just learn by doing, if I think of something that seems cool or a challenge I'll do my best to learn by examples and tutorials. Also you can find a lot of great tutorials and examples on MSDN. Just Google "msdn vb.net events" for example. Just checking out threads here in general, even if it doesn't apply to you, can lead to ideas of doing things a different way.

CodeProject® - Your Development Resource is another great place for tutorials and general learning.

take care and good luck.
 

Latest posts

Back
Top