How to remove all created labels on my form - vb.net

I have this code and I want to know how to remove all created labels in my form.
I tried to put lbl.dispose() and ltr.dispose() on a Button but it says that it's not declared or inaccessible.
Dim break As Integer = 99
For i = 0 To break
If jobA > 0 And jobA > time Then
jobA = jobA - time
Dim lbl As Label = New Label
Dim ltr As Label = New Label
lbl.Location = New System.Drawing.Point(x, 280)
lbl.Size = New System.Drawing.Point(20, 20)
lbl.Text = time + spudow(waifuu)
ltr.Location = New System.Drawing.Point(y, 250)
ltr.Size = New System.Drawing.Point(20, 20)
ltr.Text = "A"
Me.Controls.Add(lbl)
Me.Controls.Add(ltr)
ElseIf jobA > 0 And jobA < time Then
Dim lbl As Label = New Label
Dim ltr As Label = New Label
lbl.Location = New System.Drawing.Point(x, 280)
lbl.Size = New System.Drawing.Point(20, 20)
lbl.Text = jobA + spudow(waifuu)
ltr.Location = New System.Drawing.Point(y, 250)
ltr.Size = New System.Drawing.Point(20, 20)
ltr.Text = "A"
Me.Controls.Add(lbl)
Me.Controls.Add(ltr)
ElseIf jobA = time Then
Dim lbl As Label = New Label
Dim ltr As Label = New Label
lbl.Location = New System.Drawing.Point(x, 280)
lbl.Size = New System.Drawing.Point(20, 20)
lbl.Text = jobA + spudow(waifuu)
ltr.Location = New System.Drawing.Point(y, 250)
ltr.Size = New System.Drawing.Point(20, 20)
ltr.Text = "A"
Me.Controls.Add(lbl)
Me.Controls.Add(ltr)
End If
Me.Refresh()

I like Zaggler's idea of using a List(Of Label), but I'll post another approach using the .Tag property of the Labels instead. This will work well enough if all the Labels will only be added to the Form itself. If you're going to spread the Labels across multiple containers then use the List(Of Label) approach instead as it will be easier. Additionally, you can get rid of a lot of redundant code by restructuring like this (I assume there is more un-shown code that is somehow changing the "x" and "y" values!):
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim break As Integer = 99
For i = 0 To break
Dim lbl As Label = New Label
Dim ltr As Label = New Label
lbl.Tag = "dynamic"
ltr.Tag = "dynamic"
lbl.Location = New System.Drawing.Point(x, 280)
lbl.Size = New System.Drawing.Point(20, 20)
lbl.Text = ""
ltr.Location = New System.Drawing.Point(y, 250)
ltr.Size = New System.Drawing.Point(20, 20)
ltr.Text = "A"
If jobA > 0 Then
If jobA > time Then
jobA = jobA - time
lbl.Text = time + spudow(waifuu)
ElseIf jobA < TimeOfDay Then
lbl.Text = jobA + spudow(waifuu)
End If
ElseIf jobA = time Then
lbl.Text = jobA + spudow(waifuu)
End If
If lbl.Text <> "" Then
Me.Controls.Add(lbl)
Me.Controls.Add(ltr)
End If
Next i
End Sub
Then, when you want to remove the dynamic Labels, you can use code like this:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim labels = Me.Controls.OfType(Of Label).Where(Function(x) x.Tag = "dynamic").ToList
For Each lbl As Label In labels
lbl.Dispose()
Next
End Sub

Sub RemoveAllLabels()
Dim Labels As New List(Of Label)
Dim Stack As New Stack(Of Control)
Stack.Push(Me)
While Stack.Count > 0
Dim Ctrl As Control = Stack.Pop()
Labels.Add(TryCast(Ctrl, Label))
For Each C As Control In Ctrl.Controls
Stack.Push(C)
Next
End While
For Each L As Label In Labels
If Not IsNothing(L) Then L.Dispose()
Next
End Sub

