Results 1 to 10 of 10

Thread: [RESOLVED] Howto add Controls at runtime with for next

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    43

    Resolved [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.

  2. #2
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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:
    1. Dim a as Integer
    2. For a= 1 to CountedFiles
    3.      Dim picMini As New PictureBox()
    4.      picMini.ImageLocation = strJPGname
    5.      picMini.Location = new Point (X,Y) 'where x and y are the coordinates - also important.
    6.      Panel1.Controls.Add(picMini)
    7. 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:
    1. Dim a as Integer
    2. Dim picMiniArray(CountedFiles) As Integer
    3. For a= 1 to CountedFiles
    4.      Dim picMini As New PictureBox()
    5.      picMini.ImageLocation = strJPGname
    6.      picMini.Location = new Point (X,Y) 'where x and y are the coordinates - also important.
    7.      picMiniArray(a) = picMini
    8.      Panel1.Controls.Add(picMini)
    9. Next a
    10.  
    11. 'Then in code they can be accessed via:
    12. picMiniArray(4).Image = whatever 'just an example.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    43

    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.

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    43

    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 .............

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    43

    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 ................

  6. #6
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    43

    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?

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    43

    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 .............

  9. #9
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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:
    1. Dim a as Integer
    2. For a= 1 to CountedFiles
    3.      Dim picMini As New PictureBox()
    4.      picMini.ImageLocation = strJPGname
    5.      picMini.Location = new Point (X,Y) 'where x and y are the coordinates - also important.
    6.       AddHandler picMini.Click, AddressOf PicMiniHandler
    7.      Panel1.Controls.Add(picMini)
    8. Next a
    9.  
    10. 'And you need a handler sub with the appropriate signature.
    11.  
    12. Private Sub PicMiniHandler(ByVal Sender As Object, ByVal e As EventArgs)
    13.         MessageBox.Show((DirectCast(Sender, PictureBox).Name))'For example.
    14. End Sub

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    43

    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
  •  



Click Here to Expand Forum to Full Width