Results 1 to 3 of 3

Thread: [RESOLVED] [Vb.net] Dinamic new picturebox for each file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Resolved [RESOLVED] [Vb.net] Dinamic new picturebox for each file

    I am trying to create a new picturebox for each file listed by io file existes but the pictureboxes have to follow each new example side by side.

    Code:
     Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo("C:\")
    
     Dim modk = 100
            Dim makd = 100
            For Each file As System.IO.FileInfo In dir.GetFiles()
                Dim iconForFile As Icon = SystemIcons.WinLogo
                item = New ListViewItem(file.Name, 2)
                NewPictureBox.Parent = Me
                NewPictureBox.BackColor = Color.Red
                modk += 20
                makd += 20
                NewPictureBox.Location = New Point(modk, makd)
                Me.Controls.Add(NewPictureBox)
    Next
    Resultados de tradução
    the purpose of the above code is to create picturebox side by side for each file in the directory I'm trying to create a table companion menu for my program and I've never done anything like that.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [Vb.net] Dinamic new picturebox for each file

    Firstly, why are you creating a ListViewItem and not using it?

    As for the issue, think about what the code you have actually does. You start out with the coordinates (100,100) and, each time you create a PictureBox, you increase both coordinate values by 20, so your PictureBoxes will be located at (120,120), (140,140), (160,160) and so on. Is that actually what you want? I would think not, so don't do that. As you should ALWAYS do, work out the logic first, then write code to implement the logic. If you were doing this manually, e.g. hanging pictures on a wall, you would start with the location of the first picture. You would hang the picture at that location and then you would move horizontally, i.e. increase the X coordinate value, to the location of the next picture. The Y value would remain the same if all pictures were to be side-by-side. Now that you understand the logic, you can write code that actually implements that logic. If the code doesn't work as expected, you've always got something to compare it to to see why. If that requires you to pick up a pen and paper and actually write down the logic first then do that. Don't shirk the work and then wonder why the code doesn't do what you expect.

    Having said all that, you generally shouldn't be placing controls manually like that. Depending on how you want overflow to work, you may find that a TableLayoutPanel or FlowLayoutPanel will do a better job. Once they're configured, you just add the control and the layout occurs automatically.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Re: [Vb.net] Dinamic new picturebox for each file

    Quote Originally Posted by jmcilhinney View Post
    Firstly, why are you creating a ListViewItem and not using it?

    As for the issue, think about what the code you have actually does. You start out with the coordinates (100,100) and, each time you create a PictureBox, you increase both coordinate values by 20, so your PictureBoxes will be located at (120,120), (140,140), (160,160) and so on. Is that actually what you want? I would think not, so don't do that. As you should ALWAYS do, work out the logic first, then write code to implement the logic. If you were doing this manually, e.g. hanging pictures on a wall, you would start with the location of the first picture. You would hang the picture at that location and then you would move horizontally, i.e. increase the X coordinate value, to the location of the next picture. The Y value would remain the same if all pictures were to be side-by-side. Now that you understand the logic, you can write code that actually implements that logic. If the code doesn't work as expected, you've always got something to compare it to to see why. If that requires you to pick up a pen and paper and actually write down the logic first then do that. Don't shirk the work and then wonder why the code doesn't do what you expect.

    Having said all that, you generally shouldn't be placing controls manually like that. Depending on how you want overflow to work, you may find that a TableLayoutPanel or FlowLayoutPanel will do a better job. Once they're configured, you just add the control and the layout occurs automatically.
    thanks for the tips I usually do this only in electronics to create the mini circuit diagrams, but in this case I wanted to picturebox even to list images with their own as a photo mural.

Tags for this Thread

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