Related

Swap positions between three Panels

I have three panel type objects (A, B, and C), which have been dynamically generated within another panel type control.
My question is, how can I exchange panel B to the position of panel A and panel A to the position of panel B? This will be triggered by a click on a ToolStripMenuItem.
What I had thought, was to go through the arrangement of panels to know who exists and from there to work them, is that correct?
For Each obj As Control In Panel1.Controls
MsgBox(obj.Name)
Next
This is the code that I use to move to right:
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
Dim clickedPanel = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)
clickedPanel.Location = New Point((clickedPanel.Location.X + 120), clickedPanel.Location.Y)
End Sub
This is the code I use to generate objects dynamically:
Private Sub TileNavItem5_ElementClick(sender As Object, e As NavElementEventArgs) Handles TileNavItem5.ElementClick
Dim pos As Int32 = cInt(TextBox38.Text)
Dim poslabel As Int16 = cInt(TextBox42.Text)
Dim posY As Int16 = 330
Dim posX As Int16 = 3
Panel1.AutoScrollPosition = New Point(0, 0)
Dim pb As New Panel With
{
.Width = 120,
.Height = 460,
.Top = 10,
.Left = 10,
.BorderStyle = BorderStyle.FixedSingle,
.BackgroundImage = Image.FromFile("C:\example.bmp"),
.BackgroundImageLayout = ImageLayout.Stretch,
.ContextMenuStrip = CntxtMnuStrpSection,
.Name = "Panel" & Val(TextBox37.Text)
}
AddHandler pb.Click, AddressOf myClickHandler_b
Dim labela As New Label With {
.AutoSize = True,
.Location = New Point((poslabel), 12),
.Text = "Section " & CInt(TextBox37.Text),
.ForeColor = Color.White,
.BackColor = Color.Transparent,
.Font = New Font(Me.Font, FontStyle.Bold),
.Name = "Label" & CInt(TextBox37.Text)
}
pb.Location = New Point(0, 0)
pb.Location = New Point(pos, 20)
Panel1.Controls.Add(pb)
pb.Controls.Add(labela)
For j = 1 To 4
Dim pbdoors As New Panel With
{
.Width = 114,
.Height = 98,
.Top = 10,
.Left = 10,
.BorderStyle = BorderStyle.FixedSingle,
.BackgroundImageLayout = ImageLayout.Stretch,
.ContextMenuStrip = CntxtMnuStrpUnit,
.Name = "Unit" & Val(TextBox37.Text) & j
}
AddHandler pbdoors.Click, AddressOf myClickHandler_doors
pbdoors.Location = New Point(posX, posY)
pb.Controls.Add(pbdoors)
posY = (posY - 100)
Next
Panel1.AutoScrollPosition = New Point(Panel1.HorizontalScroll.Maximum, Panel1.VerticalScroll.Maximum)
TextBox37.Text = CInt(TextBox37.Text) + 1
TextBox38.Text = Val(TextBox38.Text) + 120
End Sub
You just need to find the controls. Swapping is the easy part.
Finding controls is also easy if you use Control.Controls.Find(String, Boolean). But you must at least know the control's name.
The difficulty comes in here
' TextBox37.Text = CInt(TextBox37.Text) + 1 ' implicit conversion from int to string
TextBox37.Text = (CInt(TextBox37.Text) + 1).ToString()
where you must find the control by name and you have built some integer into the name. Can you keep track of how many times 1 is added to TextBox37.Text?
If you can, you can pass it into this function, and the swapping will be performed
Private Sub swap(index1 As Integer, index2 As Integer)
' build the panel names
Dim p1Name = $"Panel{index1}"
Dim p2Name = $"Panel{index2}"
' find the panels
Dim p1 = DirectCast(Panel1.Controls.Find(p1Name, True).FirstOrDefault(), Panel)
If p1 Is Nothing Then Throw New ArgumentException("index1")
Dim p2 = DirectCast(Panel1.Controls.Find(p2Name, True).FirstOrDefault(), Panel)
If p2 Is Nothing Then Throw New ArgumentException("index2")
' swap the panels
Dim temp = p2.Location
p2.Location = p1.Location
p1.Location = temp
End Sub
swap(1, 2) will swap panel 1 with 2. swap(4, 6) will swap the panel 4 with 6. This logic is not included in your question (i.e. how many times is TileNavItem5_ElementClick called?), so you know better how to incorporate it. I hope it works for you.

