Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Controls 1

Status
Not open for further replies.

MartinCroft

Programmer
Jun 12, 2003
151
GB
Hi

Ive added several dynamic controls to a form via a button
which creates for example combox1, textbox1 on click then combobox2 textbox2 happy as can be however I want to add another button which will remove the last set of objects created

Creating as thus

Dim cb As New ComboBox
cb.Name ="ComboBox" & ipos
'// Additional code her
me.panel1.controls.add(cb)

when trying to remove trying in another button

Dim cb As New ComboBox

cb.name ="Combox" & ipos
Me.Panel1.Controls.Remove(cb)

and i get nothing, I did have it semi working at one point in that it was removing controls just not the one i wanted

how to I use the dynamical created crontrols once created
ie. if ive created textbox2 how do i change elements of textbox1 so I can remove the control



 
you are instantiating the combobox twice. the dim cb as new combobox should be a general declaration and it should only be done ones.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Take a look at this i made for you:

Code:
[b]    Public x As New ComboBox[/b]

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        x.Name = "cbo1"
        x.Location = New Drawing.Point(10, 10)

        Me.Controls.Add(x)
        x.Show()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Controls.Remove(x)
'  or Me.Controls.RemoveAt(2)    ***
    End Sub


*** Suppose that i have two buttons on the form.controls; Button1 index 0 and Button2 index 1. So the added combobox will have index 2
 
Also Chris is right! You referenced and created the cobmobox once (keyword NEW). You attempt to create a new same with the same name so as to delete it. The dynamically created control now exists in the collection... so use its name (or index) to handle with it.
 
Hi In the first button I instantiate the object
using

Dim Tb As New TextBox
and create the object

everytime i click the button it creates a new textbox

if I declare the following line globally
Dim Tb As New TextBox

taking it out of the button it creates the object as previous but when clicking the button again a new text box aprears but the previous one disapears

the creation works fine, im just not able to access that control again
 
It migth be worth mentioning I want a new textbox on each button click and to keep the ones already created
 
Here is some code that does what you want. You will need to play with it a little to get the controls placed properly, etc.

Code:
'Form-level global
Dim ipos As Long = 0

'handler for button named "AddControls"
Private Sub AddControls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddControls.Click
        Dim cb As New ComboBox
        cb.Name = "ComboBox" & ipos
        '// Additional code here
        Me.Panel1.Controls.Add(cb)
        cb.Left = 0
        cb.Top = 10
        Dim btn As New Button
        btn.Name = "Button" & ipos
        Me.Panel1.Controls.Add(btn)
        btn.Left = cb.Left + cb.Width + 50
        btn.Top = cb.Top
        AddHandler btn.Click, AddressOf RemoveControls
        ipos = ipos + 1
    End Sub

    Private Sub RemoveControls(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim ComboName As String

        'Get name of ComboBox associated with this button
        ComboName = "ComboBox" & Mid(sender.name, Len(sender.name))

        'loop through panel controls and remove ComboBox
        For i As Integer = Panel1.Controls.Count - 1 To 0 Step -1
            If Panel1.Controls(i).name = ComboName Then
                Panel1.Controls.RemoveAt(i)
            End If
        Next

        'loop through panel controls and remove Button
        For i As Integer = Panel1.Controls.Count - 1 To 0 Step -1
            If Panel1.Controls(i).name = sender.name Then
                Panel1.Controls.RemoveAt(i)
            End If
        Next
    End Sub

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Thanks everyone for your help

jebenson responce was exactly what i was looking for
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top