VB.NET Forms Panel repeated grids height Sizing - vb.net

I'm more of website developer with C# and having difficulty with VB.Net forms with VB.NET. Please review the screenshot. Parts blue color bar is header label in Panel. After that, there are some labels and then there is a Grid (LocationGrd). There are two rows, and I'm repeating the labels and location grid. How do I reduce the gap between the top and bottom boxes, there is too much gap between the first and second control boxes. Any clue?
For Index = 0 To Core.POParts.Count - 1
PartTypeDT = App.GetPartTypeForPart(Core.POParts(Index).PartNum, Core.POParts(Index).ProjectCodeOrSalesOrderID, Core.POParts(Index).IsProject)
'// Part Number
Core.PartNumLblArray.Add(GetLabel(PartNumLbl_Const, Index))
PartsPnl.Controls.Add(Core.PartNumLblArray(Index))
Core.PartNumTxtArray.Add(GetText(PartNumTxt_Const, Index, Core.POParts(Index).PartNum, Core.POParts(Index).PartID, MyBackColor))
PartsPnl.Controls.Add(Core.PartNumTxtArray(Index))
......
Next
Private Function GetLocationsGrd(Index As Integer) As DataGridView
Dim MyGrd As New DataGridView
Dim MyLocationCol As New System.Windows.Forms.DataGridViewComboBoxColumn()
Dim MyQuantityCol As New System.Windows.Forms.DataGridViewTextBoxColumn()
MyLocationCol.FillWeight = LocationCol.FillWeight
MyLocationCol.HeaderText = "Location"
MyLocationCol.Name = "LocationCol_" & Index
MyQuantityCol.DefaultCellStyle = QuantityCol.DefaultCellStyle
MyQuantityCol.FillWeight = QuantityCol.FillWeight
MyQuantityCol.HeaderText = "Quantity"
MyQuantityCol.Name = "QuantityCol_" & Index
With MyGrd
.AllowUserToDeleteRows = False
.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill
.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {MyLocationCol, MyQuantityCol})
.ContextMenuStrip = Me.ALContextMenuStrip
.Location = New System.Drawing.Point(LocationsGrd.Left, (Index * 200) + LocationsGrd.Top)
.Name = LcationsGrd_Const & Index
.Size = New System.Drawing.Size(LocationsGrd.Width, LocationsGrd.Height)
End With
Return MyGrd
End Function

Related

Rotate / Scroll picture boxes

I have a row of PictureBoxes created at run time which occupy more of the form's visible width. I want them to scroll at certain intervals so the user sees all of them if he waits.
I believe I must code to a Timer.
But what is the code for that? (The Form is set scrollable, but I do not want the user to interact with it. Just click the PB he likes)
Code for PB creation
'' In Form_Load:
Dim allSeries As IEnumerable(Of String) =
Directory.EnumerateFiles(root, "*.jpg", SearchOption.AllDirectories)
For i = 0 To allSeries.Count - 1
pb(i) = New PictureBox With {
.Name = "pb" + i.ToString,
.BackColor = Color.Transparent,
.Size = New Point(250, 300),
.BorderStyle = BorderStyle.None,
.SizeMode = PictureBoxSizeMode.Zoom,
.Top = 10,
.Left = pbLeft,
.Cursor = Cursors.Hand,
.Image = Image.FromFile(allSeries(i).ToString), 'Get the Image from the Directory
.Tag = Path.GetDirectoryName(allSeries(i)) 'Store Direcyory path
}
Controls.Add(pb(i))
pbLeft = pbLeft + 300 'position next to previous
'Next
Next
Thanks!

Unexpected TableLayoutPanel Behavour