Add an extra control to a button in VB.Net

I have a code that creates 30 buttons at run time on a form which works excellently
For i = 0 To 30
btn = New Button
Dim old As Padding = btn.Margin
Dim old2 As Padding = btn.Padding
btn.Tag = "X1"
btn.Name = "Drane"
btn.Text = "The power of trying"
btn.Width = 95
btn.Height = 120
btn.Margin = New Padding(10, 10, 10, 10)
btn.Padding = New Padding(0, 4, 0, 2)
btn.TextAlign = ContentAlignment.BottomCenter
btn.ImageAlign = ContentAlignment.TopCenter
btn.Font = New Font("Lucida Sans Unicode", 11)
btn.ForeColor = Color.Black
btn.BackColor = Color.White
Next i
Now I'm trying to add an extra control to hold the ids for buttons that I created at run time
btn.id = i
which would hold the ids of each particular button so I can get the value in other functions in my application. I know I have to add this somehow as a control but I just don't know how.
Please any help will be greatly appreciated in resolving this
This will extend the Button class and add an ID property.
Public Class ButtonMod
Inherits Button
Public Property ID As Integer
End Class
Then you just create them in a loop as you were doing already:
For i As Integer = 1 To 30
Dim btn As New ButtonMod
Dim old As Padding = btn.Margin
Dim old2 As Padding = btn.Padding
btn.Tag = "X1"
btn.Name = "Drane"
btn.Text = "The power of trying"
btn.Width = 95
btn.Height = 120
btn.Margin = New Padding(10, 10, 10, 10)
btn.Padding = New Padding(0, 4, 0, 2)
btn.TextAlign = ContentAlignment.BottomCenter
btn.ImageAlign = ContentAlignment.TopCenter
btn.Font = New Font("Lucida Sans Unicode", 11)
btn.ForeColor = Color.Black
btn.BackColor = Color.White
btn.ID = i
Next

TableLayoutPanel add Controls not visible

