Displaying images from a picturebox list - vb.net

I am trying to display a line of pictures in my program. But I am having a problem, where it is only showing the first image in the imagelist and only showing one image-box.
Private Cards As New List(Of PictureBox)
Private Sub SetupCards()
For i As Integer = 0 To imglist1.Images.Count - 1
Dim PicCard As PictureBox = New PictureBox()
PicCard.Width = 100
PicCard.Height = 200
PicCard.Top = 50
PicCard.Left = 50
Me.Controls.Add(PicCard)
PicCard.Image = imglist1.Images(i)
Cards.Add(PicCard)
Next i
End Sub

You're placing the picture boxes on top of each other, which is why you only see the last card. You've got to set a different Left property for every picture box you add.
The solution is rather simple. Just add the picture box's width to Left, multiplied by the current index i.
PicCard.Left = 50 + PicCard.Width * i

Don't need to keep the imaging controls in your own list if you add them to a parent container control.
Use ListView or third-party controls, or use custom drawing code if you need to use ListBox (which wraps respective Windows control). See
C# Can I display images in a list box?

Related

Location properties on winforms acting strange

I have a form where I allow the user to generate multiple panels with some various contents in the panels by pressing an "add" button. Depending on what the user does in the panel, the panel grows and shrinks to fit the contents. Because of this change is size, I have created a sub that formats the panels on the form.
Private Sub formatPanels(frm As Form)
Dim count As Integer = 0
Dim startPoint As Point = New Point(12, 80)
Dim endPoint As Point = New Point(0, 0)
Dim maxY As Integer = 0
For Each pnl As Control In frm.Controls
If TypeOf pnl Is Panel Then
ReDim Preserve _arr_Panels(count)
_arr_Panels(count) = pnl
count += 1
pnl.Location = startPoint
startPoint.Y += pnl.Size.Height + 30
End If
Next
End Sub
So as you can see, we loop through every panel and the first always begins at the location (12,80) and then increments with the size of the panel and some spacing.
HERE IS THE ISSUE. This ONLY happens when i am SCROLLED DOWN the form. The panels spacing all of a sudden screws up and decides to put the first panel hundreds of pixels down the form. Is the location property based on what you're looking at? So if I were scrolled down the form location(0,0) would be the top left of the current view? There must be some weird property to location that I am not aware of.
Thanks
This behavior is not related to a panel, but to any control on a form with AutoScroll = True and Anchor including Top. (Note: if Anchor didn't also include Left I had some strange positioning on the first call of the function.
The solution is described here which is to use AutoScrollPosition. If you change your startPoint to this it will adjust for the scroll position.
Dim startPoint As Point = New Point(12, Me.AutoScrollPosition.Y + 80)
And the documentation for AutoScrollPosition states this:
When adding controls programmatically to a form, use the AutoScrollPosition property to position the control either inside or outside of the current viewable scroll area.

programmably create picturebox inside panel

Hey all i have created a panel that i want to place a lot of pictureboxes inside of. I will be pulling pictures from the "my pictures" folder. I am wanting to have 3 pictureboxes per row.
My current code is this:
Dim newPictureBox As New PictureBox
newPictureBox.Image = Image.FromFile("C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg")
newPictureBox.Visible = True
newPictureBox.Top = 0 'needed calulation here for each picturebox
newPictureBox.Left = 0 'needed calulation here for each picturebox
newPictureBox.Width = 536
newPictureBox.Height = 338
newPictureBox.BackgroundImageLayout = ImageLayout.Stretch
Panel1.Controls.Add(newPictureBox)
That does just fine for the first picturebox inside the panel but i will be looping an unknown number of pictures that need pictureboxes to go with it. I know the next picturebox should have a Left value of 545 but i am unsure of how to calcualte that out? Then the top calculations for the second row, third row, etc...
What would the process be for getting the correct Top, Left coordinates for each picturebox so that it only has 3 pictureboxes in each row?

Why don't my Labels show in VB.NET Form

After reading this question, I wrote some code to create a label for each attribute of an xml element.
The problem is that when I run the project, my form only displays the first label. I've checked in the immediate window as well as debug window and all of the labels are loaded to the form, but none of them are displayed. Help?
Here's the code that runs when the form loads.
Dim doc As New XmlDocument()
doc.Load("xmlfile")
Dim ability As XmlNode = doc.GetElementsByTagName("ability").Item(0)
Dim numberofLabels = ability.Attributes.Count
ReDim labels(numberofLabels)
For counter As Integer = 0 To numberofLabels - 1
labels(counter) = New Label
labels(counter).Visible = True
labels(counter).Text = ability.Attributes.Item(counter).Name
labels(counter).Location = New System.Drawing.Point(10, 30 + counter * 10)
Me.Controls.Add(labels(counter))
Next
You should be using some layout manager, to help you with control positioning. Doing it manually is not worth the pain. Try using TableLayoutPanel or FlowLayoutPanel. Both can be docked or anchored to a parent control, so everything behaves very smoothly. Otherwise you are looking to write a lot of positioning/resizing code, and then maintaining it later.
Change the value of 10 in the original code line for a new point to a bigger value such as 40, so that new labels could appear separated visually:
labels(counter).Location = New System.Drawing.Point(10 + counter, 30 + counter * 40)

Reversi VB.net logic behind it

I am trying to make the reversi game in VB.Net. I have some difficulties translating the game`s logic into vb.net
If a button is black and the button next to it is white,than the button next to the white one will be black wen pressed.
newButton.tag = colum of button + (row of button * amount of columns)
-> I made 64 buttons via a function loop and added a tag
Dim knop As Button = sender
Dim value As String = knop.Tag
If value = "...(?)" Then
knop.BackColor = Color.Black
If ....(?)
End If
End If
I already made a scheme with the label of the buttons, but I find it hard to implement the logic. Can someone help me out with thid one?
EDIT: http://i.stack.imgur.com/3gdrJ.png
If you use Dim ButtonList As List(Of List(Of Button)) and add the buttons to the form in runtime you can add each the button for each row to a list then add that list to ButtonList. Now you can access each button by the indexes in the 2 dimensional list.
Since you're changing the backcolor just use that instead of using the tag.

Generate a new panel containing same check-boxes for each node in treeview VB.NET (Images Attached)

I want to generate a new panel with a click on each node in a treeview. But this each newly generated panel will have same check-boxes. In addition these check boxes are linked to groupboxes with a checked condition.
I can do this for 4-5 nodes by adding a panel for each node. But I have around 90 such nodes.
Kindly refer following images,
there are 90 such nodes.
Is there any easier way such as loop etc. to achieve this? or hard way (add panel for each node and use show/hide) is the only way?
Help will be really appreciated.
Cheers,
You can use a loop for this.
This is a sample:
First, you need to define some global variables:
Dim Panels(100) As Panel
Dim CheckBox1(100) As CheckBox
Dim CheckBox2(100) As CheckBox
Dim CheckBox3(100) As CheckBox
Dim Label1(100) As Label
Then, you should initialize controls on Form_Load() event:
For i As Integer = 0 To 100
'Initialize Controls
Panels(i) = New Panel()
CheckBox1(i) = New CheckBox()
CheckBox2(i) = New CheckBox()
CheckBox3(i) = New CheckBox()
Label1(i) = New Label()
'Set properties
CheckBox1.Left = 100
CheckBox2.Left = 100
CheckBox3.Left = 100
CheckBox1.Top = 100
CheckBox2.Top = 200
CheckBox3.Top = 300
Label1.Left = 100
Label1.Top = 50
Label1.Text = "ID : " & NodeNames(i) 'You should replace NodeNames(i) with a variable that you are using for the name of nodes.
'Add Controls to panel
Panels(i).Controls.Add(CheckBox1(i))
Panels(i).Controls.Add(CheckBox2(i))
Panels(i).Controls.Add(CheckBox3(i))
Panels(i).Controls.Add(Label1(i))
'Set visiblity of panel to false
Panels(i).Visible = False
'Add panel to the form.
Me.Controls.Add(Panels(i))
Next
And when you need to show a panel, you should do this:
Panels(i).BringToFront()
Panels(i).Visible = True
But having many panels in RAM is not such a good idea. I suggest you to use just one panel and modify it by code for each node. Because if you have many panels and many controls inside it, your application may use a large amount of RAM.