Positioning similar type objects on form programmatically in vb.net - vb.net

I have a from which contains some PictureBoxes. They can be from one to many.
I create them at run-time depending on the existence of specific folders.
I create them and place them the one next to each other. This means that with a scrollable form I can view all of them easy.
My question is this: How do I position them in "rows"? For a specific form size, there can be 5 labels next to each other and infinite rows of them
How do I achieve this?
My (working) code:
Public allSeries As IEnumerable(Of String) = System.IO.Directory.EnumerateDirectories(root)
For i As Integer = 1 To allSeries.Count
Dim pb As New Windows.Forms.PictureBox With {
.Name = "pb" & i.ToString,
.Size = New Drawing.Size(500, 500),
.Location = New Point(5, 5),
.BorderStyle = BorderStyle.FixedSingle,
.SizeMode = PictureBoxSizeMode.Zoom,
.Image = Image.FromFile(allSeries(i - 1).ToString + "\sImage.jpg"),
.Tag = traveldestination, 'Store Directory path
.Cursor = Cursors.Hand}
Me.Controls.Add(pb)
For i As Integer = 2 To allSeries.Count
With Me
.Controls.Item("pb" + i.ToString).Left = .Controls.Item("pb" + (i - 1).ToString).Left + 520
End With
Next
My (bad) and (not workng) code:
Dim pbsOnForm As Integer = 13 'total PictureBoxes on Form /for this instance
Dim pbsOnRow As Integer = 5 'PictureBoxes that "fit" in a row /for this intance)
For i As Integer = 1 To pbsOnForm
If i <= pbsOnRow Then
Me.Controls.Item("pb" + i.ToString).Top = Me.Controls.Item("pb" + i.ToString).Top
End If
If i > pbsOnRow And i <= 10 Then
Me.Controls.Item("pb" + i.ToString).Top = Me.Controls.Item("pb" + (i - pbsOnRow).ToString).Top
End If
Works, but when the PcrureBoxes will be more than 10, I do not know......

While using the TableLayoutPanel would fulfill most cases for this and is probably the best way to achieve this, here is some code to align the PictureBox's in row / column.
First we want to setup a method to handle the positioning. We need some variables scoped to the Form.
Dim counter As Integer = 0
Dim xPos As Integer = 5
Dim yPos As Integer = 5
Now we use these variables in a method that sets the location.
Private Sub PositionPictureBox(pb As PictureBox, Optional imgPerRow As Integer = 5)
pb.Location = New Point(xPos, yPos)
counter += 1
If counter = imgPerRow Then
counter = 0
xPos = 5
yPos = pb.Location.Y + pb.Height + 5
Else
xPos = pb.Location.X + pb.Width + 5
End If
End Sub
Finally we call the method when the PictureBox is instantiated.
For i As Integer = 1 To allSeries.Count
Dim pb As New Windows.Forms.PictureBox
With pb
.Name = "pb" & i.ToString()
.Size = New Drawing.Size(50, 50)
.Location = New Point(5, 5)
.BorderStyle = BorderStyle.FixedSingle
.SizeMode = PictureBoxSizeMode.Zoom
.Image = Image.FromFile("...")
.Tag = allSeries(i)
.Cursor = Cursors.Hand
End With
PositionPictureBox(pb)
Me.Controls.Add(pb)
Next

Related

How to dynamicallty create multiple controls at runtime

