|
-
Apr 8th, 2006, 01:51 PM
#1
Thread Starter
Member
[RESOLVED] Howto add Controls at runtime with for next
I wondered if it is possible to add Controls on my form at run-time, but the number of Controls various through 1 to 144. Loading some filenames in a listview, then I count the items. The number of items counted, so much Controls I wanna add in my Panel Control.
I know the code beneath is wrong, but how can it be done?
Dim a as Integer
For a= 1 to CountedFiles
Dim picMini & string(a) As New PictureBox()
picMini & string(a).ImageLocation = strJPGname
Panel1.Controls.Add(picMini & string(a)
Next a
Please let me know a good solution. Thanx in advance.
-
Apr 8th, 2006, 03:28 PM
#2
Re: Howto add Controls at runtime with for next
You've almost got it, except you can't concatenate the name of the control.
VB Code:
Dim a as Integer
For a= 1 to CountedFiles
Dim picMini As New PictureBox()
picMini.ImageLocation = strJPGname
picMini.Location = new Point (X,Y) 'where x and y are the coordinates - also important.
Panel1.Controls.Add(picMini)
Next a
If you want to access them easily, you could consider creating and array of pictureboxes, and since it is a reference type, that array would only contain references to the pictureboxes you create. For example:
VB Code:
Dim a as Integer
Dim picMiniArray(CountedFiles) As Integer
For a= 1 to CountedFiles
Dim picMini As New PictureBox()
picMini.ImageLocation = strJPGname
picMini.Location = new Point (X,Y) 'where x and y are the coordinates - also important.
picMiniArray(a) = picMini
Panel1.Controls.Add(picMini)
Next a
'Then in code they can be accessed via:
picMiniArray(4).Image = whatever 'just an example.
Bill
-
Apr 10th, 2006, 01:42 AM
#3
Thread Starter
Member
Re: Howto add Controls at runtime with for next
I tried to rate you, but could not succeed.
I thank you very much, I helps me a lot.
-
Apr 11th, 2006, 01:19 AM
#4
Thread Starter
Member
Re: Howto add Controls at runtime with for next
I tried the code. Just one minor issue:
an Integer cannot be converted to a Picturebox,
therefore one little adjustment:
Dim picMiniArray(CountedFiles) As New Picturebox()
Enjoy live .............
-
Apr 16th, 2006, 04:16 AM
#5
Thread Starter
Member
Re: Howto add Controls at runtime with for next
A new problem arises. The code above are all in a Private Sub. But then I cannot use pcMiniArray within another Privat Sub. For example clicking on a new added control cannot be used...........? Even a panel_click does not work (because it is full with controls (pictureboxes).
It wonders me how to solve this problem ................
-
Apr 16th, 2006, 05:34 AM
#6
Re: Howto add Controls at runtime with for next
I doubt it has to do with Private or Public or any other access level modifier. Simply put, the panel itself is a control, and has it's own collection within it. You can either iterate through the panel's control collection, or use the GetNextControl method. There is a good example in the VB.NET codebank.
Bill
-
Apr 18th, 2006, 02:38 PM
#7
Thread Starter
Member
Re: Howto add Controls at runtime with for next
I want to know which Picturebox has been mouse clicked. Therefore I must know the mouse location within the Panel. When I know the exact mouse location in the Panel, then I know also which Picturebox has been clicked.
Or could it be done more easier?
-
Apr 18th, 2006, 02:49 PM
#8
Thread Starter
Member
Re: Howto add Controls at runtime with for next
a Panel_MouseClick does not give a Mouse location because a PictureBox Control lays on top of it. Only a Panel_Mouseclick outside the PictureBox gives the right location .............
-
Apr 18th, 2006, 04:11 PM
#9
Re: Howto add Controls at runtime with for next
Since you created the pictureboxes in code, you need to add handlers in code also. For example, using that loop I gave you you might do something like this:
VB Code:
Dim a as Integer
For a= 1 to CountedFiles
Dim picMini As New PictureBox()
picMini.ImageLocation = strJPGname
picMini.Location = new Point (X,Y) 'where x and y are the coordinates - also important.
AddHandler picMini.Click, AddressOf PicMiniHandler
Panel1.Controls.Add(picMini)
Next a
'And you need a handler sub with the appropriate signature.
Private Sub PicMiniHandler(ByVal Sender As Object, ByVal e As EventArgs)
MessageBox.Show((DirectCast(Sender, PictureBox).Name))'For example.
End Sub
Bill
-
Apr 21st, 2006, 02:23 AM
#10
Thread Starter
Member
Re: Howto add Controls at runtime with for next
Thanks a lot. You helped me wonderful. Your knowledge has been of a great support.
Pat.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|