Hi having some issues with some nested tablelayoutpanels in a custom control
I have a tablelayoutpanel in a tablelayoutpanel in a tablelayoutpanel Crazy i know but it keeps it uniform and ordered
The custom control consists of 2 tablelayoutpanels that is placed in a table layout panel on a form and in a preview area of my main form.
Having them set at design time works fine but dynamically adding / removing the topmost ones with 1 row 2 columns into the one below it that has 1 column and x rows only Seems to break the autosizing behavior I'm chasing.
So I want the cells & rows to resize automatically based on the contents in this case labels but still remain in a neat ordered layout
There's no docking anywhere in the hierarchy of controls just anchors here and there
Here's my code to add the tablelayoutpanels below
"https://i.stack.imgur.com/vRfhE.png"
Private Sub AddControl(ByRef Counter As Counter)
Dim Gpanel As New TableLayoutPanel
Dim tlabel As New Label
Dim clabel As New Label
Dim pad As Integer = Counter.Cpad
TLPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink
Gpanel.AutoSizeMode = AutoSizeMode.GrowAndShrink
Gpanel.AutoSize = True
Gpanel.GrowStyle = TableLayoutPanelGrowStyle.AddRows
Gpanel.BorderStyle = BorderStyle.FixedSingle
Gpanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset
TLPanel.BorderStyle = BorderStyle.FixedSingle
Gpanel.Dock = DockStyle.None
Gpanel.Padding = New Padding(0)
Gpanel.Anchor = AnchorStyles.Top Or AnchorStyles.Left
Gpanel.BackColor = Color.Transparent
Gpanel.RowCount = 1
Gpanel.ColumnCount = 2
Gpanel.RowStyles.Add(New RowStyle(SizeType.AutoSize))
Gpanel.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))
Gpanel.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))
tlabel.Dock = DockStyle.None
clabel.Dock = DockStyle.None
tlabel.GetPreferredSize(Size.Empty)
clabel.GetPreferredSize(Size.Empty)
tlabel.Text = Counter.Clabel
clabel.Text = Counter.Ccount.ToString
tlabel.Padding = New Padding(pad)
clabel.Padding = New Padding(pad)
tlabel.Anchor = AnchorStyles.None
clabel.Anchor = AnchorStyles.None
tlabel.ForeColor = Color.FromName(Counter.Clcolor)
clabel.ForeColor = Color.FromName(Counter.Ccolor)
Dim fontstyle As New FontStyle
fontstyle = Counter.ClfontStyle
tlabel.Font = New Font(Counter.Clfont, Counter.Clfontsize, fontstyle)
fontstyle = Counter.CcfontStyle
clabel.Font = New Font(Counter.Ccfont, Counter.Ccfontsize, fontstyle)
Gpanel.Controls.Add(tlabel, 0, 0)
Gpanel.Controls.Add(clabel, 1, 0)
TLPanel.Controls.Add(Gpanel, 0, Counter.ID)
TLPanel.RowCount += 1
TLPanel.RowStyles.Add(New RowStyle(SizeType.AutoSize))
Dim Styles As TableLayoutRowStyleCollection = TLPanel.RowStyles
Dim Cstyles As TableLayoutColumnStyleCollection = Gpanel.ColumnStyles
Gpanel.RowStyles.Item(0) = New RowStyle(SizeType.AutoSize)
TLPanel.ColumnStyles.Item(0) = New ColumnStyle(SizeType.AutoSize)
For i = 0 To Cstyles.Count - 1
Cstyles.Item(i) = New ColumnStyle(SizeType.AutoSize)
Next
For i = 0 To Styles.Count - 1
Styles.Item(i) = New RowStyle(SizeType.AutoSize)
Next
TLPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset
AutoSize = True
AutoSizeMode = AutoSizeMode.GrowAndShrink
End Sub
'

Datagridview fills on second attempt. Never first attempt in Tab Control

I have a tab list where each tab has its own "x" amount of datagridviews. For some reason the first active tab fills the datagridviews just fine. When I click on the second tab in the Tab Control, the datagridviews fill the data but the gridview shows empty [When I followed the code it showed the records being filled into the datagridview].
If I click to another tab and then back to the second tab the gridview refills again and then shows the data. Does anyone know what may cause this? It is always the first attempt that never initially fills the other tabs [aside the first active tab]
So from what I can tell... nested Datagridviews can't be dynamically filled at first load using myDataGrid.Rows.Add(.....). The only way I was able to do this on first load was to build a Datatable and fill that instead. Can't understand why but it works....
With myDataGrid
Dim f As Font = .Font
.Columns.Clear()
.DataSource = Nothing
.DataMember = Nothing
.ColumnCount = 3
.AutoGenerateColumns = False
'.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells
.Columns(0).Name = "MsgID"
.Columns(0).HeaderText = "MsgID"
.Columns(0).DataPropertyName = "MsgID"
.Columns(0).Visible = False
.Columns(1).Name = "Message"
.Columns(1).HeaderText = "Message"
.Columns(1).DataPropertyName = "Message"
.Columns(2).Name = "Sig"
.Columns(2).HeaderText = "Sig"
.Columns(2).DataPropertyName = "Sig"
.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
.Font = New Font(f, FontStyle.Bold)
.DefaultCellStyle.Alignment = 1
.Refresh()
End With
Dim dt As New DataTable
dt.Columns.Add("MsgID")
dt.Columns.Add("Message")
dt.Columns.Add("Sig")
dt.Columns(0).AutoIncrement = True
With ArrMessages(x)
Dim R As DataRow = dt.NewRow
R(0) = .MsgID
R(1) = .Msgs
R(2) = .Sig
dt.Rows.Add(R)
End With
'assign sourcing
myDataGrid.DataSource = dt
myDataGrid.Refresh()
Me.myDataGrid.Parent.Refresh()
myDataGrid.Refresh()

Can't Refresh DataGridView on TabControl Page When There are More Columns Added to DataGridView

