Answered random amount of objects

andyvl

Member
Joined
May 14, 2009
Messages
14
Programming Experience
Beginner
I want to cycle through a arraylist and for each item in that list
I need to create a

picturebox, label1, label2

each with specific name attributes

when I click on the picturebox (maybe I do it with buttons) a list of actions need to be done for that element. I think this is best done by checking the name of the picturebox/button that is clicked.


But I don't have any idea how to create the 'dynamic' form.

My guess will be to

create the box hard coded, set the attributes, set the position (best with a integer variable I think so I can raise it in the loop) and set visible.

but if this is the best way, I really don't have any idea how to define the button that's clicked and go to the correct function.


(don't know if i'm clear on what I want, but any help would be appreciated)
 
Last edited:
Are you actually wanting to create a new form for each item in the arraylist? What is it that you are actually trying to achieve?
 
No,

I make a request on a form.
then I'm going to look for possible matches in a text file.
If it's a possible match I create a custom object.
All possible matches are stored in an arraylist.
(that part is coded)

What I want to do now is that I cycle through that arraylist.
(arraylist of object X.
Object X containes an imageURL, and some data (can be displayed in 3 labels)

So I want to go through the arraylist and create

picturebox (with a specific name), label with info, label with info (for 1st araylist item)
picturebox (with a specific name), label with info, label with info (for 2nd araylist item)
...

on the same form. But also an action (with parameter) needs to be specified for when I click on a picturebox.

so when I click on picturebox1 I need to doAction(paramter1)
when I click on picturebox2 I need to doAction(parameter2)
...
when I click on picturebox n I need to doAction(paramtern)
(doAction is to retrieve info from a website.
the website depends on the picturebox I click.



(I hope it's a bit more clear what I try to say. English isn't my native tongue)
 
ok, I think I'm getting there :)

sometimes is logical thinking a great sollution :)

Not sure if it's going to work at the end, but I'm half way. (my labels are filled. Now just working with my image :)

what I did is
VB.NET:
       For Each tmpFilm In Resultaten.ResultFilmLijst
            'create Labels
            Dim tmpLString As String = "Llabel" & Convert.ToString(tmpCounter)
            Dim tmpRString As String = "Rlabel" & Convert.ToString(tmpCounter)
            Dim labelL As Label = New System.Windows.Forms.Label
            Dim labelR As Label = New System.Windows.Forms.Label

            'set properties of labels
            labelL.Name = tmpLString
            labelR.Name = tmpRString
            labelL.Location = New System.Drawing.Point(Xposition1, Yposition)
            labelR.Location = New System.Drawing.Point(xposition2, Yposition)
            labelL.Text = tmpFilm.FilmNaam
            labelR.Text = tmpFilm.FilmReleaseJaar

            Yposition += 24
            tmpCounter += 1

            'in panel zetten
            Me.pnlResult.Controls.Add(labelL)
            Me.pnlResult.Controls.Add(labelR)

        Next
 
Halfway

ok, I'm halfway. all my components are created.

VB.NET:
        For Each tmpFilm In Resultaten.ResultFilmLijst
            'create componenten
            Dim tmpLString As String = "Llabel" & Convert.ToString(tmpCounter)
            Dim tmpRString As String = "Rlabel" & Convert.ToString(tmpCounter)
            Dim tmpPctString As String = "pcBox" & Convert.ToString(tmpCounter)
            Dim labelL As Label = New System.Windows.Forms.Label
            Dim labelR As Label = New System.Windows.Forms.Label
            Dim pctbox As PictureBox = New System.Windows.Forms.PictureBox
            Dim btnAdd As Button = New System.Windows.Forms.Button

            'set name 
            labelL.Name = tmpLString
            labelR.Name = tmpRString
            pctbox.Name = tmpFilm.FilmUrl
            btnAdd.Name = tmpFilm.FilmUrl

            'set location 
            labelL.Location = New System.Drawing.Point(Xposition1, Yposition)
            labelR.Location = New System.Drawing.Point(xposition2, Yposition)
            pctbox.Location = New System.Drawing.Point(Xposition1 - 30, Yposition - 5)

            'set font
            labelL.Font = New System.Drawing.Font("Microsoft Sans Serif", 10, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            labelR.Font = New System.Drawing.Font("Microsoft Sans Serif", 10, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

            'set content
            labelL.Text = Convert.ToString(tmpFilm.FilmNaam)
            labelR.Text = tmpFilm.FilmReleaseJaar
            LoadImageFromUrl(tmpFilm.FilmThumbnail, pctbox)

            'set size
            labelL.Size = New System.Drawing.Size(208, 16)
            labelR.Size = New System.Drawing.Size(208, 16)
            pctbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage
            pctbox.Size = New System.Drawing.Size(30, 30)


            'in panel zetten
            Me.pnlResult.Controls.Add(labelL)
            Me.pnlResult.Controls.Add(labelR)
            Me.pnlResult.Controls.Add(pctbox)


            'count up the counter for position and the counter for the names
            Yposition += alterYposition
            tmpCounter += 1

        Next

But I don't manage to get an eventhandler on my pctbox.

Every image stands for a particular movie.

Can somebody please help me since this is a crucial step in my application
 
AddHandler MyDynamicPicture.EventYouWantToCatch, AddressOf ASubWithTheCorrectSignature

The handler will then handle the event of any picture you created (if you have added it to all of them of course), so you need to check "sender" which image fired the event. If you name your PBoxes accordingly it should be easy.
 
Back
Top