I am trying to dynamically add images to a TableLayoutPanel. The add appears to work, when I look at the contents of the control, the new items appear to have been added. The RowCount = 10, the IEnumerable has 18 items that contain the controls I have added.
My problem is, is that when the form is shown, only the first row is visible. I cannot dynamically set the Visible property to True on anything, the instruction is ignored. What am I missing?
Code:
Private Sub LoadTable()
' get the width in pixels of the columns
Dim oSizeType As SizeType = TableLayoutPanel1.ColumnStyles(0).SizeType
Dim Width As Single
Select Case oSizeType
Case SizeType.Percent, SizeType.AutoSize
Width = TableLayoutPanel1.Width / TableLayoutPanel1.ColumnCount
Case SizeType.Absolute
Width = TableLayoutPanel1.ColumnStyles(0).Width
End Select
' Fix the height of the rows
Dim Height As Single = Width
Dim oPicture As New PictureBox
Dim Cols As Integer = 1
Dim Rows As Integer = -1
Dim Cell As String = String.Empty
' loop through all the images from the folder
For i As Integer = 0 To m_FileList.Count - 1
' establish the current row/column in the table
Dim j As Integer = i + 1
'MsgBox(Fix(j / 2))
If Fix(j / 2) <> CDbl(j / 2) Then
Cols = 0
Rows += 1
' add a row if we have moved to the next row
If Rows > 0 Then TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.Absolute, Height))
' this doesn't happen automatically
TableLayoutPanel1.RowCount = Rows + 1
Else
Cols = 1
End If
' this is used for the name of some controls
Cell = "R" & Rows & "C" & Cols
' now scale the image to fit into the Table Cells
Dim oPictureBox As New PictureBox
oPictureBox.Height = Height
oPictureBox.Width = Width
Dim oImage As Bitmap = New Bitmap(Bitmap.FromFile(CType(m_FileList.Item(i).Value, String)))
' set the PictureBox properties
oPictureBox.BackgroundImageLayout = ImageLayout.Stretch
oPictureBox.BorderStyle = BorderStyle.Fixed3D
' scale the image to the PictureBox size
'Dim oResized As Image = New Bitmap(Bitmap.FromFile(CType(oPictureBox.Tag, String)))
'ImageEdit.ResizeImage(oImage, oResized, picEnlargement.Width, picEnlargement.Height, False)
ScaleImage(oPictureBox, oImage)
' set the Image of the PictureBox
oPictureBox.Image = oImage
oPictureBox.Name = "PictureBox" & i
' get the path of the current file
Dim f As String = m_FileList(i).Value
'set the properties of the new controls
Dim t As String = GetTitle(f)
'oLabel.Text = t
'oLabel.AutoSize = True
'oLabel.Location = New System.Drawing.Point(30, 110)
oPicture = New PictureBox
With oPicture
SetToolTip(oPicture, f)
oPicture.Tag = f
.Image = oPictureBox.Image
.Dock = DockStyle.Fill
.Size = New System.Drawing.Size(100, 100)
.SizeMode = PictureBoxSizeMode.StretchImage
.Location = New System.Drawing.Point(2, 2)
.Cursor = Cursors.Hand
.Name = "PictureBox" & i + 1
.Visible = True
End With
'here we add the controls to a layout panel to
'manage the positioning of the controls
Dim oContainer As New Panel
With oContainer
.Dock = DockStyle.Fill
.Margin = New System.Windows.Forms.Padding(0)
.Controls.Add(oPicture)
'.Controls.Add(oLabel)
.MaximumSize = New Size(Height, Width)
.MinimumSize = New Size(Height, Width)
.Name = "Container_" & Cell
.Visible = True
End With
' add the
TableLayoutPanel1.Controls.Add(oContainer, Cols, Rows)
'TableLayoutPanel1.SetRow(oContainer, Rows)
'TableLayoutPanel1.SetColumn(oContainer, Cols)
TableLayoutPanel1.Controls.Item(i).Name = "Control_" & Cell
TableLayoutPanel1.Controls.Item(i).Enabled = True
TableLayoutPanel1.Controls.Item(i).Visible = True
'here we add a handler for the picture boxs click event
AddHandler oPicture.Click, AddressOf oPictureClickEvent
Next
TableLayoutPanel1.Visible = True
For i As Integer = 0 To TableLayoutPanel1.Controls.Count - 1
TableLayoutPanel1.Controls(i).Enabled = True
TableLayoutPanel1.Controls(i).Visible = True
Next
End Sub
What was missing was setting the TableLayoutPanel Height. After adding the controls, you need to adjust the Height of the table to a multiple of the height of the rows * (number of rows - 1), that's the basic requirement, but needs a tweak to stop the last row being stretched...
Private Sub LoadTable()
Dim oPictures As Control() = {}
'Dim oTableLayoutPanel As New TableLayoutPanel
' get the width in pixels of the columns
Dim oSizeType As SizeType = TableLayoutPanel1.ColumnStyles(0).SizeType
Dim Width As Single
Select Case oSizeType
Case SizeType.Percent, SizeType.AutoSize
Width = TableLayoutPanel1.Width / TableLayoutPanel1.ColumnCount
Case SizeType.Absolute
Width = TableLayoutPanel1.ColumnStyles(0).Width
End Select
' Fix the height of the rows
Dim Height As Single = Width
Dim oPicture As New PictureBox
Dim Cols As Integer = 1
Dim Rows As Integer = -1
Dim Cell As String = String.Empty
TableLayoutPanel1.RowCount = Fix(m_FileList.Count / 2)
' loop through all the images from the folder
For i As Integer = 0 To m_FileList.Count - 1
' establish the current row/column in the table
Dim j As Integer = i + 1
'MsgBox(Fix(j / 2))
If Fix(j / 2) <> CDbl(j / 2) Then
Cols = 0
Rows += 1
' add a row if we have moved to the next row
TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.Absolute, Height))
' this doesn't happen automatically
TableLayoutPanel1.RowCount = Rows + 1
Else
' this line should never be needed but just in case
If Rows < 0 Then Rows = 0
Cols = 1
End If
' this is used for the name of some controls
Cell = "R" & Rows & "C" & Cols
'--------------------------------------------
' set up the PictureBox
'--------------------------------------------
Dim oPictureBox As New PictureBox
' get the path of the current file
Dim f As String = m_FileList(i).Value
'set the properties of the new controls
Dim t As String = GetTitle(f)
' create the PictureBox
With oPictureBox
.Height = Height
.Width = Width
' load the image from the current file path
Dim oImage As Bitmap = New Bitmap(Bitmap.FromFile(CType(m_FileList.Item(i).Value, String)))
' set the PictureBox properties
.BackgroundImageLayout = ImageLayout.Stretch
.BorderStyle = BorderStyle.Fixed3D
' scale the image to the PictureBox size
ScaleImage(oPictureBox, oImage)
' set the Image of the PictureBox
.Image = oImage
.Name = "PictureBox" & i + 1
.Dock = DockStyle.Fill
.Size = New System.Drawing.Size(Height, Width)
.SizeMode = PictureBoxSizeMode.StretchImage
.Location = New System.Drawing.Point(0, 0)
.Cursor = Cursors.Hand
.Visible = True
.Enabled = True
' set the Tag to the filepath of the image
.Tag = f
' set the ToolTip for the PictureBox
' This becomes the "Title" from the File MetaData
SetToolTip(oPictureBox, f)
'here we add a handler for the PictureBox click event
AddHandler oPictureBox.Click, AddressOf PictureClickEvent
End With
oPictures.Add(oPictureBox)
Next
' now add the picturebox to the next cell addres
With TableLayoutPanel1
' add the
.Controls.AddRange(oPictures)
'.Controls.Add(oPictureBox, Cols, Rows)
'.SetRow(oPictureBox, Rows)
'.SetColumn(oPictureBox, Cols)
'.Controls.Item(i).Name = "Control_" & Cell
'.Controls.Item(i).Enabled = True
'.Controls.Item(i).Visible = True
End With
TableLayoutPanel1.Visible = True
For i As Integer = 0 To TableLayoutPanel1.Controls.Count - 1
TableLayoutPanel1.Controls(i).Enabled = True
TableLayoutPanel1.Controls(i).Visible = True
Next
' adjust the height of the table to a multiple of the number of new rows * the Height of each row
TableLayoutPanel1.Height = (TableLayoutPanel1.RowCount - 1) * Height
' set the TableLayoutPanel properties
TableLayoutPanel1.Dock = DockStyle.Top
TableLayoutPanel1.AutoSize = False
TableLayoutPanel1.Visible = True
TableLayoutPanel1.Enabled = True
TableLayoutPanel1.Focus()
TableLayoutPanel1.Parent = Me.Panel1
End Sub