I am trying to add multiple labels to a userform at runtime
It's for the player names of a board game; and until the game starts the number of players are not known. I have managed to figure out for myself how to use the dynamic array function to create the list of players. I used a For.....Next loop to add the player names. I thought I could do that to add the labels to the form, but it only adds one. Depending on where the new control type is declared, it either adds the first player only, or the last player
This code produces one label only within the groupbox, the last player
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Players_Num As Integer = InputBox("Enter the number of players")
Dim Players(Players_Num) As String
Dim newText As New Label
For i = 0 To Players_Num - 1
Players(i) = InputBox("Enter player name")
Next
'This piece of code was jsut for me to test that I was successfully using a For...Loop
'to add the players names, and will be deleted later on
For x = 0 To Players_Num - 1
MessageBox.Show(Players(x))
Next
For z = 0 To Players_Num - 1
newText.Name = "txt" & Players(z)
newText.Text = Players(z)
newText.Size = New Size(170, 20)
newText.Location = New Point(12 + 5, 12 + 5)
GroupBox1.Controls.Add(newText)
Next
End Sub
End Class
This one places only the first player
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Players_Num As Integer = InputBox("Enter the number of players")
Dim Players(Players_Num) As String
For i = 0 To Players_Num - 1
Players(i) = InputBox("Enter player name")
Next
'This piece of code was jsut for me to test that I was successfully using a For...Loop
'to add the players names, and will be deleted later on
For x = 0 To Players_Num - 1
MessageBox.Show(Players(x))
Next
For z = 0 To Players_Num - 1
Dim newText As New Label
newText.Name = "txt" & Players(z)
newText.Text = Players(z)
newText.Size = New Size(170, 20)
newText.Location = New Point(12 + 5, 12 + 5)
GroupBox1.Controls.Add(newText)
Next
End Sub
End Class
I've tried this in vs 2015 and 2019 Community
Where is it going wrong?
From the looks of the code, you are correctly creating the controls but their location is the same for all of them, essentially, they are being place one of top of the other, the first is hidden with the second, which is hidden with the third.
The line
newText.Location = New Point(12 + 5, 12 + 5)
needs to be modified to place the labels at different locations.
Perhaps, something like:
newText.Location = New Point(12 + 5, 12 + (z * 25))
This will vertically align the labels with a gap of 25 between them
You are placing them all in the same location
newText.Location = New Point(12 + 5, 12 + 5)
Use your 'z' index to place them at different locations in order to be able to see them
For me it is easier to contain controls in a TableLayoutPanel then add the TLP to what ever control collection, such as a GroupBox This way you can couple a Label with TextBox, for example. Here's an example how you can create controls from a DataTable. In your case you would only need 1 ColumnStyle for labels, I just thought I would show you a good practice for future shortcuts. (I rarely use the designer to place controls)
'Start test data
Dim DtTable As New DataTable
With DtTable
Dim NewDtRow As DataRow = .NewRow
For i As Integer = 0 To 25
Dim DtCol As New DataColumn With {.ColumnName = "Col" & i, .DataType = GetType(String)}
.Columns.Add(DtCol)
NewDtRow(DtCol.ColumnName) = "Test" & i
Next
.Rows.Add(NewDtRow)
End With
'End test data
Dim TLP1 As New TableLayoutPanel With {.Name = "TlpFields"}
With TLP1
.BorderStyle = BorderStyle.Fixed3D
.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset
.AutoScroll = True
.AutoSize = True
.RowStyles.Clear()
.ColumnStyles.Clear()
.Dock = DockStyle.Fill
.ColumnCount = 2
.ColumnStyles.Add(New ColumnStyle With {.SizeType = SizeType.AutoSize})
End With
For Each DtCol As DataColumn In DtTable.Columns
With TLP1
.RowCount += 1
.RowStyles.Add(New RowStyle With {.SizeType = SizeType.AutoSize})
'create labels
.Controls.Add(New Label With {
.Text = DtCol.ColumnName,
.Anchor = AnchorStyles.Right}, 0, .RowCount)
'create textboxs
Dim TxtBox As New TextBox With {
.Name = "TextBox" & DtCol.ColumnName,
.Size = New Size(170, 20),
.Anchor = AnchorStyles.Left}
'add binding
TxtBox.DataBindings.Add("Text", DtTable, DtCol.ColumnName)
.Controls.Add(TxtBox, 1, .RowCount)
End With
Next
Controls.Add(TLP1)

VB.net Adding Picturebox with Label and Scrollbar dynamically

So basically what I wish to accomplish is that I set total of Pictureboxes to add and each picturebox has its own according label and if we reach 6 images we allow the scrollbar to go down.
Example:
http://i.imgur.com/5xWu5Y1.png
As you can see pictures are added accordingly with label.
How can I do this in code?
Currently I got something like this:
' Total PictureBoxes/Labels to Generate
Dim wTotalPictures = 1
' Original PictureBox Starting Point
Dim wOriginalLocation = New System.Drawing.Point(40, 54)
' Assigned Label Title
Dim wImageTitle As String = "PictureTitle"
Dim wNewImage As New PictureBox
Dim wImageSize = New Size(122, 173)
With wNewImage
.SizeMode = PictureBoxSizeMode.StretchImage
.Image = Image.FromFile("C:\Users\Jason\Pictures\newImage.jpg")
.Location = wOriginalLocation
.Size = wImageSize
End With
So any help would be greatly appreciated!
EDIT:
I've now managed to change it into this, this works way better but still not 100% automatic like I want it.
' Total PictureBoxes/Labels to Generate
Dim i As Integer = 0
Dim wTotalPictures = 5
' Original PictureBox Starting Point
Dim wOriginalLocation = New System.Drawing.Point(40, 54)
' Assigned Label Title
Dim wImageTitle As String = "PictureTitle"
Dim wImageSize = New Size(122, 173)
Dim wNewLocation As Integer = 0
Do Until i = wTotalPictures
' Setup
Dim wNewImage(i) As PictureBox
wNewImage(i) = New PictureBox
' Execute
wNewImage(i).SizeMode = PictureBoxSizeMode.StretchImage
wNewImage(i).Image = Image.FromFile("C:\Users\Jason\Pictures\newImage.jpg")
wNewImage(i).Size = wImageSize
If wNewLocation < 480 Then
wNewImage(i).Location = New System.Drawing.Point(40 + wNewLocation, 54)
ElseIf wNewLocation = 480 Then
wNewImage(i).Location = New System.Drawing.Point(40, 258)
wNewLocation = 0
End If
' Add to Form
MyTab.Controls.Add(wNewImage(i))
wNewImage(i).BringToFront()
wNewLocation = wNewLocation + 160
i += 1
Loop
Please help, thanks!

