Results 1 to 33 of 33

Thread: visual basic tablelayout panel loaded with an array of picture boxes, view reset

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    visual basic tablelayout panel loaded with an array of picture boxes, view reset

    Being very much a novice I have received some GREAT HELP here in the last week for what I imagine are dumb questions to others. So, I have another one of those questions I hope someone can help me with.

    Based on replies on my previous threads I have a tablelayoutpanel. I load that tableoutpanel with an array of picturebox's. The array of pictureboxes varies in size based on a folder search string determined by a button click event.

    The tableoutpanel has scroll bars that work great - when the number of picturebox's in the array is larger than the size of the tablelayout panel it scrolls perfectly. I have a problem where I don't know how to reset the "view" of the picturebox array so the first row shows when the tablelayoutpanel is reloaded based on a change in the folder search string.

    So, one time the user clicks a button and the loading of the array is larger the than the tablelayoutpanel, they scroll down and click on an image and I process the picturebox click. After that the user might click another button which results in fewer numberr of picturebox's in the array. The tablelayoutpanel seems to be positioned at the original click position instead of back at the first row. I *thinK* I'm changing it to reset but obviously that's not working. Here's a snippet where I load that picturebox array. I thought I was going to accomplish this with a clear of the controls but apparently the tablebox array still remains at it's last size. It seems like somehow I need to redim the picturebox array but I'm not sure how to do that.

    Code:
            Dim filePaths = IO.Directory.GetFiles(myfile_path, "card*.jpg")
    
            maxcount = UBound(filePaths)
    
    
            SELECT_GAME.Text = "Play Card"
            SELECT_GAME.Enabled = True
            SELECT_GAME.Visible = True
    
            TableLayoutPanel1.Controls.Clear()
    
            If maxcount_cardimages > 0 Then
                ReDim CARDIMAGES(maxcount)
            End If
            CARDIMAGES = filePaths
    
    
            If maxcount > 0 Then
                xx = maxcount \ TableLayoutPanel1.ColumnCount
                If (maxcount Mod TableLayoutPanel1.ColumnCount) > 0 Then
                    xx += 1
                End If
                TableLayoutPanel1.RowCount = xx
            End If
    
    
            Do While myfile <= maxcount
                Dim pb As New PictureBox
                pb.Width = 280
                pb.Height = 310
                pb.SizeMode = PictureBoxSizeMode.StretchImage
                pb.ImageLocation = CARDIMAGES(myfile)
                pb.Name = myfile
                AddHandler pb.Click, AddressOf BOX_CLICKED
                TableLayoutPanel1.Controls.Add(pb)
                myfile += 1
            Loop
    Any help would be greatly appreciated. I also hope this novice is using the correct terminology to explain what I am trying to do.
    Last edited by si_the_geek; Nov 1st, 2019 at 04:54 AM.

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

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    I would suggest that you should dispose all the PictureBoxes and reset the number of rows in the TLP to one. That will obviously reset the scroll position. You can then add a new set of PictureBoxes and have the number of rows increase as required.

    I would also suggest that you create all the PictureBoxes and then make a single call to AddRange, rather than multiple calls to Add. Then you really will be adding an array of PictureBoxes to the TLP. Currently, there's no array.

    Finally, you really ought to be using a For loop there.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    Being the extreme novice that I am I think I understand what you are saying so I'm going to look up the syntax for some of that and see if I make another mess or not

    Thanks for the reply!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    Adding the controls in a batch would look something like this:
    vb.net Code:
    1. Dim pictureBoxes As New List(Of PictureBox)
    2.  
    3. For i = 1 To itemCount
    4.     pictureBoxes.Add(New PictureBox With {.Width = 280, ...})
    5. Next
    6.  
    7. myTableLayoutPanel.Controls.AddRange(pictureBoxes.ToArray())
    Clearing the existing controls could be done something like this:
    vb.net Code:
    1. For Each pb As PictureBox In myTableLayoutPanel.Controls.ToArray()
    2.     pb.Dispose()
    3. Next
    Disposing a control automatically removes it from its container. You should then just be able to set the RowCount to 1.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    Can't recall whether type inference and object initialisers are supported in VB 2008 so that code may need to be expanded a bit but hopefully you get the idea.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    Well, I get the error:
    Code:
    Error	1	'ToArray' is not a member of 'System.Windows.Forms.TableLayoutControlCollection'.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	475	38	BINGO
    on the line:
    Code:
            For Each pb As PictureBox In TableLayoutPanel1.Controls.ToArray()
    which in turn generates a lot of errors following that code.

    If I understand things correctly this is trying to make each picture box on the tablelayoutpanel accessable by an index and then deletin it. I probably am thinking wrong, but if you add the array to the tablelayoutpanel wouldn't it be just a single control on the panel and can be deleted just by clearing the controls for the panel with
    Code:
     TableLayoutPanel1.Controls.Clear()
    That is all dependent on whether or not I understand what is going on. Like I've said I'm very much the novice with no training trying to learn something new.

    what I have now is:
    Code:
           If maxcount_cardimages > 0 Then
                ReDim CARDIMAGES(maxcount)
            End If
    
    
            CARDIMAGES = filePaths
    
    
            For Each pb As PictureBox In TableLayoutPanel1.Controls.ToArray()
                pb.Dispose()
            Next
    
            TableLayoutPanel1.RowCount = 1
    
    
    
            Dim pictureBoxes As New List(Of PictureBox)
    
            For nyfile = 0 To maxcount
                pictureboxes.Add(New PictureBox with (.width = 28,_
                                                    .height = 310,_
                                                    .ImageLocation = CARDIMAGES(myfile),_
                                                    SizeMode = pictureboxsizemode.StretchImage,_
                                                   .handler = (pictureBox.click, addreessof box_clicked) ) )
            Next
    
            TableLayoutPanel1.Controls.AddRange(pictureBoxes.ToArray()
    
            TableLayoutPanel1.Enabled = True
            TableLayoutPanel1.Visible = True
    I get creating an array of type list of picturebox and then adding a picturebox to each item in that list with the attributes specified. After that list array is filled then the array is added to the tablelayoutpanel. I really don't undertand how I end up with a table of images on screen the user can click on - that's just me not having enough knowledge on this yet. I mean I get the concept but I've probably messed something all up again.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    Ah crap - seems I got that error on another try but had copied prior to making changes.

    The actual error list for above is:
    Code:
    Error	9	Name 'SizeMode' is not declared.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	489	49	BINGO
    Error	4	Leading '.' or '!' can only appear inside a 'With' statement.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	486	51	BINGO
    Error	5	Leading '.' or '!' can only appear inside a 'With' statement.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	487	49	BINGO
    Error	7	Leading '.' or '!' can only appear inside a 'With' statement.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	488	49	BINGO
    Error	11	Leading '.' or '!' can only appear inside a 'With' statement.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	490	48	BINGO
    Error	6	End of statement expected.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	487	62	BINGO
    Error	8	End of statement expected.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	488	84	BINGO
    Error	10	End of statement expected.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	489	91	BINGO
    Error	2	Class 'System.Windows.Forms.PictureBox' cannot be indexed because it has no default property.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	486	30	BINGO
    Error	1	'ToArray' is not a member of 'System.Windows.Forms.TableLayoutControlCollection'.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	475	38	BINGO
    Error	12	'Public Event Click(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	490	60	BINGO
    Error	3	'{' expected.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	486	50	BINGO
    Error	13	')' expected.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	490	76	BINGO
    Error	14	')' expected.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	493	66	BINGO
    So again I'm probably just doing something stupid. Hope you don't mind trying to help me again.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    This:
    vb.net Code:
    1. For Each pb As PictureBox In myTableLayoutPanel.Controls.ToArray()
    probably ought to have been this:
    vb.net Code:
    1. For Each pb As PictureBox In myTableLayoutPanel.Controls.Cast(Of Control).ToArray()
    As for the other errors, they are almost certainly caused by the fact that you have used parentheses where an object initialiser uses braces. Take a closer look at the first code snippet in post #4.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    Yep - I didn't realize some of those were braces, and the cast cleaned up everything except for the event handler. I'd like all the images to have the same handler since I'll know which image by the row/col. i thought I could do it with:

    Code:
            For myfile = 0 To maxcount
                pictureBoxes.Add(New PictureBox With {.Width = 28, _
                                                      .Height = 310, _
                                                      .ImageLocation = CARDIMAGES(myfile), _
                                                      .SizeMode = PictureBoxSizeMode.StretchImage, _
                                                      .AddHandler = (.Click, AddressOf BOX_CLICKED) } )
            Next
    But it gives me an errorr:

    Code:
    Error	2	')' expected.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	490	72	BINGO
    Error	1	'AddHandler' is not a member of 'System.Windows.Forms.PictureBox'.	C:\Users\davew\Documents\BINGO\BINGO\BINGO\GameStatus.vb	490	52	BINGO
    I assume that if I look at the properties of a picturebox in designer there is "handler". Since the old code used:
    Code:
                AddHandler pb.Click, AddressOf BOX_CLICKED
    After setting the properties for the picturebox I thought I might be able to add it like I tried but to no avail. So, when adding a picturebox to a list, how do you add the handler? I've spend some time searching the net but they all come back to samples like I used first.

    Thanks!

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    An object initialiser can only be used to set properties. If you want to register an event handler then it needs to be done the usual way:
    vb.net Code:
    1. Dim pictureBoxes As New List(Of PictureBox)
    2.  
    3. For i = 1 To itemCount
    4.     Dim pb As New PictureBox With {.Width = 280, ...}
    5.  
    6.     AddHandler pb.Click, AddressOf PictureBoxes_Click
    7.  
    8.     pictureBoxes.Add(pb)
    9. Next
    10.  
    11. myTableLayoutPanel.Controls.AddRange(pictureBoxes.ToArray())

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    Well, it is clean, however, it doesn't do even what it used to do. I still have the scroll bar positioned way down in an empty row, but to top it off none of the images show as before. I at least had images 4 across for as many rows as was needed so the user could just click on an image.

    What the heck am I doing wrong:

    Code:
        Private Sub DO_BY_CARD(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BY_CARD.Click
    
    
            Dim myfile As Integer = 0
    
            Dim myfile_path As String
            myfile_path = INSTALLATION_FOLDER & CARD_IMAGES_PATH
    
            Dim maxcount As Integer = 0
            Dim maxcount_cardimages = 0
    
            Dim xx As Integer = 0
    
            Dim filePaths = IO.Directory.GetFiles(myfile_path, "card*.jpg")
    
            maxcount = UBound(filePaths)
    
    
            SELECT_GAME.Text = "Play Card"
            SELECT_GAME.Enabled = True
            SELECT_GAME.Visible = True
    
    
            If maxcount_cardimages > 0 Then
                ReDim CARDIMAGES(maxcount)
            End If
    
    
            CARDIMAGES = filePaths
    
    
            For Each pb As PictureBox In TableLayoutPanel1.Controls.Cast(Of Control).ToArray()
                pb.Dispose()
            Next
    
            TableLayoutPanel1.RowCount = 1
    
    
    
            Dim pictureBoxes As New List(Of PictureBox)
    
            For myfile = 0 To maxcount
                Dim pb As New PictureBox With {.Width = 28, _
                                                      .Height = 310, _
                                                      .ImageLocation = CARDIMAGES(myfile), _
                                                      .SizeMode = PictureBoxSizeMode.StretchImage}
                AddHandler pb.Click, AddressOf BOX_CLICKED
            Next
    
            TableLayoutPanel1.Controls.AddRange(pictureBoxes.ToArray())
    
            TableLayoutPanel1.Enabled = True
            TableLayoutPanel1.Visible = True
    
    
        End Sub

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    What's the Height of the TLP after removing all the controls and rows? You might have to reset that too. This is all off the top of my head so I'm not testing anything.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    I added code to reset the height to that specified in designer. Still no difference. Still no images. I'm not sure where to go from here.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    I'll build a test project and see what I can see. Can't do it right away though.

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    My bad - I forgot to do the add. Som the pictures show, but now the handler isn't working right. I'll try debug for a while and watch some of the variables.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    Forgot to include the name of the picturebox in the add. I get images now and can click on them. Only thing is I'm right back to where I started with the scroll not resetting to the top. So, back where I started but with just a different way to load the array.
    Last edited by nddwe; Oct 24th, 2019 at 11:43 PM.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    Corrected an error as indicated in my previous post and I don't know how to delete this one. Sorry.
    Last edited by nddwe; Oct 24th, 2019 at 11:45 PM.

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    Got it working! I found an obscure reference on the net to:
    Code:
    ctrl.VerticalScroll.Value()
    that is supposed to allow for setting the scrollbar position.

    So, not knowing exactly what the actual value needed to be, I decided to try setting it to 1 to see what happens.
    Code:
            TableLayoutPanel1.VerticalScroll.Value() = 1
    I reset the row count first to that I calculate based on the number of entries to go into my picturebox table divided by the tablelayoutpanel's columncount (and add 1 if any renainder). That still doesn't seem to clear the scroll area, so scrollbars are present, but what is important is it is positioned back up to the first row. I assume there is probably something I don't understand that make this easier but it's what I could find.

    The code to do all this:
    Code:
         Private Sub DO_BY_CARD(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BY_CARD.Click
    
    
            Dim myfile As Integer = 0
    
            Dim myfile_path As String
            myfile_path = INSTALLATION_FOLDER & CARD_IMAGES_PATH
    
            Dim maxcount As Integer = 0
            Dim maxcount_cardimages = 0
    
            Dim xx As Integer = 0
            Dim wrkint As Integer = 0
    
            Dim filePaths = IO.Directory.GetFiles(myfile_path, "card*.jpg")
    
            maxcount = UBound(filePaths)
    
    
            SELECT_GAME.Text = "Play Card"
            SELECT_GAME.Enabled = True
            SELECT_GAME.Visible = True
    
    
            If maxcount_cardimages > 0 Then
                ReDim CARDIMAGES(maxcount)
            End If
    
    
            CARDIMAGES = filePaths
    
    
    
    
            TableLayoutPanel1.RowCount = maxcount / TableLayoutPanel1.ColumnCount
            If (maxcount Mod TableLayoutPanel1.ColumnCount) > 0 Then
                TableLayoutPanel1.RowCount += 1
            End If
    
            For Each pb As PictureBox In TableLayoutPanel1.Controls.Cast(Of Control).ToArray()
                pb.Dispose()
            Next
            TableLayoutPanel1.Controls.Clear()
            TableLayoutPanel1.Height = 620
            TableLayoutPanel1.Width = 1120
            TableLayoutPanel1.VerticalScroll.Value() = 1
    
    
            Dim pictureBoxes As New List(Of PictureBox)
    
            For myfile = 0 To maxcount
                Dim pb As New PictureBox With {.Width = 280, _
                                                      .Height = 310, _
                                                      .ImageLocation = CARDIMAGES(myfile), _
                                                      .SizeMode = PictureBoxSizeMode.StretchImage, _
                                                      .Name = myfile}
                AddHandler pb.Click, AddressOf BOX_CLICKED
                pictureBoxes.Add(pb)
            Next
    
            TableLayoutPanel1.Controls.AddRange(pictureBoxes.ToArray())
    
            TableLayoutPanel1.Enabled = True
            TableLayoutPanel1.Visible = True
    
    
        End Sub
    So, it may not be correct or elegant, but it does work. Hoping for a way to reset the scroll area inside to just the only loaded rows, but I don't how to do that either.

    EDIT: my understanding is that the local list array of pictureboxes is always brand new every time the subroutine is activated. Since the tablelayoutpanel is loaded each time as well, my understanding is that it is replacing the contents of tablelayoutpanel. I do the tablelayoutpanel.controls.cllear would have cleared any controls - like the previously loaded table - from it. However the actual scrollable area is still larger than the size of the tableloutpanel1 so the scroll bars are still there even if the resulting load of the table is only 1 row. I would have thought that area would have shrunk to 1 row.

    Well, at any rate, any comments/recommended changes, etc., are appreciated!! I will in the mean time try to find some obscure reference on the net that will reset that area inside of the tablelayoutpanel. Part of my problem trying to find something is try to come up with a relevant web search string. I'll keep trying.

    EDIT-EDIT: the ctrl.VerticalScroll.Value() seems to be something mentioned as something to do with vba scripts(?) with Exel spreadsheets from what I have found since. I just figured since it sounded the type of thing I needed I would try it. BTW - just exactly what the heck is VBA?
    Last edited by nddwe; Oct 26th, 2019 at 01:19 AM.

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: [RESOLVED] visual basic tablelayout panel loaded with an array of picture boxes,

    I marked this thread as resolved but don't know how to undo that. I need to add to it:

    Is there a way to remove a container, such as a tablelayoutpanel, at runtime? Perhaps if I did that and recreated the tablelayoutpanel it would solve this?

    I tried removing all the styles from all the columns and rows, then redefining them, but this didn't help either. This large "left over" in the scrollable area still remains when I reload with fewer controls. It just feels like something may not be actually working - but that's probably me not understanding how to this at runtime. It would seem the array that was loaded via a previous button click event is perhaps not being deleted?

    Any help would be greatly appreciated!

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: [RESOLVED] visual basic tablelayout panel loaded with an array of picture boxes,

    I've also tried resetting the rowcount to 2 and using the styles to set the height of each to 310 - still no difference. It's not deleting the space the old array took.

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: [RESOLVED] visual basic tablelayout panel loaded with an array of picture boxes,

    Quote Originally Posted by nddwe View Post
    Is there a way to remove a container, such as a tablelayoutpanel, at runtime?
    A TableLayoutPanel is a control. You already know how to remove a control because you're already doing it.

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: [RESOLVED] visual basic tablelayout panel loaded with an array of picture boxes,

    BUT...if you remove the control in code and then try to declare it again - trying to get around this problem, just entering the code craps out because it says the code further down that references that panel is trying to access a tablelayoutpanel that is not declared. And yes, I am creating a new tablelayoutpanel of the SAME NAME, SAME COLUMNS and ROWS as I did originally in designer. This is with Visual Basic that came with Visual Studio Express 2010. I can't use 2017 community edition because of an error it gives me when trying to start debug or building about some file missing. I looked on the net and found the Microsoft article about fixing this missing dll file but it doesn't work for me, so 201 is useless to me. As for a tablelayoutpanel being a control - no I didn't know that because it is listed in designer as a container and controls are list further up. So pardon my confusion. However, I don't mean to be rude, but please try to refrain from the snarky replies like this you have also posted in my other threads. They contribute absolutely nothing to solving the problem.
    Last edited by nddwe; Oct 31st, 2019 at 03:13 AM.

  23. #23
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [RESOLVED] visual basic tablelayout panel loaded with an array of picture boxes,

    A few points to clarify things:

    1) Don't worry about the version of VS, it isn't related. I feel that VS2010 was the most beautiful and balanced version of VS. There are advantages to the newer versions, and I do use VS2017, but I still use VS2010 because of how good it is. So don't feel too bad about that. However, whatever the issue is with VS2017 CE, you probably want to resolve that...though that's a subject for a different thread, as it's unrelated to this issue.

    2) There are lots of things that are controls..and other things. Forms are controls, even, which may not be immediately obvious. Upon looking at the documentation, I see that it still isn't totally clear that the Table Layout Panel derives from Control, but it does.

    3) In .NET, code isn't magic. You couldn't quite say that for something like VB6, where the form design wasn't quite the same code as everything else. In .NET, everything on that form is code that you could have written just as easily as the designer. Up until 2005, all the code to create, position, and set up, all controls (including the TableLayoutPanel) was found right on the one and only code page. It was inside a Region, so you could collapse it, but it was all there. This was bad in a couple ways, though also good in one way. It got in the way, and encouraged people to make bad changes, but at least everybody could see how every control was created, and realize it was all just stuff you could write.

    VS2005 introduced partial classes, such that a class can be spread across multiple files. With that introduction, all that designer code was moved to a different file with the .designer.vb extension. You can see those if you have Show All Files selected in the Solution Explorer. You might find it interesting to look at that, but it's mostly only for education, cause it's safest to ignore the file. If you make the wrong change in there, the designer won't work anymore, which is annoying. However, in this case, you might go look at how the panel is declared, added, and so forth, even though you are already doing that.

    4) JMC isn't snarky, he's blunt. He's not as folksy as I tend to be, but he's also always saying things that are worth hearing, even if they aren't always delivered the way you'd like them to be.
    My usual boring signature: Nothing

  24. #24

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: [RESOLVED] visual basic tablelayout panel loaded with an array of picture boxes,

    Thank you for your reply, and I apologize to JMC. I am the most basic of beginners at this. I am old IT guy, and we didn't have GUI's in those days nor event driven unless in the OS. I have been trying to learn this by the seat of my pants, so to someone who had experience my questions and replies might seem ridiculous, but they're the best I can do with my knowledge at the time. That's probably why nobody has come up with a solution for this problem I am having.

    Perhaps it would help to explain what I am doing. We have people in our senior's apartments who play Bingo once a week. They have always drawn the balls out of a plastic tub, including that my not belong to the current game. So, I wrote a program, the code of which I'm sure people would laugh at, but to let them just click to "draw a ball out of the tub".

    They have 2 ways they play. One way is only by game, the other is by a group of games on what they call a card.

    So, I have 2 buttons on the form for how they play - one for games only, 1 for cards only. To be able to accomplish this I wanted the process of selecting what game or games they are playing I created a folder that contains all jpg files with names that match the name of the games they can select from or just the cards they can select from since there are way fewer of those.

    The files in this folder have names like:

    game1.jpg
    game2.jpg
    .
    .
    .
    .
    and
    CARD-!.jpg
    CARD-2.jpg
    .
    .
    .

    So, the when the user clicks the button for play by game, I generate a "selection box" they can scroll through, omitting the "CARD" files. There might be, for example 35 games to select from. So I load these into a list of type picturebox, then load that as an array to the tablepanellayout. Since I have the columns fixed at 4 of 25% each in designer and the rows fixed in designer as 2 with a height of 310 (pixels??) each. Autosize is false as I don't want what shows on the form to physically grow. So, the user would have a scrollable menu of images for the user to click on. In this example there obviously would be more rows than can show in the tablelayout panel so a scroll bar shows to let them be able to chose from all the games.

    In this example, there would be 9 rows, so that scrollable area is, for lack of better terms, vertically sized at 9 rows. This part works fine.

    The problem comes when once that scrollable area is 9 rows "long", when you try to load fewer options to choose from by the user clicking by card, there are only 2 rows generated but that scrollable area remains sized at the previous 9 rows "long". So the user only has 6 images to choose from but there is a huge scrollable area of nothing.

    I have tried clearing the controls then setting the rowcount to only the number of rows needed to hold number-of-files-selected divided by the columncount and adding 1 to row count if there is a remainder.

    So, that's the problem. The code snippet for doing all of this shows both the "by card" option and the "by game" option. Perhaps this might explain things better and help isolate what the heck this simple guy is doing wrong:
    Code:
        Private Sub DO_BY_CARD(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BY_CARD.Click
    
    
            Dim myfile As Integer = 0
    
            Dim myfile_path As String
            myfile_path = INSTALLATION_FOLDER & CARD_IMAGES_PATH
    
            Dim maxcount As Integer = 0
            Dim maxcount_cardimages = 0
    
            Dim xx As Integer = 0
            Dim wrkint As Integer = 0
    
            Dim oldrowcount As Integer = 0
            Dim currentrowcount As Integer = 0
    
            Dim filePaths = IO.Directory.GetFiles(myfile_path, "card*.jpg")
    
            maxcount = UBound(filePaths)
    
    
            SELECT_GAME.Text = "Play Card"
            SELECT_GAME.Enabled = True
            SELECT_GAME.Visible = True
    
    
            If maxcount_cardimages > 0 Then
                ReDim CARDIMAGES(maxcount)
            Else
                ReDim CARDIMAGES(1)
            End If
    
    
            CARDIMAGES = filePaths
    
            TableLayoutPanel1.Height = 628
            TableLayoutPanel1.Width = 1120
            TableLayoutPanel1.VerticalScroll.Value() = 1
    
            For Each pb As PictureBox In TableLayoutPanel1.Controls.Cast(Of Control).ToArray()
                pb.Dispose()
            Next
    
            TableLayoutPanel1.Controls.Clear()
    
            TableLayoutPanel1.RowCount = maxcount / TableLayoutPanel1.ColumnCount
    
            If (maxcount Mod TableLayoutPanel1.ColumnCount) > 0 Then
                TableLayoutPanel1.RowCount += 1
            End If
    
            Dim pictureBoxes As New List(Of PictureBox)
    
            If maxcount > 0 Then
                xx = maxcount \ TableLayoutPanel1.ColumnCount
                If (maxcount Mod TableLayoutPanel1.ColumnCount) > 0 Then
                    xx += 1
                End If
                TableLayoutPanel1.RowCount = xx
            End If
    
    
            For myfile = 0 To maxcount
                Dim pb As New PictureBox With {.Width = 280, _
                                                      .Height = 310, _
                                                      .ImageLocation = CARDIMAGES(myfile), _
                                                      .SizeMode = PictureBoxSizeMode.StretchImage, _
                                                      .Name = myfile}
                AddHandler pb.Click, AddressOf BOX_CLICKED
                pictureBoxes.Add(pb)
            Next
    
            TableLayoutPanel1.Controls.AddRange(pictureBoxes.ToArray())
    
            TableLayoutPanel1.Enabled = True
            TableLayoutPanel1.Visible = True
    
    
        End Sub
    
    
    
        Private Sub DO_BY_GAME(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BY_GAME.Click
    
    
            Dim myfile As Integer = 0
    
            Dim myfile_path As String
            myfile_path = INSTALLATION_FOLDER & CARD_IMAGES_PATH
    
            Dim wrk_path As String = myfile_path
    
            Dim maxcount As Integer = 0
            Dim maxcount_cardimages = 0
    
            Dim xx As Integer = 0
    
            Dim filePaths = IO.Directory.GetFiles(myfile_path, "*.jpg")
    
            maxcount = UBound(filePaths)
            init_screen()
    
            SELECT_GAME.Text = "Play Game"
            SELECT_GAME.Enabled = True
            SELECT_GAME.Visible = True
    
            If maxcount_cardimages > 0 Then
                ReDim CARDIMAGES(maxcount)
            Else
                ReDim CARDIMAGES(1)
            End If
    
            CARDIMAGES = filePaths
    
            TableLayoutPanel1.Height = 620
            TableLayoutPanel1.Width = 1120
            tablelayoutpanel1.VerticalScroll.Value() = 1
    
            For Each pb As PictureBox In TableLayoutPanel1.Controls.Cast(Of Control).ToArray()
                pb.Dispose()
                xx -= 1
            Next
    
            TableLayoutPanel1.Controls.Clear()
    
            If maxcount > 0 Then
                xx = maxcount \ TableLayoutPanel1.ColumnCount
                If (maxcount Mod TableLayoutPanel1.ColumnCount) > 0 Then
                    xx += 1
                End If
                TableLayoutPanel1.RowCount = xx
            End If
    
            Dim pictureBoxes As New List(Of PictureBox)
    
            For myfile = 0 To maxcount
                If Mid(CARDIMAGES(myfile), (Len(INSTALLATION_FOLDER) + Len(CARD_IMAGES_PATH) + 1), 4) <> "CARD" Then
    
                    Dim pb As New PictureBox With {.Width = 280, _
                                                          .Height = 310, _
                                                          .ImageLocation = CARDIMAGES(myfile), _
                                                          .SizeMode = PictureBoxSizeMode.StretchImage, _
                                                          .Name = myfile}
                    AddHandler pb.Click, AddressOf BOX_CLICKED
                    pictureBoxes.Add(pb)
                End If
            Next
    
            TableLayoutPanel1.Controls.AddRange(pictureBoxes.ToArray())
    
            TableLayoutPanel1.Enabled = True
            TableLayoutPanel1.Visible = True
    
        End Sub
    
    
        Private Sub BOX_CLICKED(ByVal sender As Object, ByVal e As EventArgs)
    
            Dim pb As PictureBox = CType(sender, PictureBox)
            CARDIMAGE_SELECTED = Convert.ToDecimal(pb.Name)
            TableLayoutPanel1.Enabled = False
            TableLayoutPanel1.Visible = False
            PictureBox1.ImageLocation = CARDIMAGES(CARDIMAGE_SELECTED)
            PictureBox1.Refresh()
            SELECT_GAME.Enabled = True
    
    
        End Sub
        Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    
            End
    
        End Sub
        Public Sub ShowMessage(ByVal message As String)
            mymessage.Label1.Text = message
            mymessage.ShowDialog()
        End Sub
    This is also why I thought maybe, as a last ditch dumb way around it, I could at runtime remove the tablelayoutpanel and redeclare it The problem is when I enter that code it then kicks out errors saying the lines of code below reference a tablelayoutpanel that doesn't exist. So I can't actually code it - it won't let me.

    ANY help is greatly appreciated, and again I apologize to JMC. I'm just a simple guy who needs more simple replies so I can try to learn where my mistakes are.

    Thanks in advance.
    Last edited by nddwe; Oct 31st, 2019 at 04:39 PM.

  25. #25
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: [RESOLVED] visual basic tablelayout panel loaded with an array of picture boxes,

    Quote Originally Posted by Shaggy Hiker View Post
    Upon looking at the documentation, I see that it still isn't totally clear that the Table Layout Panel derives from Control, but it does.
    I'm not sure that that's quite right. I'm not sure what documentation you looked at but this page look like this at the top:

    Name:  Inheritance.jpg
Views: 462
Size:  13.9 KB

    The inheritance hierarchy is fairly clear from that last line.
    Quote Originally Posted by Shaggy Hiker View Post
    JMC isn't snarky, he's blunt.
    I've said that myself on occasions, but I may have said it too snarkily to get the message across.

  26. #26
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: [RESOLVED] visual basic tablelayout panel loaded with an array of picture boxes,

    Quote Originally Posted by nddwe View Post
    again I apologize to JMC.
    Apology unnecessary but accepted. I tend not to hold a grudge. The next thread is always a new start.

  27. #27

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: [RESOLVED] visual basic tablelayout panel loaded with an array of picture boxes,

    Quote Originally Posted by jmcilhinney View Post
    Apology unnecessary but accepted. I tend not to hold a grudge. The next thread is always a new start.
    Thanks! I'm just getting very frustrated at myself for not being able to figure out what must surely be something simple. I'm afraid I expressed that frustration out on you. I feel pretty dang stupid and I'm sure my novice questions sound ridiculously stupid to most people.

  28. #28
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: [RESOLVED] visual basic tablelayout panel loaded with an array of picture boxes,

    Quote Originally Posted by nddwe View Post
    I feel pretty dang stupid and I'm sure my novice questions sound ridiculously stupid to most people.
    No question sounds stupid insofar as all beginners start off not knowing stuff. The problem that some people, myself included, have is when people come up against something that they don't know and assume that they can't find out for themselves or just don't think it through logically. Again, that's a fairly natural thing for beginners to do but my approach is not to just answer such a question as asked and thus encourage similar lack of action in the future, but rather to encourage/push them to work through the pain and solve the problem - or at least parts of it - for themselves. I think that that is what will benefit them most in the long run so I'm OK with the odd bit of pushback. Some people never get the message and some do. That doesn't mean that I don't give a straight answer to any question ever though. There's still plenty of questions that I consider perfectly legitimate.

  29. #29

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: [RESOLVED] visual basic tablelayout panel loaded with an array of picture boxes,

    I understand completely. Years ago I was a systems programmer and systems administrator on several large mainframes. I used to get questions from analysts and programmers that I used to tell them that long line of manuals over there is for everyone and you can find your answer there.. That's what is making this so frustrating for me. I've read Microsoft documents online. I've looked in the help. I've looked for threads on the net to see if I could find answers there and if not, copy the code, try it and see what it does so I get a better understanding. I have searched the proverbial hi and low using every search string I could think of and can't find anything the refers to this particular problem. I know I could shrink my code by having the same handler for both buttons and modifying my code based on which button the call returns when I look it up by the sender. At this point I've kept everything separate just so I can try to deal with one thing at a time. I know some of my prior threads have perhaps come across as not having done my research first, but I really have tried. In this particular case I have looked at every Microsoft document I could find on containers, panels and a tablelayoutpanels. I've read about all kinds of things I don't understand yet but don't seem relevant. I don't think my problem is a result of needing to reassign styles to the rows, such as the height, as I haven't done anything that should change that from what's already set in designer and I don't change at runtime - at least not that I'm aware of. There are a "zillion" of those things I have looked into one at a time, tried my best to understand and if sounded even remotely possible I would try it in my code to see what happened. So, I'm stuck. If one of the analyst or programmers came to me at this point with sufficient documentation I would then help them. Sometimes things weren't quite so obvious even given expertise on the hardware and the operating system. As far as I am aware, I have done my "due diligence" - not in the context of how that's normally used when referring to contracts, etc., but in the area of having gone as far as I currently understand to go. When I try to start digging into classes, what "internally" a control, etc. are - well I'm getting lost. My mind is a lot older now and just doesn't work like it did back in those days. So, please give me your honest opinion, is there something more I should be trying to read and understand before I can fix this on my own? I am truly lost at this point. Thank you!

  30. #30

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: [RESOLVED] visual basic tablelayout panel loaded with an array of picture boxes,

    So, when the rowcount is set lower than it's previous count I finally noticed that the last row (in the case row 2) does not show a row divider and instead it's all the way at the end. I thought this indicated that the row height of the 2nd didn't get reset back to my desired height - 310. So I did a bunch of reading in the help and couldn't find what I was looking for. I searched the net and found a post that mentioned clearing the rowstyles, then redefining the styles - in this case the height.

    So, I first set the rowcount to 2 - the desired rows i needed. Then I did the tablelayoutpanel.rowstyles,clear() to clear the size of rows as well as any other settings. I then looped through starting at 0 and through the rowcount - 1 to set the relative to zero index. Then for each row I set the tablelayoutpanel. style to size.mode of absolute then setting the style height to 310.

    I would have assumed this would reset it back to where it is initially in the designer - 2 rows with a absolute height of 310. I therefore assumed it would add the picturebox controls array into those 2 rows, and that each of those rows would have a height of 310 - essentially (I thought) getting rid of the big scroll area the resulted when initially there were more than 2 rows so there was scrolling. Nope. Still has that huge empty scroll area, and it still looks like there is no row divider at what I thought should be the end of the 2nd row. So I retested going immediately to the option so that only 2 rows were generated. This appears to show correctly, except for something I noticed - there is still not row divider after the last row. Am I misunderstanding that there should still be some sort of divider (or perhaps a "line") at the end of the last row?

    I suppose I'm clear off on something that ends up not applying to my problem, since obviously it didn't fix it. I just would have thought that resizing the row heights would have cleared all of that.
    I'm getting SO aggravated that I can't figure this out on my own. I've looked at what I at least were the things I needed to look at: the Microsoft page that gives the start to using a tablelayoutpanel and then all of the various things it describes for it. which is a whole lot of pages I read through. Nothing sticks out as something that should fix the problem. So, it obviously HAS to be something I'm doing, but I can't find it.

    Right now I'm giving up. If I was using something like the DEC Windows or, God forbid, Curses, I could do this. But I'm just to dang mad to continue so the users in the building of this program are just going to have to go back to the old way and scroll right to actually show the files in a filechooser and scroll vertically to what ever they need to. i'm an old guy trying to help them out but it's not worth the frustration at feel at myself for not being able to SEE the error. I'm too dang old for that. I would have hoped someone would have been able to duplicate the problem and at least point me at some documentation I need to read.

    So thanks for the help that has been provided, but I'm giving up.

  31. #31
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [RESOLVED] visual basic tablelayout panel loaded with an array of picture boxes,

    Quote Originally Posted by jmcilhinney View Post
    I'm not sure that that's quite right. I'm not sure what documentation you looked at but this page look like this at the top:

    Name:  Inheritance.jpg
Views: 462
Size:  13.9 KB

    The inheritance hierarchy is fairly clear from that last line.

    I've said that myself on occasions, but I may have said it too snarkily to get the message across.
    HA! That is the exact page I looked at, but the HTML formatting was messed up, so I didn't see what you showed in that post, and I didn't understand what I was looking at...or how the formatting got messed up in just the way I saw it. In particular, the arrows in the inheritance hierarchy were on a different line from the words, and the words were run together. I saw a ComponentControl, which isn't something I'm familiar with...probably because it doesn't exist. Anybody else who saw the same thing might not have been clear on whether it was a control or a component or some third kind of hybrid animal.
    My usual boring signature: Nothing

  32. #32

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    So this time I do have it solved. Setting the row count to 0 prior to setting the number of rows and loading the new picturebox's. When I read on this before I thought it would destroy the complete way the row information was set in designer. Instead, it cleared all the rows but didn't change the default definitions for the row such as the row height, which I was worried about. This indeed completely reset the "view" with the large scrollable area. I'm not sure why deleting the rows didn't do the same thing - I don't understand that and don't claim to.

  33. #33

    Thread Starter
    Lively Member
    Join Date
    Oct 2019
    Posts
    107

    Re: visual basic tablelayout panel loaded with an array of picture boxes, view reset

    I'm going to summarize as best i can of what I have learned in this thread thanks to people like jmcilhinney and what ultimately solved the 2 problems I was having.

    The first problem was "forcing" the vertical scrollbar back to the top. I found this:

    Code:
    TableLayoutPanel.VerticalScroll.Value() = 1
    While I have left that line in my code, I *think* the problem was actually fixed by getting the actual size in rows "shrunk" when the tablelayoutpanel was loaded with fewer pictureboxes than in a previous button click.

    The second problem was having a button click fill a tablelayoutpanel with pictureboxes, all of a fixed size, the columncount and column width set to absolute values, and the row count initially defined as 2 rows of a fixed height. These 2 rows of that fixed height allowed 2 rows to show in the tablelayoutpanel on the screen, with scrollbars to get to the rest of the rows When this was filled via the selected button click the tablelayoutpanel might have 20 rows in it - it varies. Then clicking a different button resulted in what should have been just 2 rows of pictureboxes, but while showing those rows there was also a huge empty area to scroll down that made it appear it still thought it contained the previous number of rows, say 20. I tried setting the tablelayoutpanel.rowcount to the number of rows I knew I would need - but no difference. I went through a loop and deleted all the known rows - still no difference. If you have read through this thread you could see how I was getting extremely frustrated with not being able to figure this out and I did really give up. Then going through yet more documents on the net I found a reference to setting the tablelayoutpanel.rowcount to 0 to actually remove all rows. I was afraid that doing so might make me have to dynamically add rows and using the styles set each row's height. As it turns out I only needed to set the rowcount to 0. I fully admit I don't understand why this worked but deleting the rows didn't. At any rate it works as one would expect it to now.

    In the designer, i have the tablelayoutpanel properties that would appear to me to be the ones that matter for this set as:

    autosize=false -> I don't want the size on the form to change as the panel rows increase.

    autoscroll=true

    columncount=4

    columns as a collection of with each row defined as 25% -> this gives me 4 columns of an equal fixed size

    rowcount=2

    rows as a collection with each of the 2 rows of a absolute height of 320 pixels -> combined with the size of the tablelayoutpanel on the screen results in my need to show exactly to visible rows on the screen


    So, what I needed to accomplish has FINALLY been accomplished. I had a lot of help here. I don't claim to necessarily understand why some things didn't work and while others have. But they have worked and from all the reading I online I *think* this is how I was supposed to do it.

    If this in anyway helps anyone, thank the people who replied to my previous posts and to this one.

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