Runtime datagridview

I want to design datagridview at run time I want it to fill with data and appear on the form according to comboboxtext and if combobox is empty it disappear and when I click on datagridview I want the cell to be selectedvalue in the combobox and datagriddisapear
And here the code that I tried
Private Sub ComDr_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ComDr.KeyPress
Dim d As New DataGridView
Dim dt As DataTable = New DAL().selectdatatable(String.Format("select Dr_Name,Dr_No from DR where Dr_Name like '%{0}%'", ComDr.Text))
If dt.Rows.Count > 0 Then
' Try
Controls.Add(d)
d.DataSource = dt
d.BringToFront()
With d
.Name = "Dr_Name"
.Width = ComDr.Width
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
.ColumnHeadersHeight = 30
.Height = 10 * 35
.ForeColor = Color.Blue
.BackgroundColor = Color.Beige
.RowHeadersVisible = False
.BackgroundColor = Color.BurlyWood
'.DefaultCellStyle.SelectionForeColor = Color.Yellow
' .DefaultCellStyle.SelectionForeColor = Color.Black
' .DefaultCellStyle.Font = New Font("tahoma", 13)
.Location = New System.Drawing.Point(680, ComDr.Location.Y + 30)
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End With
For i As Integer = 0 To d.Rows.Count - 1
Dim row As DataGridViewRow = d.Rows(i)
row.Height = 35
d.Rows(i).DefaultCellStyle.BackColor = If(i Mod 2 = 0, Color.Wheat, Color.BurlyWood)
Next
AddHandler d.CellClick, AddressOf d_cellclick
'ComDr.DisplayMember = d(0, d.SelectedRows(0).Index).Value.ToString
'ComDr.ValueMember = d(1, d.SelectedRows(0).Index).Value.ToString
'ComDr.Text = String.Empty
'Controls.Remove(d)
' ComDr.SelectedValue = d(0, d.SelectedRows(0).Index).Value.ToString
'Catch ex As Exception
'MsgBox(ex.Message)
'End Try
' Controls.Remove(d)
End If
End Sub

