none
Saving and Loading locations of dynamically created controls at runtime ? RRS feed

  • Question

  • How to save and then load location of controls (in my case : picture boxes) which are dynamically created by user (via clicking mouse button in different locations on the form) at the run time ?

    Monday, April 22, 2019 5:43 PM

Answers

  • The basics is to create a class that represents details about a PictureBox e.g. (In this case ImageFromFile would be the location of an image on disk or change it to something else)

    Imports System.Drawing
    
    <Serializable>
    Public Class PictureBoxItem
        Public Property Location() As Point
        Public Property Point() As Point
        Public Property Name() As String
        Public Property ImageFileName() As String
    
    End Class
    
    

    Iterate all PictureBox controls on form closing, add them to a List(Of PictureBoxItem) then serialize them then in form load iterate the items and recreate as per the following article with code.

    Adding PictureBoxes to a List(Of PictureBoxItem) should be done at the time a PictureBox is created as you mentioned.

    Don't have time to write up a demo, sorry.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Kevin993 Wednesday, April 24, 2019 9:52 PM
    Monday, April 22, 2019 6:29 PM
    Moderator

All replies

  • The basics is to create a class that represents details about a PictureBox e.g. (In this case ImageFromFile would be the location of an image on disk or change it to something else)

    Imports System.Drawing
    
    <Serializable>
    Public Class PictureBoxItem
        Public Property Location() As Point
        Public Property Point() As Point
        Public Property Name() As String
        Public Property ImageFileName() As String
    
    End Class
    
    

    Iterate all PictureBox controls on form closing, add them to a List(Of PictureBoxItem) then serialize them then in form load iterate the items and recreate as per the following article with code.

    Adding PictureBoxes to a List(Of PictureBoxItem) should be done at the time a PictureBox is created as you mentioned.

    Don't have time to write up a demo, sorry.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Kevin993 Wednesday, April 24, 2019 9:52 PM
    Monday, April 22, 2019 6:29 PM
    Moderator
  • Hi,

    Do you want to click on the form to generate controls at the click location?try the code:

      Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles MyBase.MouseClick
            Me.Controls.Add(New PictureBox With {.Location = e.Location, .BackColor = Color.Red})
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 24, 2019 6:04 AM
  • The basics is to create a class that represents details about a PictureBox e.g. (In this case ImageFromFile would be the location of an image on disk or change it to something else)

    Imports System.Drawing
    
    <Serializable>
    Public Class PictureBoxItem
        Public Property Location() As Point
        Public Property Point() As Point
        Public Property Name() As String
        Public Property ImageFileName() As String
    
    End Class

    Iterate all PictureBox controls on form closing, add them to a List(Of PictureBoxItem) then serialize them then in form load iterate the items and recreate as per the following article with code.

    Adding PictureBoxes to a List(Of PictureBoxItem) should be done at the time a PictureBox is created as you mentioned.

    Don't have time to write up a demo, sorry.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    At first, I was completely unfamiliar with the serializing stuff. Although it was difficult to find out how it works , I checked the link you provided me with and I did it!

    Thanks for guiding me Kareninstructor



    • Edited by Kevin993 Wednesday, April 24, 2019 9:56 PM
    Wednesday, April 24, 2019 9:55 PM