Drawing an array of PictureBoxes in vb.net

I'm trying to draw an array of PictureBoxes, for testing I use the same picture for each picturebox.
But instead of showing the picture, it shows the color blue.
I would show you a picture, but I dont have 10 reputation..
Dim teren(120) As PictureBox
Dim x_locatie As Integer = 1, y_locatie As Integer = 0
For i = 0 To 10
x_locatie = 210
For j = 0 To 12
teren(i * j) = New PictureBox()
teren(i * j).Size = New Size(61, 61)
teren(i * j).Name = "x" + i.ToString + "y" + j.ToString
teren(i * j).Location = New Point(x_locatie, y_locatie)
Dim locatie As String = folder + "\harta\test.png"
teren(i * j).ImageLocation = locatie
teren(i * j).Show()
Next
y_locatie += 61
Next
I also tried another method , but same result.
Sub PictureBox1_Paint(sender1 As Object, er As PaintEventArgs)
If myImage IsNot Nothing Then
Dim r As New Rectangle(x, y, xlatime, ylungime)
er.Graphics.DrawImage(myImage, r)
End If
End Sub
Sub deseneaza(ByVal poza As String, ByRef x_perm As Integer, ByRef y_perm As Integer, ByRef lungime As Integer, ByRef latime As Integer)
myImage = Image.FromFile(poza)
x = x_perm
y = y_perm
xlatime = latime
ylungime = lungime
Refresh()
End Sub
'this part of code is in body of another function
Dim x_locatie As Integer = 1, y_locatie As Integer = 0
For i = 0 To 10
x_locatie = 210
For j = 0 To 12
Dim locatie As String = folder + "\harta\test.png"
deseneaza(locatie, x_locatie, y_locatie, 61, 61)
Next
y_locatie += 61
Next
I saw in other threads that their problem solution was something like that Dim teren() As PictureBox {teren1, teren2 , ... , teren n} But the problem in my case is that I need 120 PictureBoxes, and I think that it must be a way to do this without writing 120 pictureboxes.
Please try this...it will generate 16 picture box, size 20x20, in a row. I put it under "FormLoading" event.
Dim Shapes(16) As PictureBox
For i = 1 To 16
Shapes(i) = New PictureBox
With Shapes(i)
.BackColor = SystemColors.Control 'Color.Green
.BackgroundImage = New Bitmap(My.Resources.led_blk)
.BackgroundImageLayout = ImageLayout.Zoom
.Size = New Size(20, 20)
.Visible = True
.Location = New Point( 23 * i, 50)
End With
Me.Controls.Add(Shapes(i))
Next
I think GDI+ will be the way to go. I think you need a custom class that has a rectangle structure as a member with other properties that help you with further logic with the character intersecting with them. Paint should be done in the Paint event of the surface control you are using - PictureBox has the best rendering - IMO.
Public Class Tile
Public Property Bounds As New Rectangle
Public Property IsImpassable As Boolean
'others you think of
End Class
Dim iTop = 325
Dim pBox(48) As PictureBox
Dim pinColor = Color.SkyBlue
Dim leftStart = 50
For j = 0 To 3
For i = 0 To 11
pBox(i) = New PictureBox
'pBox(i).Image = Image.FromFile("\NoTest.bmp")
pBox(i).Visible = True
pBox(i).BackColor = pinColor
pBox(i).Top = iTop + (j * 40)
pBox(i).Width = 20
pBox(i).Height = 20
pBox(i).Left = leftStart + (35 * i)
If i > 9 Then
pBox(i).Left = leftStart + (35 * i) + 15
pBox(i).Width = 25
End If
pBox(i).BringToFront()
pBox(i).SizeMode = PictureBoxSizeMode.StretchImage
Controls.Add(pBox(i))
Next
Next

dynamically layout buttons .net