create labels on a form taken from cells in a datagridview on another form vb.net

I have a datagridview on a form with 6 columns and an undetermined number of rows. The user is entering values into this datagridview. I would now like to create an invoice based on the entries in the datagridview. I am creating this invoice on another form. Unfortunately, the values in the cells are not showing up with the code I have. This is the code located in the Invoice class.
Dim lbl(5) As Label
For Each row As DataGridViewRow In Main.DataGridView1.Rows
If Not row.IsNewRow Then
Dim x As Integer = 0
For Each col As DataGridViewColumn In Main.DataGridView1.Columns
i = row.Index
j = col.Index
With lbl(x)
.AutoSize = True
.BackColor = System.Drawing.SystemColors.Control
.Font = New Font(lbl(x).Font.FontFamily, 8.45, FontStyle.Regular)
.ForeColor = System.Drawing.SystemColors.ControlText
.Location = New System.Drawing.Point(i * 111 + 6, (i + 1) * 24 + 16)
.Text = Main.DataGridView1.Rows(i).Cells(j).Value
End With
MsgBox(lbl(x).Text)
GroupBoxInvoiceInvoice.Controls.Add(lbl(x))
x += 1
Next
End If
Next
Any help would be greatly appreciated!
Here is the new block of code.
Dim Label1, Label2, Label3, Label4, Label5, Label6 As New Label
Dim lbl() As Control = {Label1, Label2, Label3, Label4, Label5, Label6}
Dim val() As String = {"Date", "Category", "Description", "Units", "Rate", "Amount"}
For Each row As DataGridViewRow In Main.DataGridView1.Rows
If Not row.IsNewRow And i < Main.DataGridView1.Rows.Count Then
Dim x As Integer = 0
For Each col As DataGridViewColumn In Main.DataGridView1.Columns
i = row.Index
j = col.Index
With lbl(x)
.AutoSize = True
.BackColor = System.Drawing.SystemColors.Control
.Font = New Font(lbl(x).Font.FontFamily, 8.45, FontStyle.Regular)
.ForeColor = System.Drawing.SystemColors.ControlText
.Location = New System.Drawing.Point(j * 111 + 6, (i + 1) * 24 + 16)
.Text = Main.DataGridView1.Rows(i).Cells(j).Value
End With
GroupBoxInvoiceInvoice.Controls.Add(lbl(x))
x += 1
j += 1
Next
i += 1
j = 0
x = 0
End If
Next
Thank you, Plutonix!! You're help has been awesome and greatly appreciated! You have shown much patience, also! Here is the final portion of code that worked for me based off of what you had shown below. Thanks again!!
i = 0
For Each row As DataGridViewRow In Main.DataGridView1.Rows
If Not row.IsNewRow Then
For c As Integer = 0 To row.Cells.Count - 3
Dim lbl As New Label
With lbl
.AutoSize = True
.BackColor = System.Drawing.SystemColors.Control
.Font = New Font(lbl.Font.FontFamily, 8.45, FontStyle.Regular)
.ForeColor = System.Drawing.SystemColors.ControlText
.Location = New System.Drawing.Point(c * 111 + 6, (i + 1) * 24 + 16)
.Text = row.Cells(c).Value.ToString
End With
GroupBoxInvoiceInvoice.Controls.Add(lbl)
Console.WriteLine("label {0}", row.Cells(c).Value.ToString)
Next
End If
i += 1
Next
You have 1 or 2 problems with your loop.
Dim lbl(5) As Label
This declares you plan to make an array of labels, but you never initialize it. When I try to run your code (w/o dgv) it crashes because there is nothing in lbl(x) ('Object reference not set to an instance of an object.'). There is no reason to stash the labels in an array if they are going to be added to a form. So, just create labels as you need them:
For Each col As DataGridViewColumn In Main.DataGridView1.Columns
i = row.Index
j = col.Index
Dim lbl As New Label ' make a new label
With lbl
.AutoSize = True
.BackColor = System.Drawing.SystemColors.Control
.Font = New Font(lbl(x).Font.FontFamily, 8.45, FontStyle.Regular)
.ForeColor = System.Drawing.SystemColors.ControlText
' the above props are being set to default values, so arent really needed
.Location = New System.Drawing.Point(i * 111 + 6, (i + 1) * 24 + 16)
' I think this needs to be changed:
.Text = Main.DataGridView1.Rows(i).Cells(j).Value.ToString
End With
MsgBox(lbl(x).Text)
GroupBoxInvoiceInvoice.Controls.Add(lbl)
x += 1
Next
This worked fine without the dgv reference, so if the labels are still blank, check to make sure there is something in those cells to display. If that loop was executing all the way something somewhere was eating the exception and could be eating another.
-------------------- EDIT ----------------------------
The problem is using row.index and col.index as for i,j. Those have to do with databands and return 0 otherwise. You want to loop thru the CELLS for each row:
For Each row As DataGridViewRow In dgv.Rows
If row.IsNewRow = False Then
' loop thru cells in row
For c As Integer = 0 To rrow.Cells.Count - 1
Dim lbl As New Label
With lbl
.AutoSize = True
.BackColor = System.Drawing.SystemColors.Control
.BorderStyle = BorderStyle.Fixed3D
.ForeColor = System.Drawing.SystemColors.ControlText
.Location = New System.Drawing.Point(r + 5, c * 22) ' FIX
' CONVERT value TO STRING!!!
.Text = rrow.Cells(c).Value.ToString
End With
Panel1.Controls.Add(lbl)
' prints as many time as there are labels/cells
Console.WriteLine("label {0}", rrow.Cells(c).Value.ToString)
Next
End If
Next
Console.WriteLine("{0}", Panel1.Controls.Count) ' my locations are foobar
' but the right number are there.
You will want to add r,c (or i, j) counters for positioning the labels on the form, but otherwise, all is well in LabelLand...of should be