There is a DataGridView spawned dynamically and a TabControl is assigned as a parent. The DataGridView is populated with random numbers. While there are no problems with headers or cell values, I can't seem to be able to resize the DataGridView on the tab control page when more columns are added dynamically.
Is there an issue with the Parent scoping for the TabControl, that prevents regeneration and display of more columns on the TabControl, once everything is refreshed?
Dim datagridview1 As New DataGridView
Datagridview1.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
Dim dgvColumnHeaderStyle As New DataGridViewCellStyle()
dgvColumnHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Datagridview1.ColumnHeadersDefaultCellStyle = dgvColumnHeaderStyle
Datagridview1.AllowUserToAddRows = False
Datagridview1.ScrollBars = ScrollBars.Both
DataGridView1.Font = New System.Drawing.Font("Lucida Sans Typewriter", 8)
For j As Integer = 1 To UBound(columnheaders)
DataGridView1.Columns.Add(columnheaders(j), columnheaders(j))
Next
For j As Integer = 1 To UBound(columnheaders)
DataGridView1.Columns(columnheaders(j)).HeaderText = columnheaders(j)
Next
For i As Integer = 0 To UBound(rowheaders) - 1
Dim n As Integer = DataGridView1.Rows.Add()
For j = 0 To UBound(columnheaders) - 1
DataGridView1.Rows.Item(n).Cells(j).Value = Rnd()
If i Mod 2 = 0 Then DataGridView1.Rows.Item(i).Cells(j).Style.BackColor = Color.White
If i Mod 2 <> 0 Then DataGridView1.Rows.Item(i).Cells(j).Style.BackColor = Color.AliceBlue
Next
Next
Datagridview1.Visible = True
Datagridview1.Height = Me.Height - 100
Datagridview1.Width = Me.Width - TabControl2.Left - 100
Datagridview1.Parent = TabControl2.TabPages(1)
TabControl2.TabPages(1).Refresh()
TabControl2.Refresh()
Datagridview1.Refresh()
RESOLVED: It worked out that any time you programatically add a datagridview to a tabcontrol, if you update (recreate) the datagridview and want to see the new datagridviw on the same tabcontrol page, you have to clear the controls on the tab page. Following gives an example for using a TabControl1 control and page 0, i.e., the first tab:
Me.TabControl1.SelectedTab = TabControl2.TabPages.Item(0)
Me.TabControl1.TabPages(0).Controls.Clear()
Me.TabControl1.TabPages(0).Controls.Add(datagridview1)

label doesn't appear on dynamically created form vb.net

I'm staring at my code for hours now and I don't understand what's going on. I'm creating a form with a number of checkboxes and labels, based on a textbox value from another form.
This is the code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim lb As Label
Dim cb1 As CheckBox
Dim cb As CheckBox
Dim i As Integer
Dim j As Integer
For i = 1 To CInt(Aantal.Text) - 1
indivwoningen.Width = indivwoningen.Size.Width + 21
indivwoningen.Button1.Location = New Point(indivwoningen.Button1.Location.X + 21, indivwoningen.Button1.Location.Y)
lb = New Label
indivwoningen.Controls.Add(lb)
lb.Text = i + 1
lb.Font = indivwoningen.Label23.Font
lb.Location = New Point(indivwoningen.Label23.Location.X + 21 * i, indivwoningen.Label23.Location.Y)
For j = 1 To 18
cb1 = New CheckBox
indivwoningen.Controls.Add(cb1)
cb = indivwoningen.Controls.Find("CheckBox" & j & "00", False)(0)
cb1.Location = New Point(cb.Location.X + 21 * i, cb.Location.Y)
cb1.Width = cb.Width
cb1.Text = cb.Text
If i < 10 Then
cb1.Name = "CheckBox" & j & "0" & i
Else
cb1.Name = "CheckBox" & j & i
End If
Next
Next
indivwoningen.Show()
End Sub
The created form has got two flaws:
Only the first created label is visible
The checkboxes aren't properly aligned.
I don't understand what's going on. Can someone help me?
EDIT: Here are pictures before I create extra controls and after
Set the Autosize property to True for the labels and set the Height of the checkboxes to the same height of your reference checkbox
For i = 1 To CInt(Aantal.Text) - 1
....
lb = New Label
indivwoningen.Controls.Add(lb)
lb.Text = i + 1
lb.Font = indivwoningen.Label23.Font
lb.Autosize = True
lb.Location = New Point(indivwoningen.Label23.Location.X + 21 * i, indivwoningen.Label23.Location.Y)
For j = 1 To 18
cb1 = New CheckBox
indivwoningen.Controls.Add(cb1)
cb = indivwoningen.Controls.Find("CheckBox" & j & "00", False)(0)
cb1.Location = New Point(cb.Location.X + 21 * i, cb.Location.Y)
cb1.Width = cb.Width
cb1.Height = cb.Height
cb1.Text = cb.Text
If i < 10 Then
cb1.Name = "CheckBox" & j & "0" & i
Else
cb1.Name = "CheckBox" & j & i
End If
Next
Next
Well, for the checkbox the explanation seems to be easy. The new checkbox has, by default, an Heigth of 24 pixels while the one drawn on the form as a smaller Height. So, because the check square is centered inside the Height of the Checkbox it appears to be not aligned to the reference checkbox.
For the labels the problem is of the same kind. Without setting the Autosize, the labels are created with a default size of 100x23 pixels. This means that the label with text "2" extends its size to cover the position of the labels with text "3","4","5","6", while the label with text "3" covers the label with text "7" and so on.
In any case setting AutoSize seems to be the default behavior, followed also in the Form.Designer.vb file where the controls are created following your design time instructions.
You could also try to set the Size of the dynamically created labels to the same size of the reference label and the effect is the same.