Hie everyone
I have a procedure which puts buttons on a .net windows form using vb.net. It works ok but because I do not know the number of buttons I will be programming because they come from the database I would like a way to lay them out in rows of 10. I have programmed up to 50 which ios 5 rows but the method I am using will not work if there is more than 50. Is there a way do this. I have tried using mod of the number of boxes and it does not work.
Here is the code.
Private Sub AddButtons()
Dim xPos As Integer = 0
Dim yPos As Integer = 0
Dim n As Integer = 1
Dim numberOfBoxes As Integer
numberOfBoxes = txtNumberOfBoxes.Text
numberOfBoxes = numberOfBoxes + 1
' Declare and Initialize one variable
Dim btnArray(numberOfBoxes) As System.Windows.Forms.Button
For i As Integer = 0 To numberOfBoxes
btnArray(i) = New System.Windows.Forms.Button
Next i
While (n < numberOfBoxes)
With (btnArray(n))
.Tag = n + 1 ' Tag of button
.Width = 100 ' Width of button
.Height = 100 ' Height of button
If (n = 11) Then ' Location of second line of buttons:
xPos = 0
yPos = 120
ElseIf (n = 21) Then
xPos = 0
yPos = 240
ElseIf (n = 31) Then
xPos = 0
yPos = 360
ElseIf (n = 41) Then
xPos = 0
yPos = 480
ElseIf (n = 51) Then
xPos = 0
yPos = 600
End If
'If n Mod 10 = 0 Then
' xPos = xPos
' yPos = yPos + 50
'End If
' Location of button:
.Left = xPos
.Top = yPos
' Add buttons to a Panel:
pnlButtons.Controls.Add(btnArray(n)) ' Let panel hold the Buttons
xPos = xPos + .Width ' Left of next button
.Text = (n)
' for Event of click Button
AddHandler .Click, AddressOf Me.ClickButton
n += 1
End With
End While
btnAddButton.Enabled = False ' not need now to this button now
Label1.Visible = True
End Sub
I would suggest using a FlowLayoutPanel or, more likely, a TableLayoutPanel. You configure it to grow in the manner required and then all you have to do in code is Add the Button to its Controls collection. It handles all the layout so there's no calculation required.
Private Sub AddButtons()
Dim numberOfBoxes As Integer = txtNumberOfBoxes.Text
For i As Integer = 0 To numberOfBoxes - 1
'since row is an integer it won't have decimal places
'also \ divides without decimales-
Dim row As Integer = i \ 10
'The modulo operator is your friend. it gives you
'the rest of the devision.
' http://en.wikipedia.org/wiki/Modulo_operation
Dim col As Integer = i Mod 10
Dim newButton = New System.Windows.Forms.Button
With newButton
.Tag = i + 1 ' Tag of button
.Width = 100 ' Width of button
.Height = 100 ' Height of button
.Left = col * .Width
.Top = row * (.Height + 20)
.Text = i + 1
AddHandler .Click, AddressOf Me.ClickButton
End With
pnlButtons.Controls.Add(newButton)
Next i
btnAddButton.Enabled = False ' not need now to this button now
Label1.Visible = True
End Sub

problems adding controls to a groupbox

I am creating a grid of textboxes inside a groupbox in vb.net. they are all of a uniform width,height, font etc.. the code I currently have is this:
Dim new_cell As New TextBox
With new_cell
.Multiline = True
.Width = cell_width
.Height = cell_height
.Font = ("arial", 12)
End With
For j = 0 To N
For i = 0 To M
new_cell.Left = (i * cell_width) + i
new_cell.Top = (j * cell_width) + j
space.Controls.Add(new_cell)
Next
Next
where M,N are the grid size and space is the groupbox.
this was supposed to create the whole grid however it only creates one textbox at the bottom corner. This is because the changes to new_cell on the next iteration of the loop affect the previous control and so a new control is never added. I could replace the single textbox with an array and then loop through and add each element of the textbox array to the groupbox. this however creates ugly code and seems inefficient( what with the repeated properties for each cell) and so I was wondering whether there was a way to add a control to a groupbox and then disassociate (or something) the textbox variable and the textbox added to the groupbox.
You create only one TextBox. You need to move Dim new_cell As New TextBox inside the i loop.
For j = 0 To N
For i = 0 To M
Dim new_cell As New TextBox
With new_cell
.Multiline = True
.Width = cell_width
.Height = cell_height
.Font = ("arial", 12)
End With
new_cell.Left = (i * cell_width) + i
new_cell.Top = (j * cell_width) + j
space.Controls.Add(new_cell)
Next
Next
Or even better:
Dim cell_x As Integer = space.DisplayRectangle.Left
Dim cell_y As Integer = space.DisplayRectangle.Top
Dim cell_i As Integer = 0 ': Cell `i` spacing
Dim cell_j As Integer = 0 ': Cell `j` spacing
For j = 0 To N
For i = 0 To M
space.Controls.Add(New TextBox() With {
.Name = String.Format("N{0}M{1}", j, i),
.Multiline = True,
.Width = cell_width,
.Height = cell_height,
.Font = New Font("arial", 12),
.Left = (cell_x + (i * (cell_width + cell_i))),
.Top = (cell_y + (j * (cell_height + cell_j)))
})
Next
Next