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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rediscovering control arrays (runtime)

Status
Not open for further replies.

falled

Programmer
Nov 3, 2006
47
ES
I'm newbie in VB .NET.

I'm working in a Software Company developing applications for stores and today, I've encountered my first real challenge, How to create runtime controls????

I come from flash and it's very easy to create this kind of arrays

createEmptyMovieClip("name",...)

But if I wanna create an indeterminate number of controls, and this number I only can read it in run time mode??

I've spend all this day surfing on the internet looking for any answer, but unfortunatelly I've founded nothing. But suddenly, after trying many combinations I found This:

//Author: Xavier Colomer Pages
//Ncatdesigner@hotmail.com
Dim nRows As Integer
nRows = 5
Dim count As Integer
Dim Array(nRows) As Object
For count = 1 To nRows Step 1
Array(count) = New Windows.Forms.Button
Array(count).Name = "Button" & count
Array(count).Top = 100
Array(count).Text = "Button" & count
Array(count).BackColor = Color.Green
Array(count).width = 100
Array(count).Height = 100
Array(count).Left = 100 + (Array(count).Width + 10) * count
Me.Controls.Add(Array(count))
Next count

I think it's really easy now.

Thank you all for trying to help me and as I hope this little code can help other people to get his desired results coding his apps.

Read you soon sorry for my bad writting!

--Work a little, enjoy a lot, live forever, die never
 
Yes, it's really easy to add controls dynamically. But wiring up the events to the dynamic buttons is more hassle.
[ol][li]Create a class module, this example is for checkboxes. Note the WithEvents...
Code:
Private WithEvents myCheckBox As MSForms.CheckBox
Private myForm As ColumnUI

Private Sub Class_Initialize()
    Set myCheckBox = Nothing
    Set myForm = Nothing
End Sub

Public Sub Init(cb As MSForms.CheckBox, frm As ColumnUI)

    'set references to checkbox and owning form
    
    Set myCheckBox = cb
    Set myForm = frm
End Sub

Private Sub myCheckBox_Change()

    'update the model
    
    paths = Split(myCheckBox.Name, "~", , vbTextCompare)
    myForm.setValues paths(0), paths(1), myCheckBox
End Sub

Private Sub Class_Terminate()
    Set myCheckBox = Nothing
    Set myForm = Nothing
End Sub
[/li][li]Create a collection in your form to hold instances of your class module
Code:
Private cbColl As Collection
[/li][li]As you add the items to the form, create an instance of your class module for each, and add them to the collection
Code:
For Each j In myRanges(i).Keys
        
            'add new checkbox
        
            Dim myCheckBox As MSForms.CheckBox
            Set myCheckBox = myPage.Controls.Add("Forms.CheckBox.1", i & "~" & j)
            myCheckBox.Top = ypos
            myCheckBox.Left = 10
            myCheckBox.Caption = j
            myCheckBox.Value = myRanges.Item(i).Item(j)
            ypos = ypos + 20
            
            'adjust scrolling if we have filled the page
            
            If ypos > MultiPage1.Height Then
                myPage.ScrollHeight = ypos + 20
                myPage.ScrollBars = fmScrollBarsVertical
                myPage.KeepScrollBarsVisible = fmScrollBarsVertical
            End If
            
            'connect the handler for this checkbox
            
            Dim myHandler As CheckBoxListener
            Set myHandler = New CheckBoxListener
            myHandler.Init myCheckBox, Me
            cbColl.Add myHandler
        Next
[/li][li]Create a callback method on your form to process the events captured by any of the class module instances
Code:
Sub setValues(r, i, v As MSForms.CheckBox)

    'callback from the check box handlers
    
    myRanges.Item(r).Item(i) = v.Value
End Sub
[/li][/ol]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
You may want to check if you are in the correct forum - you mention VB .NET but this is the VBA forum....

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top